工具类-发送邮件(通过JavaMail发送)
Posted juihai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工具类-发送邮件(通过JavaMail发送)相关的知识,希望对你有一定的参考价值。
前段时间在工作中用到了邮件发送监控的报警信息,今天在这个记录一下JavaMail的邮件工具类。
下边为用到的JavaMail的jar包的pom依赖。这里用的是JavaMail的1.4.6的版本。
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.6</version> </dependency>
这里是工具类中的用到的邮件的一些参数对象的实体类。
package com.juihai.entity; import java.util.Arrays; public class EmailTO { private String title;//邮件主题 private String content;//邮件内容 private String sender;//发件人账号 private String password;//发件人密码 private String host;//发件邮箱服务器 private String[] toAdress;//收件人地址 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String[] getToAdress() { return toAdress; } public void setToAdress(String[] toAdress) { this.toAdress = toAdress; } @Override public String toString() { return "EmailTO [title=" + title + ", content=" + content + ", sender=" + sender + ", password=" + password + ", host=" + host + ", toAdress=" + Arrays.toString(toAdress) + "]"; } }
这里是就是发送Email的工具类。具体说明件代码中的注释。
import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.util.Properties; 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; import org.apache.log4j.Logger; import com.juihai.entity.EmailTO; public class SendEmailUtils { /** 日志输出 */ private static Logger log = Logger.getLogger(SendEmailUtils.class); public static boolean send(EmailTO to) throws GeneralSecurityException{ final String user = to.getSender(); final String password = to.getPassword(); log.info("SendEmailUtils >>>> Start Send Mail"); Properties properties = System.getProperties();// 获取系统属性 properties.setProperty("mail.smtp.host", to.getHost());// 设置邮件服务器 "smtp.163.com",这是发件人的邮箱服务器 properties.put("mail.smtp.auth", "true"); String senderNick = null;
//部分邮箱服务器需要开启SSL验证才可以发送邮件,需要的话,开启注释即可 /*MailSSLSocketFactory sf = new MailSSLSocketFactory();// SSL验证 begin sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf);// SSL验证 end*/ Session session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); // 发件人邮件用户名、密码 } }); try { MimeMessage message = new MimeMessage(session);// 创建默认的 MimeMessage对象 senderNick = javax.mail.internet.MimeUtility.encodeText("Juihai");//设置发件人昵称 message.setFrom(new InternetAddress(senderNick+"<"+user+">")); //message.addRecipient(Message.RecipientType.TO,new InternetAddress());//创建单个收件人 String[] tos = to.getToAdress();//创建多个收件人 if (tos != null && tos.length != 0) { InternetAddress[] toAddress = new InternetAddress[tos.length]; for (int i = 0; i < tos.length; i++) { toAddress[i] = new InternetAddress(tos[i]); } message.setRecipients(Message.RecipientType.TO, toAddress); } message.setSubject(to.getTitle());//设置邮件主题 //message.setText(content);//发送纯文本内容 message.setContent(to.getContent(), "text/html;charset=utf-8");//发送html邮件内容 Transport.send(message);//发送Email log.info(" SendEmailUtils邮件发送成功"); return true; }catch (MessagingException mex) { mex.printStackTrace(); log.info(" >>>>SendEmailUtils邮件发送失败"+mex); } catch (UnsupportedEncodingException e) { log.info(" >>>>SendEmailUtils邮件发送失败-设置发件人昵称error"+e); e.printStackTrace(); } return false; } }
以上是关于工具类-发送邮件(通过JavaMail发送)的主要内容,如果未能解决你的问题,请参考以下文章
Java编程系列使用JavaMail通过SMTP协议发送局域网(内网)邮件