最近有个项目,要发邮件给用户设定的邮箱报警,然后就用了163邮箱,代码是网上借来的^^,如下:
package com.smartdoorbell.util;
import android.os.AsyncTask;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* Created by zhangjie on 15/8/31.
*/
public class JEmailHelper {
private int port = 25; // smtp协议使用的端口
private String host = "smtp.163.com"; // 发件人邮件服务器
private String user = "xxxxxxx@163.com"; // 使用者账号
private String password = "xxxxxxx"; // 使用者密码
private List<String> emailTos;
private List<String> emailCCs;
private String subject;
private String body;
private List<String> paths;
enum SendStatus {
SENDING, UNDO, SENDOK, SENDFAIL
};
private SendStatus sendStatus;
public interface JieEmailInfterface {
void startSend();
void SendStatus(SendStatus sendStatus);
}
private JieEmailInfterface jieEmailInfterface;
public void setJieEmailInfterface(JieEmailInfterface jieEmailInfterface) {
this.jieEmailInfterface = jieEmailInfterface;
}
public JEmailHelper() {
synchronized (JEmailHelper.class) {
sendStatus = SendStatus.UNDO;
}
}
// 构造发送邮件帐户的服务器,端口,帐户,密码
public JEmailHelper(String host, int port, String user, String password) {
synchronized (JEmailHelper.class) {
this.port = port;
this.user = user;
this.password = password;
this.host = host;
sendStatus = SendStatus.UNDO;
}
}
/**
* @param emailTos
* 主要接收人的电子邮箱列表
* @param emailCCs
* 抄送人的电子邮箱列表
* @param subject
* 邮件标题
* @param body
* 正文内容
* @param paths
* 发送的附件路径集合
*/
public void setParams(List<String> emailTos, List<String> emailCCs,
String subject, String body, List<String> paths) {
this.emailTos = emailTos;
this.emailCCs = emailCCs;
this.subject = subject;
this.body = body;
this.paths = paths;
}
public void sendEmail() {
new JieAsynTask().execute();
}
private void sendEmailBg() throws Exception {
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");// true一定要加引号
properties.put("mail.transport.protocol", "smtp");
JieAuthenticator jieAuth = new JieAuthenticator(user, password);
Session session = Session.getInstance(properties, jieAuth);
// 创建一个消息
MimeMessage msg = new MimeMessage(session);
// 设置发送人
msg.setFrom(new InternetAddress(user));
// 设置主要接收人
if (emailTos != null && !emailTos.isEmpty()) {
int size = emailTos.size();
InternetAddress[] addresses = new InternetAddress[size];
for (int i = 0; i < size; i++) {
addresses[i] = new InternetAddress(emailTos.get(i));
}
msg.setRecipients(Message.RecipientType.TO, addresses);
}
// 设置抄送人的电子邮件
if (emailCCs != null && !emailCCs.isEmpty()) {
int size = emailCCs.size();
InternetAddress[] addresses = new InternetAddress[size];
for (int i = 0; i < size; i++) {
addresses[i] = new InternetAddress(emailCCs.get(i));
}
msg.setRecipients(Message.RecipientType.CC, addresses);
}
msg.setSubject(subject);
// 创建一个消息体
MimeBodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setText(body);
// 创建Multipart增加其他的parts
Multipart mp = new MimeMultipart();
mp.addBodyPart(msgBodyPart);
// 创建文件附件
if (paths!=null){
for (String path : paths) {
if (path!=null){
MimeBodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.attachFile(path);
mp.addBodyPart(fileBodyPart);
}
}
}
// 增加Multipart到消息体中
msg.setContent(mp);
// 设置日期
msg.setSentDate(new Date());
// 设置附件格式
MailcapCommandMap mc = (MailcapCommandMap) CommandMap
.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
msg.saveChanges();
// 发送消息
Transport.send(msg);
}
class JieAuthenticator extends Authenticator {
private String strUser;
private String strPwd;
public JieAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
class JieAsynTask extends AsyncTask<Void, Void, SendStatus> {
@Override
protected void onPreExecute() {
super.onPreExecute();
if (jieEmailInfterface != null) {
jieEmailInfterface.startSend();
}
}
@Override
protected void onPostExecute(SendStatus result) {
super.onPostExecute(result);
if (jieEmailInfterface != null) {
jieEmailInfterface.SendStatus(result);
}
sendStatus = SendStatus.UNDO;
}
@Override
protected SendStatus doInBackground(Void... params) {
try {
sendStatus = SendStatus.SENDING;
sendEmailBg();
sendStatus = SendStatus.SENDOK;
} catch (Exception e) {
e.printStackTrace();
sendStatus = SendStatus.SENDFAIL;
}
return sendStatus;
}
}
}
然后,随便输入了个标题和内容测试,没有问题。于是,把报警信息整理好,写入标题和内容,杯具的事情发生了,一直无法发送成功,返回错误信息如下:
com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp11,D8CowACnIsnYuENXzwhNBw--.59036S2 1464056024,please see http://mail.163.com/help/help_spam_16.htm?ip=121.34.169.53&hostid=smtp11&time=1464056024
去http://mail.163.com/help/help_spam_16.htm?ip=121.34.169.53&hostid=smtp11&time=1464056024这个网址一看,是被作为垃圾邮件了!@ !
开始了换主题、换内容,换了N次后,才终于不被认为是垃圾邮件了,坑爹的,最啃爹的是,换了N都词语后都不行后,把“:”(汉语输入法)换成“:”(英文输入法),好了,这是什么规则?!!!
ps:上面的代码需要添加activation.jar、additionnal.jar、mail.jar,可以自行百度下载。