Android邮箱可用于app将错误的消息发送到开发者的邮箱
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailManager {
private static String mailServerHos="smtp.126.com";
private static String mailServerPort = "25";
// 邮件发送者的地址
private static String fromAddress="shui501@126.com";
// 邮件接收者的地址
private static String toAddress="632095376@qq.com";
// 登陆邮件发送服务器的用户名和密码
private static String userName="shui501@126.com";
private static String password="*******";
// 是否需要身份验证
private static boolean validate = true;
// 邮件主题
private static String subject="主题";
public static boolean sendmail(String errorMsg) {
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Properties props = new Properties();
props.put("mail.smtp.host", mailServerHos);
props.put("mail.smtp.port", mailServerPort);
props.put("mail.smtp.auth", "true");
props.put("mail.user", userName);
props.put("mail.password", password);
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
return new PasswordAuthentication(userName, password);
}
};
Session sendMailSession = Session.getDefaultInstance(props,authenticator);
try
{
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(fromAddress);
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(toAddress);
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(subject);
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = errorMsg;
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
return false;
}
}