JavaMail 中用于发送邮件的类有 Session 类和 Transport 类,当然也少不了 Message 类。它们之间的进行工作的关系和步骤如下:
各个类的关系与操作流程:
1、创建一个 Properties 对象,该类在 java.util 包中,以键-值对的形式设置邮件发送中需要用到的传输协议,如 SMTP 协议等;一般的 smtp 服务器都需要用户身份认证,所以这也要设置为 true ,以便向 smtp 服务器提交用户名、密码等信息;
2、先使用 Session 类中静态的 getInstance() 或getDefaultInstance() 获得自身对象,此时调用这两个方法应该传入上面创建的 Properties 对象;
3、再用 Session 对象调用 getTransport() 方法获得 Transport 抽象类的具体实现子类对象,如 MimeTransport 类;
4、将 Sesson 对象作为参数创建 MimeMessage 对象,即一封邮件,前面的文章已经有所讲述;
5、使用 MimeTransport 对象调用相关方法将 MimeMessage 对象发送出去。
由于前面的文章已经写了创建各类邮件的代码,所以在这里就直接拿来用而不再编写了,创建邮件的代码放在本文的最后面。我们使用到了上篇文章中的 WithAttachmentMessage 类作为一封邮件,稍稍修改一下 from 和 to 就可以从不同的邮箱发送给不同的邮箱了。
实现代码:
- import java.util.Properties;
-
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.MimeMessage;
-
- import createMails.WithAttachmentMessage;
-
-
-
-
- public class MessageSender {
-
-
-
-
- public Session createSession(String protocol) {
- Properties property = new Properties();
- property.setProperty("mail.transport.protocol", protocol);
- property.setProperty("mail.smtp.auth", "true");
-
- Session session = Session.getInstance(property);
-
-
-
- session.setDebug(true);
- return session;
- }
-
-
-
-
- public void sendMail(Session session, MimeMessage msg) throws Exception {
-
-
- String smtpServer = "smtp.sina.com";
- String user = "test_hao@sina.cn";
- String pwd = "123456";
-
-
- Transport transport = session.getTransport();
-
- transport.connect(smtpServer, user, pwd);
-
- transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
- transport.close();
- }
-
-
- public static void main(String[] args) throws Exception {
- MessageSender sender = new MessageSender();
-
-
- Session session = sender.createSession("smtp");
-
- MimeMessage mail = new WithAttachmentMessage().createMessage(session);
- sender.sendMail(session, mail);
- }
- }
测试发送结果(猜想:不同的 smtp 服务器对于同一邮件会有不同的解析格式、规范):
1、发送到 163 邮箱
163 将我们的正文部分的HTML 文本和图片都变成附件了,真奇怪,可能是因为正文是 HTML 格式。
2、发送到 sina 邮箱:
比 163 邮箱稍好一些,附件有3个,内嵌在 HTML 正文中的图片被当作附件处理了,正文中的纯文本被解析了,可是内嵌在 HTML 中的图片却显示不了。
3、发送到 QQ 邮箱:
效果与我们预期的完全一致!附件有两个,正文文本、图片全部正确解析并显示出来。
PK结果:
目前浅显的来看,应该是 QQ 邮箱最厉害了,此外,QQ 邮箱接收邮件的速度最快,不行可以试一试。当然,安全性什么的就不能从此处有所了解了。
深入:
由于在代码中开启了 JavaMail API 的调试功能,所以可以从控制台输出的响应信息中了解到更多的信息,特别是关于上面不同 smtp 服务器解析同一封邮件的过程,它可能跟我们传输、发送过去的具体过程有关。
小结:
现在回想起来,前面使用 telnet 程序在控制台中麻烦地手工发送纯文本邮件是多么低效。JavaMail API 将这一切与底层协作的过程、操作都封装掉了,这就是软件开发的目的吧。
本文使用到上一篇文章中的程序代码:
- import java.io.FileOutputStream;
- import java.util.Properties;
-
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
-
-
-
-
-
- public class WithAttachmentMessage {
-
-
-
-
- public MimeBodyPart createAttachment(String fileName) throws Exception {
- MimeBodyPart attachmentPart = new MimeBodyPart();
- FileDataSource fds = new FileDataSource(fileName);
- attachmentPart.setDataHandler(new DataHandler(fds));
- attachmentPart.setFileName(fds.getName());
- return attachmentPart;
- }
-
-
-
-
- public MimeBodyPart createContent(String body, String fileName)
- throws Exception {
-
- MimeBodyPart contentBody = new MimeBodyPart();
-
- MimeMultipart contentMulti = new MimeMultipart("related");
-
-
- MimeBodyPart textBody = new MimeBodyPart();
- textBody.setContent(body, "text/html;charset=gbk");
- contentMulti.addBodyPart(textBody);
-
-
- MimeBodyPart jpgBody = new MimeBodyPart();
- FileDataSource fds = new FileDataSource(fileName);
- jpgBody.setDataHandler(new DataHandler(fds));
- jpgBody.setContentID("logo_jpg");
- contentMulti.addBodyPart(jpgBody);
-
-
- contentBody.setContent(contentMulti);
- return contentBody;
- }
-
-
-
-
- public MimeMessage createMessage(Session session) throws Exception {
-
- String from = "test_hao@sina.cn";
- String to = "test_hao@163.com";
-
- String subject = "创建内含附件、图文并茂的邮件!";
- String body = "<h4>内含附件、图文并茂的邮件测试!!!</h4> </br>"
- + "<a href = http://haolloyin.blog.51cto.com/> 蚂蚁</a></br>"
- + "<img src = \"cid:logo_jpg\"></a>";
-
- MimeMessage msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(from));
- msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
- msg.setSubject(subject);
-
-
- MimeBodyPart attachment01 = createAttachment("F:\\java\\Hello_JavaMail.java");
- MimeBodyPart attachment02 = createAttachment("F:\\java\\Hello_JavaMail.7z");
- MimeBodyPart content = createContent(body, "F:\\java\\logo.jpg");
-
-
- MimeMultipart allPart = new MimeMultipart("mixed");
- allPart.addBodyPart(attachment01);
- allPart.addBodyPart(attachment02);
- allPart.addBodyPart(content);
-
-
- msg.setContent(allPart);
- msg.saveChanges();
- return msg;
- }
-
-
- public static void main(String[] args) throws Exception {
- WithAttachmentMessage mail = new WithAttachmentMessage();
- Session session = Session.getDefaultInstance(new Properties());
- MimeMessage message = mail.createMessage(session);
- message.writeTo(new FileOutputStream("withAttachmentMail.eml"));
- }
- }
我的相关文章:
图解使用Telnet程序手工发送邮件
简单的E-mail发送端程序的Java实现
图解使用telnet程序手工接收邮件及其过程
JavaMail:创建内含附件、图文并茂的邮件
本文出自 “蚂蚁” 博客,请务必保留此出处http://haolloyin.blog.51cto.com/1177454/354320