---
title: "（转）JavaMail：邮件发送以及sina、163、QQ服务器不同的解析结果（附图）"
author: "wahahappy"
date: "2011-05-01T19:13:00Z"
tags: []
categories: ["编程语言"]
slug: "javamail-sina-163-qq"
draft: false
lastmod: "2011-05-01T19:26:38Z"
id: 1025833
url: "https://wahahappy.iteye.com/blog/1025833"
---

呼…前面的准备终于为真正实用的 JavaMail 功能、操作——发送邮件打下了基础，现在我们想要把一封正文部分是 HTML 格式、并包含两个附件的复杂邮件发送给指定的收件人。

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;  
/**  
 * 邮件发送程序  
 * @author haolloyin  
 */ 
public class MessageSender {  
 
    /**  
     * 创建Session对象，此时需要配置传输的协议，是否身份认证  
     */ 
    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);  
          
        // 启动JavaMail调试功能，可以返回与SMTP服务器交互的命令信息  
        // 可以从控制台中看一下服务器的响应信息  
        session.setDebug(true);   
        return session;  
    }  
 
    /**  
     * 传入Session、MimeMessage对象，创建 Transport 对象发送邮件  
     */ 
    public void sendMail(Session session, MimeMessage msg) throws Exception {  
          
        // 设置发件人使用的SMTP服务器、用户名、密码  
        String smtpServer = "smtp.sina.com";  
        String user = "test_hao@sina.cn";  
        String pwd = "123456";  
 
        // 由 Session 对象获得 Transport 对象  
        Transport transport = session.getTransport();  
        // 发送用户名、密码连接到指定的 smtp 服务器  
        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();  
          
        // 指定使用SMTP协议来创建Session对象  
        Session session = sender.createSession("smtp");  
        // 使用前面文章所完成的邮件创建类获得 Message 对象  
        MimeMessage mail = new WithAttachmentMessage().createMessage(session);  
        sender.sendMail(session, mail);  
    }  
} 

 
```

**测试发送结果（猜想：不同的 smtp 服务器对于同一邮件会有不同的解析格式、规范）：**

**1、发送到 163 邮箱**

163 将我们的正文部分的HTML 文本和图片都变成附件了，真奇怪，可能是因为正文是 HTML 格式。

[**![MailSender01](http://img1.51cto.com/attachment/201007/21/1177454_12796919311Lbi.png "MailSender01")**](http://img1.51cto.com/attachment/201007/21/1177454_1279691930hmjJ.png)

[![MailSender02](http://img1.51cto.com/attachment/201007/21/1177454_1279691932YJpY.png "MailSender02")](http://img1.51cto.com/attachment/201007/21/1177454_1279691931Dszt.png)

**2、发送到 sina 邮箱：**

比 163 邮箱稍好一些，附件有3个，内嵌在 HTML 正文中的图片被当作附件处理了，正文中的纯文本被解析了，可是内嵌在 HTML 中的图片却显示不了。

[![MailSender03](http://img1.51cto.com/attachment/201007/21/1177454_1279691933asMY.png "MailSender03")](http://img1.51cto.com/attachment/201007/21/1177454_12796919325Qga.png)

[![MailSender04](http://img1.51cto.com/attachment/201007/21/1177454_1279691933q6tz.png "MailSender04")](http://img1.51cto.com/attachment/201007/21/1177454_1279691933x3mt.png)

**3、发送到 QQ 邮箱：**

效果与我们预期的完全一致！附件有两个，正文文本、图片全部正确解析并显示出来。

[![MailSender05](http://img1.51cto.com/attachment/201007/21/1177454_1279691935O0jr.png "MailSender05")](http://img1.51cto.com/attachment/201007/21/1177454_1279691934IU4m.png)

**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; 

/** 
* 创建内含附件、图文并茂的邮件 
* @author haolloyin 
*/ 
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; 
} 

/** 
* 根据传入的邮件正文body和文件路径创建图文并茂的正文部分 
*/ 
public MimeBodyPart createContent(String body, String fileName) 
throws Exception { 
// 用于保存最终正文部分 
MimeBodyPart contentBody = new MimeBodyPart(); 
// 用于组合文本和图片，"related"型的MimeMultipart对象 
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); 

// 将上面"related"型的 MimeMultipart 对象作为邮件的正文 
contentBody.setContent(contentMulti); 
return contentBody; 
} 

/** 
* 根据传入的 Seesion 对象创建混合型的 MIME消息 
*/ 
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 部分 
MimeBodyPart attachment01 = createAttachment("F:\\java\\Hello_JavaMail.java"); 
MimeBodyPart attachment02 = createAttachment("F:\\java\\Hello_JavaMail.7z"); 
MimeBodyPart content = createContent(body, "F:\\java\\logo.jpg"); 

// 将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象 
MimeMultipart allPart = new MimeMultipart("mixed"); 
allPart.addBodyPart(attachment01); 
allPart.addBodyPart(attachment02); 
allPart.addBodyPart(content); 

// 将上面混合型的 MimeMultipart 对象作为邮件内容并保存 
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")); 
} 
} 

```

 

 转自[http://haolloyin.blog.51cto.com/1177454/354320](http://haolloyin.blog.51cto.com/1177454/354320)