作为工具类,这篇文章主要是记录一下如何配置邮件发送的username、password、
Authenticate、post、sendfrom、sendto等等详细的配置留作后用
public class EmailHelper {
static SimpleDateFormat mDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private static String mailName;// 发送者邮件账户名
/**
* 发送者邮件账户密码
* */
private static String mailPassword;
/**
* 接收者邮件账户名
* */
private static String SEND_TO_MAILLIST[];
private static String mailHost;
private static String mailProt;
private static String mailSProt;
/**
* @param user
* @param pw
* @param sendTo
*/
public static void setMailSetup(String user, String pw, String... sendTo) {
mailName = user;
mailPassword = pw;
SEND_TO_MAILLIST = sendTo;
}
/**
* @param _mailHost
* @param port
* @param sport
*/
public static void setMailSettings(String _mailHost, String port,
String sport) {
mailHost = _mailHost;
mailProt = port;
mailSProt = sport;
}
/**
* @param title
* @param body
*/
public synchronized static void sendMail(String title, String body) {
Mail mail = new Mail(mailName, mailPassword, SEND_TO_MAILLIST);
StringBuffer $title = new StringBuffer();
if (title == null) {
title = "";
}
$title.append(title).append("_").append("[ ");
$title.append(mDateFormat.format(new Date(System.currentTimeMillis())));
mail.setTitle($title.toString());
mail.setBody(body);
try {
mail.send();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param title
* @param t
*/
public synchronized static void sendMail(String title, Throwable t) {
Mail mail = new Mail(mailName, mailPassword, SEND_TO_MAILLIST);
StringBuffer $title = new StringBuffer();
if (title == null) {
title = "";
}
$title.append(title).append("_").append("[ ");
$title.append(mDateFormat.format(new Date(System.currentTimeMillis())));
mail.setTitle($title.toString());
StringBuffer body = new StringBuffer();
body.append(exceptionToString(t));
mail.setBody(body.toString());
try {
mail.send();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param context
* @param logUser
* @param title
* @param t
*/
public synchronized static void sendMail(Context context, String logUser,
String title, Throwable t) {
Mail mail = new Mail(mailName, mailPassword, SEND_TO_MAILLIST);
StringBuffer $title = new StringBuffer();
if (title == null) {
title = "";
}
$title.append(title).append("_").append("[ ");
$title.append(mDateFormat.format(new Date(System.currentTimeMillis())));
$title.append(" ]_");
mail.setTitle($title.toString());
StringBuffer body = new StringBuffer();
body.append("LOG_USER_NAME: ").append(logUser).append("\n");
body.append(exceptionToString(t));
mail.setBody(body.toString());
try {
mail.send();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param context
* @param logUser
* @param title
* @param logs
* @param t
*/
public synchronized static void sendMail(Context context, String logUser,
String title, String logs, Throwable t) {
Mail mail = new Mail(mailName, mailPassword, SEND_TO_MAILLIST);
StringBuffer $title = new StringBuffer();
if (title == null) {
title = "";
}
$title.append(title).append("_").append("[ ");
$title.append(mDateFormat.format(new Date(System.currentTimeMillis())));
$title.append(" ]_");
$title.append(logUser);
mail.setTitle($title.toString());
StringBuffer body = new StringBuffer();
body.append("LOG_USER_NAME: ").append(logUser).append("\n");
body.append(logs).append('\n');
body.append(exceptionToString(t));
mail.setBody(body.toString());
try {
mail.send();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param context
* @param logUser
* @param filename
*/
public synchronized static void sendMail(Context context, String logUser,
String filename) {
Mail mail = new Mail(mailName, mailPassword, SEND_TO_MAILLIST);
StringBuffer $title = new StringBuffer();
$title.append(logUser).append("_").append("[ ");
$title.append(mDateFormat.format(new Date(System.currentTimeMillis())));
$title.append(" ]_");
$title.append(logUser);
mail.setTitle($title.toString());
StringBuffer body = new StringBuffer();
body.append("LOG_USER_NAME: ").append(logUser).append("\n");
mail.setBody(body.toString());
try {
mail.addAttachment(filename);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
mail.send();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将Excepiton信息转换成String字符串.
*
* @param t
* @return
*/
public static String exceptionToString(Throwable throwable) {
if (throwable == null) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
throwable.printStackTrace(new PrintStream(baos));
} finally {
try {
baos.close();
} catch (Exception ex) {
ex.printStackTrace();
System.gc();
}
}
return baos.toString();
}
private static class Mail extends javax.mail.Authenticator {
private String _user;// 名
private String _pass;// 密
private String[] _to;// 送达
private String _from;// 我
private final String _port;
private final String _sport;
private final String _host;
private String _subject;
private String _body;
private final boolean _auth;
private final boolean _debuggable;
private final Multipart _multipart;
public Mail() {
_host = mailHost; // default smtp server
_port = mailProt; // default smtp port
_sport = mailSProt; // default socketfactory port
_user = ""; // username
_pass = ""; // password
_from = ""; // email sent from
_subject = ""; // email subject
_body = ""; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
if (mailName == null || mailPassword == null
|| SEND_TO_MAILLIST == null || mailHost == null
|| mailProt == null) {
throw new NullPointerException("EmailHelper 配置未初始化完成");
}
// There is something wrong with MailCap, javamail can not find a
// handler for the multipart/mixed part, so this bit needs to be
// added.
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);
}
public Mail(String user, String pass, String to[]) {
this();
_subject = "buglists";
_user = user;
_from = _user;
_pass = pass;
_to = to;
}
public void setTitle(String title) {
_subject = title;
}
public boolean send() throws Exception {
Properties props = _setProperties();
if (!_user.equals("") && !_pass.equals("") && _to.length > 0
&& !_from.equals("") && !_subject.equals("")
&& !_body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if (_debuggable) {
props.put("mail.debug", "true");
}
if (_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
public String getBody() {
return _body;
}
public void setBody(String _body) {
this._body = _body;
}
}
}