用到java开发发送邮件的部分,其实很简单依赖的jar包有点击下载(无需积分)
主要是邮箱的服务器的验证
以下是源码
都是经过我自己运行才发表的,有任何问题可以留言或者邮箱联系我,我会第一时间处理。
服务器登陆的安全认证:
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * 服务器邮箱登录验证 * * @author [email protected] * */ public class MailAuthenticator extends Authenticator { /** * 用户名(登录邮箱) */ private String username; /** * 密码 */ private String password; /** * 初始化邮箱和密码 * * @param username * 邮箱 * @param password * 密码 */ public MailAuthenticator(String username, String password) { this.username = username; this.password = password; } String getPassword() { return password; } @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } String getUsername() { return username; } public void setPassword(String password) { this.password = password; } public void setUsername(String username) { this.username = username; } }
发件箱工厂:
import java.io.IOException; import java.util.Properties; /** * 发件箱工厂 * * @author [email protected] * */ public class MailSenderFactory { /** * 服务邮箱 */ private static SimpleMailSender serviceSms = null; /** * 获取邮箱 * * @param type * 邮箱类型 * @return 符合类型的邮箱 * @throws IOException */ public static SimpleMailSender getSender() throws IOException { if (initUser() == null) { serviceSms = new SimpleMailSender("[email protected]", "test_for_masque"); } return serviceSms; } private static SimpleMailSender initUser() throws IOException { Properties p = PropertiesUtil.getInstance("mail-user.properties"); if (null == p) return null; serviceSms = new SimpleMailSender(p.getProperty("user.name"), p.getProperty("user.password")); return serviceSms; } }
发送邮件用户名以及密码我是在文件中配置的
import java.io.IOException; import java.util.Properties; /** * 读取Properties * * @author [email protected] * */ public class PropertiesUtil { static Properties props = new Properties(); private PropertiesUtil() { } public static Properties getInstance(String path) throws IOException { props.load(PropertiesUtil.class.getClassLoader().getResourceAsStream( path)); return props; } public static String getProperty(String key) { String val = ""; if (props != null) { String prop = props.getProperty(key); if (prop != null) { val = prop; } } return val; } }
配置用户名以及密码:mail-user.properties
#test user.name = [email protected] user.password = test_for_masque
邮件的主题和内容实体封装
/** * * @author [email protected] * */ public class SimpleMail { private String subject; private String 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; } }
邮件发送的类:
import java.util.List; import java.util.Properties; import javax.mail.Message.RecipientType; 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; /** * 简单邮件发送器,可单发,群发。 * * @author [email protected] * */ public class SimpleMailSender { /** * 发送邮件的props文件 */ private final transient Properties props = System.getProperties(); /** * 邮件服务器登录验证 */ private transient MailAuthenticator authenticator; /** * 邮箱session */ private transient Session session; /** * 初始化邮件发送器 * * @param smtpHostName * SMTP邮件服务器地址 * @param username * 发送邮件的用户名(地址) * @param password * 发送邮件的密码 */ public SimpleMailSender(final String smtpHostName, final String username, final String password) { init(username, password, smtpHostName); } /** * 初始化邮件发送器 * * @param username * 发送邮件的用户名(地址),并以此解析SMTP服务器地址 * @param password * 发送邮件的密码 */ public SimpleMailSender(final String username, final String password) { // 通过邮箱地址解析出smtp服务器,对大多数邮箱都管用 final String smtpHostName = "smtp." + username.split("@")[1]; init(username, password, smtpHostName); } /** * 初始化 * * @param username * 发送邮件的用户名(地址) * @param password * 密码 * @param smtpHostName * SMTP主机地址 */ private void init(String username, String password, String smtpHostName) { // 初始化props props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", smtpHostName); // 验证 authenticator = new MailAuthenticator(username, password); // 创建session session = Session.getInstance(props, authenticator); } /** * 发送邮件 * * @param recipient * 收件人邮箱地址 * @param subject * 邮件主题 * @param content * 邮件内容 * @throws AddressException * @throws MessagingException */ public void send(String recipient, String subject, Object content) throws AddressException, MessagingException { // 创建mime类型邮件 final MimeMessage message = new MimeMessage(session); // 设置发信人 message.setFrom(new InternetAddress(authenticator.getUsername())); // 设置收件人 message.setRecipient(RecipientType.TO, new InternetAddress(recipient)); // 设置主题 message.setSubject(subject); // 设置邮件内容 message.setContent(content.toString(), "text/html;charset=utf-8"); // 发送 Transport.send(message); } /** * 群发邮件 * * @param recipients * 收件人们 * @param subject * 主题 * @param content * 内容 * @throws AddressException * @throws MessagingException */ public void send(List<String> recipients, String subject, Object content) throws AddressException, MessagingException { // 创建mime类型邮件 final MimeMessage message = new MimeMessage(session); // 设置发信人 message.setFrom(new InternetAddress(authenticator.getUsername())); // 设置收件人们 final int num = recipients.size(); InternetAddress[] addresses = new InternetAddress[num]; for (int i = 0; i < num; i++) { addresses[i] = new InternetAddress(recipients.get(i)); } message.setRecipients(RecipientType.TO, addresses); // 设置主题 message.setSubject(subject); // 设置邮件内容 message.setContent(content.toString(), "text/html;charset=utf-8"); // 发送 Transport.send(message); } /** * 发送邮件 * * @param recipient * 收件人邮箱地址 * @param mail * 邮件对象 * @throws AddressException * @throws MessagingException */ public void send(String recipient, SimpleMail mail) throws AddressException, MessagingException { send(recipient, mail.getSubject(), mail.getContent()); } /** * 群发邮件 * * @param recipients * 收件人们 * @param mail * 邮件对象 * @throws AddressException * @throws MessagingException */ public void send(List<String> recipients, SimpleMail mail) throws AddressException, MessagingException { send(recipients, mail.getSubject(), mail.getContent()); } }
运行测试主方法:将地址改为自己的QQ邮箱测试下吧
import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.mail.MessagingException; import javax.mail.internet.AddressException; /** * * @author [email protected] * */ public class Main { public static void main(String[] args) { try { SimpleMailSender sender = MailSenderFactory.getSender(); List<String> recipients = new ArrayList<String>(); for(int i=0;i<10;i++) recipients.add("[email protected]"); for (String recipient : recipients) sender.send(recipient, "测试邮件", "这是一封测试邮件!"); // sender.send(recipients, subject, content); } catch (IOException e) { e.printStackTrace(); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } }