首先封装邮件实体,协议采用SMTP:
import java.util.List; import javax.mail.internet.InternetAddress; /** * 邮箱实体类 * @author Administrator * */ public class EmailInfo { // 邮箱对应SMTP的服务器的地址 private String smtpHost; // 邮箱的用户名 private String user; private String password; // 邮件主题 private String subject; // 邮件内容(可以在字符串中插入html标记) private String content; // 邮件内容的编码(默认可为空) private String charset = "text/html;charset=UTF-8"; // 接收邮件的对象的邮箱地址 private List<InternetAddress> receiveEmailAddress; // 抄送邮件的对象的地址 private List<InternetAddress> ccEmailAddress; public EmailInfo() { super(); } public EmailInfo(String smtpHost, String user, String password, String subject, String content, List<InternetAddress> receiveEmailAddress) { super(); this.smtpHost = smtpHost; this.user = user; this.password = password; this.subject = subject; this.content = content; this.receiveEmailAddress = receiveEmailAddress; } public String getSmtpHost() { return smtpHost; } public void setSmtpHost(String smtpHost) { this.smtpHost = smtpHost; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public List<InternetAddress> getReceiveEmailAddress() { return receiveEmailAddress; } public void setReceiveEmailAddress(List<InternetAddress> receiveEmailAddress) { this.receiveEmailAddress = receiveEmailAddress; } public String getCharset() { return charset; } public void setCharset(String charset) { this.charset = charset; } public List<InternetAddress> getCcEmailAddress() { return ccEmailAddress; } public void setCcEmailAddress(List<InternetAddress> ccEmailAddress) { this.ccEmailAddress = ccEmailAddress; } }
然后封装工具方法类:
import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; import com.safety.pub.common.Common; import edu.emory.mathcs.backport.java.util.Arrays; /** * 邮件操作公共类 * @author Administrator * */ public class EmailUtils { public static Logger log = Logger.getLogger(EmailUtils.class); /** * 发送邮件 * @param email * @throws MessagingException */ public static void sendEmail(EmailInfo email) throws MessagingException { final Properties props = generateProp(email); Session mailSession = generateSession(props); MimeMessage message = new MimeMessage(mailSession); InternetAddress from = new InternetAddress(props.getProperty("mail.user")); message.setFrom(from); List<InternetAddress> list = email.getReceiveEmailAddress(); if(!Common.isEmpty(list)) { InternetAddress[] to = list.toArray(new InternetAddress[]{}); log.debug("发送邮件的地址为:" + Arrays.toString(to)); message.setRecipients(RecipientType.TO, to); List<InternetAddress> ccList = email.getCcEmailAddress(); if(!Common.isEmpty(ccList)) { InternetAddress[] cc = ccList.toArray(new InternetAddress[]{}); log.debug("抄送邮件的地址为:" + Arrays.toString(cc)); message.setRecipients(RecipientType.CC, cc); } log.debug("发送邮件的主题为:" + email.getSubject()); message.setSubject(email.getSubject()); log.debug("发送邮件的内容体为:" + email.getContent()); message.setContent(email.getContent(), email.getCharset()); Transport.send(message); } } private static Session generateSession(final Properties props) { return Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }); } private static Properties generateProp(EmailInfo email) { final Properties prop = new Properties(); // 表示SMTP发送邮件,需要进行身份验证 prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.host", email.getSmtpHost()); prop.put("mail.user", email.getUser()); prop.put("mail.password", email.getPassword()); return prop; } //测试是否能够成功发送 public static void main(String[] args) throws MessagingException { String smtpHost="smtp.163.com"; String user="[email protected]"; String password="chnashxbjkbnzxqg"; String subject="测试111"; String content="测试发送邮件"; InternetAddress to = new InternetAddress("[email protected]"); ArrayList<InternetAddress> receiveEmailAddress=new ArrayList<InternetAddress> (); receiveEmailAddress.add(to); EmailInfo email =new EmailInfo(smtpHost,user,password,subject,content,receiveEmailAddress); sendEmail(email); } }
时间: 2024-11-04 01:52:48