Java 实现邮件的发送
Posted 胖陀螺的春天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 实现邮件的发送相关的知识,希望对你有一定的参考价值。
Java 实现邮件的发送 |
/** * */ package com.email; /** * <p>Description: </p> * @author 朱开心 * @date 2018年6月6日 * */ import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * 服务器邮箱 */ public class MailAuthenticator extends Authenticator{ /** * 用户名 */ private String username; /** * 登录密码 */ private String password; /** * 继承Authenticator类需重写此方法 */ @Override protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username,password); } public MailAuthenticator(){ } public MailAuthenticator(String username, String password){ this.username = username; this.password = password; } public String getUsername() { return username; } public String getPassword() { return password; } } |
/** * */ package com.email; /** * <p>Description: </p> * @author 朱开心 * @date 2018年6月6日 * */ /** * 邮件对象 */ public class Mail { /** * 邮件标题 */ private String subject; /** * 邮件内容 */ private String content; public Mail(){ } public Mail(String subject, String content) { this.subject = subject; this.content = content; } 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; } } |
/** * */ package com.email; import java.security.GeneralSecurityException; /** * <p>Description: </p> * @author 朱开心 * @date 2018年6月6日 * */ import java.util.List; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; import com.sun.mail.util.MailSSLSocketFactory; /** * 邮件发送器:可单发,可群发 */ public class MailSender { /** * 发送邮件的Properties文件 */ private final transient Properties props = System.getProperties(); /** * 服务器邮箱登录验证 */ private transient MailAuthenticator mailAuthenticator; /** * 邮箱Session */ private transient Session session; /** * 初始化邮箱发送器 * * @param mailServiceUrl * 服务器邮箱地址 * @param username * 服务器邮箱用户名 * @param password * 服务器邮箱登录密码 */ public MailSender(final String mailServiceUrl,final String username, final String password){ init(mailServiceUrl, username, password); } /** * 初始化邮箱发送器 * * @param username * 服务器邮箱用户名 * @param password * 服务器邮箱登录密码 */ public MailSender(final String username, final String password){ //通过邮箱地址解析smtp服务器,对大多数邮箱都管用 final String mailServiceUrl = "smtp." + username.split("@")[1]; init(mailServiceUrl, username, password); } /** * 初始化操作 * * @param mailServiceUrl * 服务器邮箱地址 * @param username * 服务器邮箱用户名 * @param password * 服务器邮箱登录密码 */ public void init(String mailServiceUrl, String username, String password){ //初始化props props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", mailServiceUrl); MailSSLSocketFactory sf; try { sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); } catch (GeneralSecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } //服务器邮箱验证 mailAuthenticator = new MailAuthenticator(username, password); //创建session,想当于邮箱登录 session = Session.getInstance(props, mailAuthenticator); } /** * 发送邮件 * * @param recipient * 收信人邮箱地址 * @param subject * 邮件标题 * @param content * 邮件内容 * @throws AddressException * @throws MessagingException */ public void send(String recipient, String subject, String content) throws AddressException, MessagingException{ //创建mime类型邮件 final MimeMessage msg = new MimeMessage(session); //设置发信人 msg.setFrom(new InternetAddress(mailAuthenticator.getUsername())); //设置收信人 msg.setRecipient(RecipientType.TO, new InternetAddress(recipient)); //设置邮件标题 msg.setSubject(subject); //设置邮件内容 msg.setContent(content, "text/html;charset=utf-8"); //发送邮件 Transport.send(msg); } /** * 群发邮件 * * @param recipients * 收信人们的邮箱地址 * @param subject * 邮件标题 * @param content * 邮件内容 * @throws AddressException * @throws MessagingException */ public void send(List<String> recipients, String subject, String content) throws AddressException, MessagingException{ //创建mime类型邮件 final MimeMessage msg = new MimeMessage(session); //设置发信人 msg.setFrom(new InternetAddress(mailAuthenticator.getUsername())); //设置收信人们 int num = recipients.size(); InternetAddress[] addresses = new InternetAddress[num]; for (int i = 0; i < num; i++) { addresses[i] = new InternetAddress(recipients.get(i)); } msg.setRecipients(RecipientType.TO, addresses); //设置邮件标题 msg.setSubject(subject); //设置邮件内容 msg.setContent(content, "text/html;charset=utf-8"); Transport.send(msg); } /** * 发送邮件 * * @param recipient * 收信人邮箱地址 * @param mail * 邮件对象 * @throws AddressException * @throws MessagingException * @throws */ public void send(String recipient, Mail mail) throws AddressException, MessagingException{ this.send(recipient, mail.getSubject(), mail.getContent()); } /** * 群发邮件 * * @param recipients * 收信人们的邮箱地址 * @param mail * 邮件对象 * @throws AddressException * @throws MessagingException * @throws */ public void send(List<String> recipients, Mail mail) throws AddressException, MessagingException{ this.send(recipients, mail.getSubject(), mail.getContent()); } } |
/** * */ package com.email; /** * <p>Description: </p> * @author 朱开心 * @date 2018年6月6日 * */ import javax.mail.MessagingException; import javax.mail.internet.AddressException; /** * 测试发送邮件 */ public class MailTest { public static void main(String[] args) { //发件人邮箱地址:这里填写要发送出去的邮箱地址即可 String username = "[email protected]"; //发件人邮箱登录密码:这里填写要发送出去的邮箱的登录密码即可 String password = "akxpgntfdbtubcja"; //akxpgntfdbtubcja // vulyumdckcaibfga // akxpgntfdbtubcja //创建邮箱发送器 MailSender mailSender = new MailSender(username,password); //邮箱对象 Mail mail = new Mail("zhukaixin","我是世界上最帅的男人"); //收件人邮箱地址:这里填写收件的邮箱的地址即可 String recipient = "[email protected]"; try { mailSender.send(recipient, mail); } catch (AddressException e) { System.out.println("发信人邮箱路径不正确........"); e.printStackTrace(); } catch (MessagingException e) { System.out.println("邮件发送失败........"); e.printStackTrace(); } // System.out.println("发送成功........"); } } |
以上是关于Java 实现邮件的发送的主要内容,如果未能解决你的问题,请参考以下文章