554 DT:SPM 163 smtp5,D9GowAD3RPYqSvxZjpMaAA--.4817S2 1509706293 坑爹的防垃圾邮件机制

代码如下

 

 

package ssmtest;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.junit.Test;

public class mailTest {

@Test
public void sendmail() {

Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 开启发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.163.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");

// 设置环境信息
Session session = Session.getInstance(props);

// 创建邮件对象
Message msg = new MimeMessage(session);
try {

// 邮件主题
msg.setSubject("邮件");

// 设置邮件内容

// 设置发件人
try {
msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("赵睿智") + "\"<15142538049@163.com>"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
msg.setFrom(new InternetAddress("15142538049@163.com"));
msg.setDescription("");
msg.setSubject("");

// 设置发送时间
msg.setSentDate(new Date());

// 整封邮件的MINE消息体
MimeMultipart msgMultipart = new MimeMultipart("mixed");// 混合的组合关系
// 设置邮件的MINE消息体
msg.setContent(msgMultipart);

// 开始附件处理
// 附件1
MimeBodyPart attch1 = new MimeBodyPart();
// 附件2
MimeBodyPart attch2 = new MimeBodyPart();
// 正文内容
MimeBodyPart content = new MimeBodyPart();

// 把内容,附件1,附件2加入到 MINE消息体中

msgMultipart.addBodyPart(attch1);
msgMultipart.addBodyPart(attch2);
msgMultipart.addBodyPart(content);

DataSource ds1 = new FileDataSource(new File("C:/Users/TRS_ZHZ/Desktop/a.txt"));
// 数据处理器
DataHandler dh1 = new DataHandler(ds1);
// 设置第一个附件的数据
attch1.setDataHandler(dh1);
// 设置第一个附件的文件名
attch1.setFileName("file1.txt");

DataSource ds2 = new FileDataSource(new File("C:/Users/TRS_ZHZ/Desktop/timg.jpg"));
DataHandler dh2 = new DataHandler(ds2);
attch2.setDataHandler(dh2);
try {
attch2.setFileName(MimeUtility.encodeText("文件2.jpg"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// 正文(图片和文字部分)
MimeMultipart bodyMultipart = new MimeMultipart("related");
// 设置内容为正文
content.setContent(bodyMultipart);

// html代码部分
MimeBodyPart htmlPart = new MimeBodyPart();
// html中嵌套的图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
bodyMultipart.addBodyPart(htmlPart);
bodyMultipart.addBodyPart(jpgBody);
// 添加正文图片
FileDataSource fds = new FileDataSource("C:/Users/TRS_ZHZ/Desktop/timg.jpg");
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("logo_jpg");
// 添加正文html代码
htmlPart.setContent("<span style='color:red'>hello world</span>" + "<img src= \"cid:logo_jpg\">",
"text/html;charset=utf-8");

msg.saveChanges();

Transport transport = session.getTransport();
// 连接邮件服务器
transport.connect(" 此处为账户", "此处为设置的密码");
// 发送邮件
//946869226@qq.com
transport.sendMessage(msg, new Address[] { new InternetAddress("946869226@qq.com") });
// 关闭连接
transport.close();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

错误信息:

com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp3,DdGowABnLuvkUPxZlqkSAA--.416S2 1509708015,please see http://mail.163.com/help/help_spam_16.htm?ip=114.243.25.188&hostid=smtp3&time=1509708015

 大家有没有什么好的规避的办法?

posted @ 2017-11-03 19:31  暗夜飞羽睿  阅读(656)  评论(0编辑  收藏  举报