为了净化网络大环境,请不要利用这个方法发送垃圾邮件,敬请合理合法使用!
我们在用邮箱注册一个软件的时候,通常它都会给我的邮箱发送个激活确认的邮件。这个一般情况下都是服务器端来进行处理的,但是突然间想知道Android客户端应该会有方法同样实现这个功能——利用某个特定邮箱账号向用户自动发送邮件!Android系统有自带的Email进行发送邮件,但这种会展示给用户,我们这里讲的是默认不显示给用户,也就是"偷偷"的发送!
我们需要用到三个jar包 android_mail.jar 。
首先,我们先定义一个邮件的bean类
public class MailBean implements Serializable {
//发送人
private String sender;
// 收件人
private List<String> recipientsTO;
// 抄送人
private List<String> recipientsCC;
// 密送人
private List<String> recipientsBCC;
//主题
private String subject;
// 正文
private String body;
// 附件列表
private List<String> attachments;
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public List<String> getRecipientsTO() {
return recipientsTO;
}
public void setRecipientsTO(List<String> recipientsTO) {
this.recipientsTO = recipientsTO;
}
public List<String> getRecipientsCC() {
return recipientsCC;
}
public void setRecipientsCC(List<String> recipientsCC) {
this.recipientsCC = recipientsCC;
}
public List<String> getRecipientsBCC() {
return recipientsBCC;
}
public void setRecipientsBCC(List<String> recipientsBCC) {
this.recipientsBCC = recipientsBCC;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public List<String> getAttachments() {
return attachments;
}
public void setAttachments(List<String> attachments) {
this.attachments = attachments;
}
@Override
public String toString() {
return "MailBean{" +
"sender='" + sender + '\'' +
", recipientsTO=" + recipientsTO +
", recipientsCC=" + recipientsCC +
", recipientsBCC=" + recipientsBCC +
", subject='" + subject + '\'' +
", body='" + body + '\'' +
", attachments=" + attachments +
'}';
}
}
接下来就是最重要的发送邮件的工具类:
配置发送邮件账户的属性:我这里用的是126邮箱,使用gmail等其他邮箱smtp的配置都失效了,暂时还未找到解决的方法,如谁有解决方法,还请评论告知!
private final static Session session;
private final static String user = "*****@126.com";//你的邮箱账号
private final static String password = "******";//你的邮箱密码
static {
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.126.com");//邮箱的host地址,基本都是smtp.****.com
properties.put("mail.smtp.port", "25");//邮箱smtp端口号
properties.put("mail.smtp.auth", "true");//开启smtp验证
properties.put("mail.debug", "false");
session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
}
接下载是地址字符串构造成网络地址对象的方法,这里考虑到可能会发给多个账号等。
private static InternetAddress[] listToAddresses(List<String> list) {
if (list != null && !list.isEmpty()) {
int size = list.size();
InternetAddress[] addresses = new InternetAddress[size];
try {
for (int i = 0; i < size; i++) {
addresses[i] = new InternetAddress(list.get(i));
}
return addresses;
} catch (AddressException ex) {
ex.printStackTrace();
}
}
return null;
}
执行发送邮件的方法:
public static void sendMail(MailBean mail) {
try {
MimeMessage msg = new MimeMessage(session);
//设置邮件发件人
msg.setFrom(new InternetAddress(user));
//设置邮件收件人
msg.setRecipients(Message.RecipientType.TO, listToAddresses(mail.getRecipientsTO()));
//设置邮件抄送人
msg.setRecipients(Message.RecipientType.CC, listToAddresses(mail.getRecipientsCC()));
//设置邮件密送人
msg.setRecipients(Message.RecipientType.BCC, listToAddresses(mail.getRecipientsBCC()));
//设置邮件主题
msg.setSubject(mail.getSubject(), "utf-8");
//设置邮件正文
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(mail.getBody(), "text/html; charset=utf-8");
mp.addBodyPart(mbp);
//设置邮件附件
List<String> attachments = mail.getAttachments();
if (attachments != null && !attachments.isEmpty()) {
for (String attachment : attachments) {
MimeBodyPart attach = new MimeBodyPart();
attach.attachFile(attachment);
mp.addBodyPart(attach);
}
}
msg.setContent(mp);
//设置发送时间
msg.setSentDate(new Date());
//发送
Transport.send(msg);
} catch (MessagingException | IOException ex) {
ex.printStackTrace();
}
}
完整的代码:
public class MailUtil {
private final static Session session;
private final static String user = "*****@126.com";//你的邮箱账号
private final static String password = "******";//你的邮箱密码
static {
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.126.com");//邮箱的host地址,基本都是smtp.****.com
properties.put("mail.smtp.port", "25");//邮箱smtp端口号
properties.put("mail.smtp.auth", "true");//开启smtp验证
properties.put("mail.debug", "false");
session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
}
public static void sendMail(MailBean mail) {
try {
MimeMessage msg = new MimeMessage(session);
//设置邮件发件人
msg.setFrom(new InternetAddress(user));
//设置邮件收件人
msg.setRecipients(Message.RecipientType.TO, listToAddresses(mail.getRecipientsTO()));
//设置邮件抄送人
msg.setRecipients(Message.RecipientType.CC, listToAddresses(mail.getRecipientsCC()));
//设置邮件密送人
msg.setRecipients(Message.RecipientType.BCC, listToAddresses(mail.getRecipientsBCC()));
//设置邮件主题
msg.setSubject(mail.getSubject(), "utf-8");
//设置邮件正文
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(mail.getBody(), "text/html; charset=utf-8");
mp.addBodyPart(mbp);
//设置邮件附件
List<String> attachments = mail.getAttachments();
if (attachments != null && !attachments.isEmpty()) {
for (String attachment : attachments) {
MimeBodyPart attach = new MimeBodyPart();
attach.attachFile(attachment);
mp.addBodyPart(attach);
}
}
msg.setContent(mp);
//设置发送时间
msg.setSentDate(new Date());
//发送
Transport.send(msg);
} catch (MessagingException | IOException ex) {
ex.printStackTrace();
}
}
/**
* 把list里的地址字符串构造成网络地址对象的数组
*/
private static InternetAddress[] listToAddresses(List<String> list) {
if (list == null || list.isEmpty()) return null;
int size = list.size();
InternetAddress[] addresses = new InternetAddress[size];
try {
for (int i = 0; i < size; i++) {
addresses[i] = new InternetAddress(list.get(i));
}
return addresses;
} catch (AddressException ex) {
ex.printStackTrace();
}
return null;
}
}