JavaMail for Android
2018-06-14 本文已影响171人
Il_mondo
compile 'com.sun.mail:android-mail:1.6.0'
// compile 'com.sun.mail:android-activation:1.6.0'
public static void javaMailSendSimpleEmail() throws Exception {
// 收件人电子邮箱
String to = "*@qq.com";
// 发件人电子邮箱
String from = "*@163.com";
// 获取系统属性
Properties properties = new Properties();
// 设置邮件服务器
properties.setProperty("mail.transport.protocol", "SMTP");
properties.setProperty("mail.smtp.host", "smtp.163.com");
properties.setProperty("mail.smtp.port", "25");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.timeout", "1000");
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 登陆邮件发送服务器的用户名和密码
return new PasswordAuthentication("*@163.com", "***");
}
});
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("邮件标题");
// 设置消息体
message.setText("邮件内容");
// 发送消息
Transport.send(message);
}
发送带复件的邮件
public static void sendEmail(File file) throws Exception {
// 收件人电子邮箱
String to = "*@qq.com";
// 发件人电子邮箱
String from = "*@163.com";
// 获取系统属性
Properties properties = new Properties();
// 设置邮件服务器
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", "smtp.163.com");
properties.setProperty("mail.smtp.port", "25");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.timeout", "1000");
properties.put("mail.debug", true);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 登陆邮件发送服务器的用户名和密码
return new PasswordAuthentication(
"*", "*");
}
});
MimeBodyPart text = new MimeBodyPart();
text.setContent("<h4>你猜我在做什么呢?</h4>", "text/html;charset=UTF-8");
//创建邮件附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(file));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
InternetAddress address = new InternetAddress(from);
message.setFrom(address);
message.addRecipient(Message.RecipientType.CC, address);
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//创建容器描述数据关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setSubject("做什么.");
message.setContent(mp);
message.saveChanges();
Transport transport = session.getTransport();
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
防坑:
- 以HTML形式发送text, 防止被当成垃圾文件.
MimeBodyPart text = new MimeBodyPart();
text.setContent("<h4>你猜我在做什么呢?</h4>", "text/html;charset=UTF-8");
- 添加抄送 降低163 [554 DT:SPM]错误
// 添加抄送
InternetAddress address = new InternetAddress(from);
message.setFrom(address);
message.addRecipient(Message.RecipientType.CC, address);
// 不使用 Transport.send(message);
Transport transport = session.getTransport();
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
参考文章
[1]路易斯睿齐·Android 使用 JavaMail 发送邮件 实践总结
[2]Oracle·JavaMail[M]