package cn.itcast.javamail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.junit.Test;
import cn.itcast.utils.MailUtils;
/**
* java邮件发送
*
* @author seawind
*
*/
public class JavaMailTest {
@Test
// 编写最复杂一封邮件,既要内嵌图片,也要携带附件
public void demo5() throws Exception {
// 步骤一 创建Session
Session session = MailUtils.createSession();
// 步骤二 编写邮件Message
MimeMessage message = new MimeMessage(session);
// 设置邮件头(简单邮件和复杂邮件相同
message.setFrom(new InternetAddress("aaa@estore.com"));
message.setRecipients(Message.RecipientType.TO, "bbb@estore.com");
message.setSubject("javamail发送最复杂邮件");
// 设置邮件体
MimeBodyPart pic = new MimeBodyPart();
pic.setDataHandler(new DataHandler(new FileDataSource("beauty.jpg")));
pic.setContentID("myimg");// 内嵌图片唯一标识
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(new FileDataSource(
"大嘴巴 - maybe的机率.mp3")));
attachment.setFileName(MimeUtility.encodeText("大嘴巴 - maybe的机率.mp3"));// 附件解决中文路乱码
MimeBodyPart content = new MimeBodyPart();
content.setContent("<h1>最复杂邮件,有图片,有附件</h1><img src='cid:myimg' />",
"text/html;charset=utf-8");
// 整合
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(pic);
mp1.addBodyPart(content);
mp1.setSubType("related");
MimeBodyPart temp = new MimeBodyPart();// 将multipart转换bodypart可以和其它bodypart一起合并
temp.setContent(mp1);
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attachment);
mp2.addBodyPart(temp);
mp2.setSubType("mixed");
message.setContent(mp2);
// 步骤三 发送邮件 Transport
MailUtils.sendMail(session, message);
}
@Test
// 携带附件邮件
public void demo4() throws Exception {
// 步骤一:创建Session
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "localhost");
properties.put("mail.smtp.auth", "true");// 连接认证
properties.put("mail.debug", "true");// 在控制台显示连接日志信息
Session session = Session.getInstance(properties);// 与邮件服务器连接会话
// 步骤二:创建Message
MimeMessage message = new MimeMessage(session);
// 设置邮件头(简单邮件和复杂邮件相同
message.setFrom(new InternetAddress("aaa@estore.com"));
message.setRecipients(Message.RecipientType.TO, "bbb@estore.com");
message.setSubject("javamail发送携带附件邮件");
// 设置邮件体
MimeBodyPart attachment = new MimeBodyPart();
// 链接数据文件
attachment.setDataHandler(new DataHandler(new FileDataSource(
"大嘴巴 - maybe的机率.mp3")));
// 显示附件名
// 中文附件名编码 JavaMail 提供工具类 MimeUtility
attachment.setFileName(MimeUtility.encodeText("大嘴巴 - maybe的机率.mp3"));// 设置filename自动生成Content-Disposition:attachment;filename=xxx
MimeBodyPart content = new MimeBodyPart();
content.setContent("<h1>附件是首好听的歌曲!</h1>", "text/html;charset=utf-8");
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(attachment);
mimeMultipart.addBodyPart(content);
mimeMultipart.setSubType("mixed");
message.setContent(mimeMultipart);
// 步骤三:Transport发送邮件
Transport transport = session.getTransport();
transport.connect("aaa", "111");
transport.sendMessage(message, message.getAllRecipients());
}
@Test
// 内嵌图片邮件
public void demo3() throws Exception {
// 发送邮件需要三个步骤
// 步骤一:创建Session
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "localhost");
properties.put("mail.smtp.auth", "true");// 连接认证
properties.put("mail.debug", "true");// 在控制台显示连接日志信息
Session session = Session.getInstance(properties);// 与邮件服务器连接会话
// 步骤二:创建Message
MimeMessage message = new MimeMessage(session);
// 设置邮件头(简单邮件和复杂邮件相同
message.setFrom(new InternetAddress("aaa@estore.com"));
message.setRecipients(Message.RecipientType.TO, "bbb@estore.com");
message.setSubject("javamail发送内嵌图片邮件");
// 设置邮件体(简单邮件和复杂邮件区别 就在于邮件体)
MimeBodyPart pic = new MimeBodyPart();// 图片
// 链接数据文件
pic.setDataHandler(new DataHandler(new FileDataSource("beauty.jpg")));
// DataSource dataSource = new FileDataSource("beauty.jpg");
// DataHandler dataHandler = new DataHandler(dataSource);
// pic.setDataHandler(dataHandler);
// 设置一个唯一标识(用于在正文中引入)
pic.setContentID("mypic");
MimeBodyPart content = new MimeBodyPart(); // 邮件正文
content.setContent("<h1>美女图片</h1><img src='cid:mypic' />",
"text/html;charset=utf-8");
// 将两个BodyPart整合
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(pic);
mimeMultipart.addBodyPart(content);
// 设置关系
mimeMultipart.setSubType("related");
message.setContent(mimeMultipart);
// message.writeTo(System.out);
// 步骤三:Transport发送邮件
Transport transport = session.getTransport();
transport.connect("aaa", "111");
transport.sendMessage(message, message.getAllRecipients());
}
@Test
public void demo2() throws AddressException, MessagingException {
// 步骤一 创建与邮件服务器连接会话
Properties properties = new Properties();// 配置与服务器连接参数
// 设置properties 属性
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "localhost");
properties.put("mail.smtp.auth", "true");// 连接认证
properties.put("mail.debug", "true");// 在控制台显示连接日志信息
Session session = Session.getInstance(properties);// 与邮件服务器连接会话
// 步骤二 编写Message
MimeMessage message = new MimeMessage(session);// 代表一封邮件
// from字段
message.setFrom(new InternetAddress("aaa@estore.com"));
// to 字段
message.setRecipients(Message.RecipientType.TO, "bbb@estore.com");
// subject字段
message.setSubject("javamail发送简单邮件");
// 邮件正文内容
message.setText("使用javamail 可以发送简单邮件 ...");
// 步骤三 使用Transport发送邮件
Transport transport = session.getTransport();
// 发邮件前进行身份校验
// transport.connect("aaa", "111");
transport.sendMessage(message, message.getAllRecipients());
}
@Test
public void demo1() throws Exception {
// 使用JavaMailAPI 设计一封邮件
// 步骤一 创建 javamail中Session
Properties properties = new Properties();// 配置与服务器连接参数
Session session = Session.getInstance(properties);// 与邮件服务器连接会话
// 步骤二 通过Session创建Message邮件
MimeMessage message = new MimeMessage(session);// 代表一封邮件
// 步骤三 编写邮件内容
// from字段
message.setFrom(new InternetAddress("aaa@estore.com"));
// to 字段
message.setRecipients(Message.RecipientType.TO, "bbb@estore.com");
// subject字段
message.setSubject("javamail发送简单邮件");
// 邮件正文内容
message.setText("使用javamail 可以发送简单邮件 ...");
message.writeTo(System.out);
}
}
java mail 初步
最新推荐文章于 2021-12-07 20:51:26 发布