Created
June 14, 2016 12:02
-
-
Save 0532/5d7b25480e44ce5993c7f9b39a65eab0 to your computer and use it in GitHub Desktop.
send emails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.macrosky.dnd.controller.smails; | |
import java.util.Properties; | |
import java.util.ResourceBundle; | |
import javax.mail.BodyPart; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Multipart; | |
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; | |
import org.apache.log4j.Logger; | |
/** | |
* Created By WangLichao On 2016年6月14日. | |
*/ | |
public class SendMailsUtils { | |
private static Logger logger = Logger.getLogger(SendMailsUtils.class); | |
private static final ResourceBundle BUNDLE = ResourceBundle | |
.getBundle("config"); | |
private static final String fromEmail; | |
private static final String mailPasswd; | |
private static final String toEmails; | |
private static final String copyToEmails; | |
private static final String mailHost; | |
static { | |
mailHost = BUNDLE.getString("mail_host"); | |
fromEmail = BUNDLE.getString("from_email"); | |
mailPasswd = BUNDLE.getString("mail_passwd"); | |
toEmails = BUNDLE.getString("to_emails"); | |
copyToEmails = BUNDLE.getString("copyTo_emails"); | |
} | |
public static boolean doSend(String to, String copyto, String subject, | |
String content) { | |
try { | |
Properties props = System.getProperties(); // 系统属性 | |
props.put("mail.smtp.auth", "true"); | |
props.put("mail.smtp.host", mailHost); | |
props.put("username", fromEmail); | |
props.put("password", mailPasswd); | |
Session session = Session.getDefaultInstance(props, null);// 邮件会话对象 | |
session.setDebug(true); | |
MimeMessage mimeMsg = new MimeMessage(session);// MIME邮件对象 | |
Multipart mp = new MimeMultipart();// Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 | |
// 设置发信人 | |
mimeMsg.setFrom(new InternetAddress(fromEmail)); | |
// 设置接收人 | |
to = "".equals(to) ? toEmails : to; | |
mimeMsg.setRecipients(Message.RecipientType.TO, | |
InternetAddress.parse(to)); | |
// 设置抄送人 | |
copyto = "".equals(copyto) ? copyToEmails : copyto; | |
if (!"".equals(copyto)) { | |
mimeMsg.setRecipients(Message.RecipientType.CC, | |
InternetAddress.parse(copyto)); | |
} | |
// 设置主题 | |
mimeMsg.setSubject(subject); | |
// 设置正文 | |
BodyPart bp = new MimeBodyPart(); | |
bp.setContent(content, "text/html;charset=utf-8"); | |
mp.addBodyPart(bp); | |
mimeMsg.setContent(mp); | |
mimeMsg.saveChanges(); | |
// 发送邮件 | |
if (props.get("mail.smtp.auth").equals("true")) { | |
Transport transport = session.getTransport("smtp"); | |
transport.connect((String) props.get("mail.smtp.host"), | |
(String) props.get("username"), | |
(String) props.get("password")); | |
if (mimeMsg.getRecipients(Message.RecipientType.TO) != null) { | |
transport.sendMessage(mimeMsg, | |
mimeMsg.getRecipients(Message.RecipientType.TO)); | |
} | |
if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) { | |
transport.sendMessage(mimeMsg, | |
mimeMsg.getRecipients(Message.RecipientType.CC)); | |
} | |
transport.close(); | |
logger.info("send success!"); | |
} else { | |
Transport.send(mimeMsg); | |
} | |
logger.info("邮件发送成功!"); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} | |
return true; | |
} | |
// public static void main(String[] args) { | |
// doSend("", "", "汉字", "汉字12ee"); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment