最近在学习JavaWeb,写到一个用javaMail发送邮件的程序,邮件发送成功了,但是我发现接收到的邮件除了我发送的信息外还会带上不知来源的冗余信息(如下图的红色字体上方的黑色字体),我想知道在发送邮件时如何去除:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class MailDemo01 {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties prop=new Properties();
prop.setProperty("mail.host","smtp.qq.com");//设置QQ邮件服务器
prop.setProperty("mail.transport.protocol","smtp");//邮件发送协议
prop.setProperty("mail.smtp.auth","true");//需要通过验证用户名和密码
//使用JavaMail发送邮件的五个步骤
//1.创建定义整个应用程序所需环境信息的Session对象
//QQ使用,其他邮箱不用
Session session=Session.getDefaultInstance(prop, new Authenticator() {
//发件人的邮件用户名和授权码
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("QQ邮箱","QQ邮箱授权码");
}
});
//开启Session的debug模式,这样就可以看到程序发送Email的运行状态了
session.setDebug(true);
//2.通过Session得到transport对象
Transport transport=session.getTransport();
System.out.println("transport对象创建成功!");
//3.使用邮箱的用户名和授权码连上邮件服务器
transport.connect("smtp.qq.com","QQ邮箱","QQ邮箱授权码");
System.out.println("QQ邮箱服务器连接成功!");
//4.创建邮件
//注意需要传递session
MimeMessage message=new MimeMessage(session);
System.out.println("邮件信息初始化-------");
message.setFrom(new InternetAddress("QQ邮箱"));//指明邮件的发件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress("QQ邮箱"));//指明收件人
message.setSubject("Hello,javaMail!");//指明邮件标题
//指明邮件内容,可使用html来改变想要呈现的样式
message.setText("<h1 style='color:red'>Hello,I'm learning javaMail!</h1>","UTF-8");
System.out.println("邮件内容设置成功!");
//5.发送邮件
transport.sendMessage(message,message.getAllRecipients());
System.out.println("邮件发送成功!");
//6.关闭连接
transport.close();
}
}
可以用Spring的Thymeleaf自己画一个邮件模版,然后在代码使用创建的模版,就可避免以上问题。
网上查了一下例子,根据这篇文章 Java Mail(二):JavaMail介绍及发送一封简单邮件 的示例代码。
Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.qq.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
//邮件内容部分
Message msg = new MimeMessage(session);
msg.setSubject("seenews 错误");
StringBuilder builder = new StringBuilder();
builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");
builder.append("页面爬虫错误");
builder.append("\n data " + TimeTool.getCurrentTime());
msg.setText(builder.toString());
//邮件发送者
msg.setFrom(new InternetAddress("**发送人的邮箱地址**"));
//发送邮件
Transport transport = session.getTransport();
transport.connect("smtp.qq.com", "**发送人的邮箱地址**", "**你的邮箱密码或者授权码**");
transport.sendMessage(msg, new Address[] { new InternetAddress("**接收人的邮箱地址**") });
transport.close();
但是报错了
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
Exception in thread "main" javax.mail.AuthenticationFailedException: 530
Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
因为示例代码是用的163邮箱,而笔者是 QQ 邮箱,看 Log 分析是 QQ 邮箱需要 SSL 加密。