利用Java Mail发送邮件的代码
2018-07-20 来源:open-open
import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import org.apache.commons.lang.StringUtils; public class MailUtil { private MimeMessage message; private Session session; private Transport transport; private String smtpHost = ""; private int smtpPort = 25; private String senderUserName = ""; private String senderPassword = ""; public MailUtil(Properties properties, boolean debug) { this.smtpHost = properties.getProperty("mail.smtp.host"); this.smtpPort = Integer.parseInt(properties.getProperty("mail.smtp.port")); this.senderUserName = properties.getProperty("mail.sender.username"); this.senderPassword = properties.getProperty("mail.sender.password"); session = Session.getInstance(properties); session.setDebug(debug);//开启后有调试信息 message = new MimeMessage(session); } /** * 发送邮件 * * @param subject 邮件主题 * @param mailBody 邮件内容 * @param senderNickName 发件人NickName * @param receiveUser 收件人地址 * @param ccReceiveUser 抄送地址 * @param bccReceiveUser 密送地址 */ public void sendEmail(String subject, String mailBody, String senderNickName, String receiveUser, String ccReceiveUser, String bccReceiveUser, Boolean isHtmlFormat) { try { // 发件人 InternetAddress from = null; if (StringUtils.isEmpty(senderNickName)) { from = new InternetAddress(senderUserName); } else { from = new InternetAddress(MimeUtility.encodeWord(senderNickName) + " <" + senderUserName + ">"); } message.setFrom(from); // 收件人 InternetAddress to = new InternetAddress(receiveUser); message.setRecipient(Message.RecipientType.TO, to); //抄送人 if (!StringUtils.isEmpty(ccReceiveUser)) { InternetAddress cc = new InternetAddress(ccReceiveUser); message.setRecipient(Message.RecipientType.CC, cc); } //密送人 if (!StringUtils.isEmpty(bccReceiveUser)) { InternetAddress bcc = new InternetAddress(bccReceiveUser); message.setRecipient(Message.RecipientType.BCC, bcc); } message.setSubject(subject); String content = mailBody.toString(); if (isHtmlFormat) { message.setContent(content, "text/html;charset=UTF-8"); } else { message.setContent(content, "text/plain;charset=UTF-8"); } message.saveChanges(); transport = session.getTransport("smtp"); transport.connect(smtpHost, smtpPort, senderUserName, senderPassword); transport.sendMessage(message, message.getAllRecipients()); } catch (Exception e) { e.printStackTrace(); } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } } } public static void main(String[] args) { Properties properties = new Properties(); properties.setProperty("mail.smtp.host", "smtp.sina.com"); properties.setProperty("mail.smtp.port", "25"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.sender.username", "XXXXXXXXX"); properties.setProperty("mail.sender.password", "XXXXXXXXX"); MailUtil mailUtil = new MailUtil(properties, false); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:python递归删除指定目录
下一篇:断点下载文件Java代码
最新资讯
热门推荐