最近有个项目,要发邮件给用户设定的邮箱报警,然后就用了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,可以自行百度下载。

最新文章

  1. java servlet
  2. sqlmap http头注入的一个技巧
  3. js中查找一个字符是否存在。
  4. phpcms如何使用推荐位调用自定义字段
  5. 03 在Linux下安装Myeclipse及Tomcat(含下载)
  6. gcc -D
  7. Android:单元测试
  8. iBatis调用存储过程以及MySQL创建存储过程
  9. WPF开发的彩票程序(练手好例子) 附源码
  10. 01.阿里云SDK调用,获取ESC主机详细信息
  11. linux 文件属性(转)
  12. JS页面打印
  13. react事件绑定,事件传参,input单向数据绑定
  14. POI导出Excel 错误THE MAXIMUM COLUMN WIDTH FOR AN INDIVIDUAL CELL IS 255 CHARACTERS
  15. docker 容器和镜像理解
  16. java 框架 面试 2
  17. struts 类型转换
  18. c++11 继承构造
  19. xampp + windows 配置 memcache流程
  20. sql语句判断身份证性别等

热门文章

  1. Windows phone应用开发[17]-xap提交异常处理
  2. PHP连接数据库的方法
  3. [Linux编程]__read_mostly变量含义
  4. asp.net 页面如何将Eval中的时间显示为“yyyy-MM-dd ” 格式
  5. 【Composer】实战操作一:使用库
  6. [转] Android PhoneGap Cordova 体系结构
  7. 结合NGUI做的手机拍照(可自定义相框)
  8. 基于Struts2框架实现登录案例 之 程序国际化
  9. 用 JavaScript 修改样式元素
  10. 推荐vpn的文章
  11. 怎样在Android本地视频播放器开发
  12. UpdatePanel Repeater内LinkButton造成页面刷新问题
  13. SpringMVC全注解
  14. Structured Streaming + Kafka 集成中遇到的问题
  15. Oracle 10g RAC OCR、Voting disk更换
  16. Eclipse+Maven环境下java.lang.OutOfMemoryError: PermGen space及其解决方法
  17. Spring MVC 实现文件的上传和下载 (八)
  18. Ubuntu常用软件安装(小集合)
  19. 修改mongodb(带仲裁节点的副本集)各机器端口
  20. Kotlin入门(16)容器的遍历方式