前言:
最近由于实现web商城的自动发送邮件功能的需求,故涉猎的邮箱协议的内部原理。现将简单版的Java Mail实例做个代码展示,并附上其中可能出现的bug贴出,方便感兴趣的读者进行测试!
1.载入相关jar包
客户端传输邮件需要用的jar包:activation.jar和mail.jar
(下载:https://github.com/SeaSky0606/share)
2.JavaMail代码实现
代码语言:javascript复制 1 package com.seasky.mail;
2
3 import java.util.Properties;
4
5 import javax.mail.Address;
6 import javax.mail.Message;
7 import javax.mail.MessagingException;
8 import javax.mail.Session;
9 import javax.mail.Transport;
10 import javax.mail.internet.InternetAddress;
11 import javax.mail.internet.MimeMessage;
12
13 public class Mail {
14
15 /**
16 * @param args
17 */
18 public static void main(String[] args) {
19
20 //属性设置,创建会话
21 Properties properties = new Properties();
22 properties.setProperty("mail.transport.protocol", "smtp");
23 Session session = Session.getInstance(properties);
24 session.setDebug(true); //设置debug模式
25 //创建Message对象
26 Message message = new MimeMessage(session);
27 try {
28 message.setSubject("我是主题");
29 message.setFrom(new InternetAddress("soft03_test@sina.com"));
30 message.setContent("<h2>我是内容</h2>", "text/html;charset=utf-8");
31
32 //创建接口,并连接(登录)
33 Transport transport = session.getTransport();
34 transport.connect("smtp.sina.com", "soft03_test", "soft03_test");
35 //发送邮件
36 transport.sendMessage(message, new Address[]{new InternetAddress("123456789@qq.com"),
37 new InternetAddress("soft03_test@sina.com")});
38 } catch (MessagingException e) {
39 e.printStackTrace();
40 }finally{
41 System.out.println("---finish--");
42 }
43
44 }
45
46 }
登录测试邮箱查看:
3.Debug模式下打印出smtp连接建立的过程
代码语言:javascript复制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)!