前言:
最近由于实现web商城的自动发送邮件功能的需求,故涉猎的邮箱协议的内部原理。现将简单版的Java Mail实例做个代码展示,并附上其中可能出现的bug贴出,方便感兴趣的读者进行测试!
1.载入相关jar包
客户端传输邮件需要用的jar包:activation.jar和mail.jar
(下载:https://github.com/SeaSky0606/share)
2.JavaMail代码实现
package com.seasky.mail; import java.util.Properties; import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; public class Mail { /**
* @param args
*/
public static void main(String[] args) { //属性设置,创建会话
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(properties);
session.setDebug(true); //设置debug模式
//创建Message对象
Message message = new MimeMessage(session);
try {
message.setSubject("我是主题");
message.setFrom(new InternetAddress("soft03_test@sina.com"));
message.setContent("<h2>我是内容</h2>", "text/html;charset=utf-8"); //创建接口,并连接(登录)
Transport transport = session.getTransport();
transport.connect("smtp.sina.com", "soft03_test", "soft03_test");
//发送邮件
transport.sendMessage(message, new Address[]{new InternetAddress("123456789@qq.com"),
new InternetAddress("soft03_test@sina.com")});
} catch (MessagingException e) {
e.printStackTrace();
}finally{
System.out.println("---finish--");
} } }
登录测试邮箱查看:
3.Debug模式下打印出smtp连接建立的过程
DEBUG: setDebug: JavaMail version 1.4.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.sina.com", port 25, isSSL false
220 smtp-2-33.smtpsmail.fmail.yf.sinanode.com ESMTP
DEBUG SMTP: connected to host "smtp.sina.com", port: 25 EHLO CJH
250-smtp-2-33.smtpsmail.fmail.yf.sinanode.com
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
c29mdDAzX3Rlc3Q=
334 UGFzc3dvcmQ6
c29mdDAzX3Rlc3Q=
235 OK Authenticated
DEBUG SMTP: use8bit false
MAIL FROM:<soft03_test@sina.com>
250 ok
RCPT TO:<123456789@qq.com>
250 ok
RCPT TO:<soft03_test@sina.com>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: 123456789@qq.com
DEBUG SMTP: soft03_test@sina.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: soft03_test@sina.com
Message-ID: <535071474.0.1446284102239.JavaMail.Administrator@CJH>
Subject: =?UTF-8?B?5oiR5piv5Li76aKY?=
MIME-Version: 1.0
Content-Type: text/html;charset=utf-8
Content-Transfer-Encoding: base64 PGgyPuaIkeaYr+WGheWuuTwvaDI+
.
250 ok queue id 2476333014883
---finish--
4.常见问题:
1.出现包冲突
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
没有找到相关的内部类错误,即说明存在 jar包冲突!
解决方法:
去除开发环境的内部javaEE包,或者删除其内部java.mail下面的所有类!
2.注册的实验邮箱测试失败
535 5.7.8 authentication failed
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
at javax.mail.Service.connect(Service.java:169)
at com.seasky.mail.JMailTest.main(JMailTest.java:35)
---finish--
解决方法:
由于实验测试邮箱可能需要开通vip,所以建议直接使用本样例中的新浪邮箱的账号密码(soft03_test/soft03_test)!