public class Mail {
//发件人邮箱和密码
static String myEmail="xxxxx@126.com";
static String myEmailpassword="xxxx";//改成了授权码
//发件人邮箱smtp服务器
static String EmailSMTPHost="smtp.126.com";
//收件人邮箱
static String sendEmail="xxxx@qq.com";
public static void main(String[] args)throws Exception{
//1.连接邮件服务器的参数配置
Properties props=new Properties();
props.setProperty("mail.transport.protocol","smtp");//协议
props.setProperty("mail.stmp.host", EmailSMTPHost);//smtp服务器地址
props.setProperty("mail.stmp.auth","true");//需要请求认证
/**
* ssl安全连接认证
*/
/*
final String smtpPort="465";//根据不同邮箱改写,qq邮箱的smtp(ssl)端口为465或587
props.setProperty("mail.smtp.port",smtpPort);
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
*/
//2.创建回话对象和邮箱服务器交互
Session session=Session.getInstance(props);
session.setDebug(true);//设置debug模式,可查看发送log
//3.创建一封邮件
MimeMessage message=createMimeMessage(session,myEmail,sendEmail);
//4.根据session获取邮件传输对象
Transport transport=session.getTransport();
//5.使用邮箱和密码连接邮件服务器
transport.connect(EmailSMTPHost,myEmail,myEmailpassword);
//6.发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message,message.getAllRecipients());
//7.关闭连接
transport.close();
}
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {
//1.创建邮件
MimeMessage message=new MimeMessage(session);
//2.Form:发件人
message.setFrom(new InternetAddress(sendMail,""));
//3.TO:收件人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail,"用户","UTF-8"));
/*
// Cc: 抄送(可选)
message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("ee@receive.com", "USER_EE", "UTF-8"));
// Bcc: 密送(可选)
message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress("ff@receive.com", "USER_FF", "UTF-8"));
*/
//4.Subject:邮件主题
message.setSubject("洞洞拐","UTF-8");
//5.正文
message.setContent("祁总你好,准备跑路吧","text/html;charset=UTF-8");
//6.设置发件时间
message.setSentDate(new Date());
//7.保存设置
message.saveChanges();
return message;
}
}
碰到问题:
—需要关闭SSL认证
DEBUG SMTP: nested IOException, closing
Exception in thread “main” javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2204)
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1690)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at mail.Mail.main(Mail.java:58)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2184)
… 3 more
----需要开启授权码
javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
—STMP未开启
Exception in thread “main” javax.mail.AuthenticationFailedException: 550 Óû§ÎÞȨµÇ½
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at mail.Mail.main(Mail.java:55)
```
复杂邮件链接: http://blog.csdn.net/xietansheng/article/details/51722660