Commit 2530b8419fb37b7fb144ca807d53bf4e804c3880
1 parent
3bd57b7b
青浦时刻表上传代码依赖
Showing
4 changed files
with
304 additions
and
0 deletions
src/main/java/com/bsth/email/MailAuthenticator.java
0 → 100644
| 1 | +package com.bsth.email; | |
| 2 | + | |
| 3 | +import javax.mail.Authenticator; | |
| 4 | +import javax.mail.PasswordAuthentication; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * 服务器邮箱登录验证 | |
| 8 | + */ | |
| 9 | +public class MailAuthenticator extends Authenticator{ | |
| 10 | + /** | |
| 11 | + * 用户名(登录邮箱) | |
| 12 | + */ | |
| 13 | + private String username; | |
| 14 | + /** | |
| 15 | + * 密码 | |
| 16 | + */ | |
| 17 | + private String password; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * 初始化邮箱和密码 | |
| 21 | + * | |
| 22 | + * @param username | |
| 23 | + * 邮箱 | |
| 24 | + * @param password | |
| 25 | + * 密码 | |
| 26 | + */ | |
| 27 | + public MailAuthenticator(String username, String password) { | |
| 28 | + this.username = username; | |
| 29 | + this.password = password; | |
| 30 | + } | |
| 31 | + | |
| 32 | + String getPassword() { | |
| 33 | + return password; | |
| 34 | + } | |
| 35 | + | |
| 36 | + @Override | |
| 37 | + protected PasswordAuthentication getPasswordAuthentication() { | |
| 38 | + return new PasswordAuthentication(username, password); | |
| 39 | + } | |
| 40 | + | |
| 41 | + String getUsername() { | |
| 42 | + return username; | |
| 43 | + } | |
| 44 | + | |
| 45 | + public void setPassword(String password) { | |
| 46 | + this.password = password; | |
| 47 | + } | |
| 48 | + | |
| 49 | + public void setUsername(String username) { | |
| 50 | + this.username = username; | |
| 51 | + } | |
| 52 | +} | ... | ... |
src/main/java/com/bsth/email/SendEmailController.java
0 → 100644
| 1 | +package com.bsth.email; | |
| 2 | + | |
| 3 | +import com.bsth.email.entity.EmailBean; | |
| 4 | +import com.bsth.util.Tools; | |
| 5 | +import org.springframework.stereotype.Component; | |
| 6 | + | |
| 7 | +import java.util.List; | |
| 8 | + | |
| 9 | +@Component | |
| 10 | +public class SendEmailController { | |
| 11 | + | |
| 12 | + /* | |
| 13 | + * recipients | |
| 14 | + * 收件人集合 | |
| 15 | ||
| 16 | + * 邮件 | |
| 17 | + */ | |
| 18 | + public int sendMail(List<String> recipients,EmailBean mail){ | |
| 19 | + Tools t = new Tools("mailbox.properties"); | |
| 20 | + SimpleMailSender sms = new SimpleMailSender(t.getValue("username"),t.getValue("password")); | |
| 21 | + try { | |
| 22 | + for (String recipient : recipients) { | |
| 23 | + sms.send(recipient, mail.getSubject(),mail.getContent()); | |
| 24 | + } | |
| 25 | + } catch (Exception e) { | |
| 26 | + e.printStackTrace(); | |
| 27 | + return -1; | |
| 28 | + } | |
| 29 | + return 1; | |
| 30 | + } | |
| 31 | + | |
| 32 | + /* | |
| 33 | + * recipient | |
| 34 | + * 收件人 | |
| 35 | ||
| 36 | + * 邮件 | |
| 37 | + */ | |
| 38 | + public int sendMail(String recipient,EmailBean mail){ | |
| 39 | + Tools t = new Tools("mailbox.properties"); | |
| 40 | + SimpleMailSender sms = new SimpleMailSender(t.getValue("username"),t.getValue("password")); | |
| 41 | + try { | |
| 42 | + sms.send(recipient, mail.getSubject(),mail.getContent()); | |
| 43 | + } catch (Exception e) { | |
| 44 | + e.printStackTrace(); | |
| 45 | + return -1; | |
| 46 | + } | |
| 47 | + return 1; | |
| 48 | + } | |
| 49 | +} | ... | ... |
src/main/java/com/bsth/email/SimpleMailSender.java
0 → 100644
| 1 | +package com.bsth.email; | |
| 2 | + | |
| 3 | +import com.bsth.email.entity.EmailBean; | |
| 4 | + | |
| 5 | +import java.util.List; | |
| 6 | +import java.util.Properties; | |
| 7 | + | |
| 8 | +import javax.activation.CommandMap; | |
| 9 | +import javax.activation.MailcapCommandMap; | |
| 10 | +import javax.mail.MessagingException; | |
| 11 | +import javax.mail.Session; | |
| 12 | +import javax.mail.Transport; | |
| 13 | +import javax.mail.internet.AddressException; | |
| 14 | +import javax.mail.internet.InternetAddress; | |
| 15 | +import javax.mail.internet.MimeMessage; | |
| 16 | +import javax.mail.internet.MimeMessage.RecipientType; | |
| 17 | + | |
| 18 | + | |
| 19 | +public class SimpleMailSender { | |
| 20 | + /** | |
| 21 | + * 发送邮件的props文件 | |
| 22 | + */ | |
| 23 | + private final transient Properties props = System.getProperties(); | |
| 24 | + /** | |
| 25 | + * 邮件服务器登录验证 | |
| 26 | + */ | |
| 27 | + private transient MailAuthenticator authenticator; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * 邮箱session | |
| 31 | + */ | |
| 32 | + private transient Session session; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * 初始化邮件发送器 | |
| 36 | + * | |
| 37 | + * @param smtpHostName | |
| 38 | + * SMTP邮件服务器地址 | |
| 39 | + * @param username | |
| 40 | + * 发送邮件的用户名(地址) | |
| 41 | + * @param password | |
| 42 | + * 发送邮件的密码 | |
| 43 | + */ | |
| 44 | + public SimpleMailSender(final String smtpHostName, final String username, | |
| 45 | + final String password) { | |
| 46 | + init(username, password, smtpHostName); | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * 初始化邮件发送器 | |
| 51 | + * | |
| 52 | + * @param username | |
| 53 | + * 发送邮件的用户名(地址),并以此解析SMTP服务器地址 | |
| 54 | + * @param password | |
| 55 | + * 发送邮件的密码 | |
| 56 | + */ | |
| 57 | + public SimpleMailSender(final String username, final String password) { | |
| 58 | + //通过邮箱地址解析出smtp服务器,对大多数邮箱都管用 | |
| 59 | + final String smtpHostName = "smtp." + username.split("@")[1]; | |
| 60 | + init(username, password, smtpHostName); | |
| 61 | + | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * 初始化 | |
| 66 | + * | |
| 67 | + * @param username | |
| 68 | + * 发送邮件的用户名(地址) | |
| 69 | + * @param password | |
| 70 | + * 密码 | |
| 71 | + * @param smtpHostName | |
| 72 | + * SMTP主机地址 | |
| 73 | + */ | |
| 74 | + private void init(String username, String password, String smtpHostName) { | |
| 75 | + // 初始化props | |
| 76 | + props.put("mail.smtp.auth", "true"); | |
| 77 | + props.put("mail.smtp.host", smtpHostName); | |
| 78 | + // 验证 | |
| 79 | + authenticator = new MailAuthenticator(username, password); | |
| 80 | + // 创建session | |
| 81 | + session = Session.getInstance(props, authenticator); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * 发送邮件 | |
| 86 | + * | |
| 87 | + * @param recipient | |
| 88 | + * 收件人邮箱地址 | |
| 89 | + * @param subject | |
| 90 | + * 邮件主题 | |
| 91 | + * @param content | |
| 92 | + * 邮件内容 | |
| 93 | + * @throws AddressException | |
| 94 | + * @throws MessagingException | |
| 95 | + */ | |
| 96 | + public void send(String recipient, String subject, Object content) | |
| 97 | + throws AddressException, MessagingException { | |
| 98 | + // 创建mime类型邮件 | |
| 99 | + final MimeMessage message = new MimeMessage(session); | |
| 100 | + // 设置发信人 | |
| 101 | + message.setFrom(new InternetAddress(authenticator.getUsername())); | |
| 102 | + // 设置收件人 | |
| 103 | + message.setRecipient(RecipientType.TO, new InternetAddress(recipient)); | |
| 104 | + // 设置主题 | |
| 105 | + message.setSubject(subject); | |
| 106 | + // 设置邮件内容 | |
| 107 | + message.setContent(content.toString(), "text/html;charset=utf-8"); | |
| 108 | + | |
| 109 | + MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); | |
| 110 | + mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); | |
| 111 | + mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); | |
| 112 | + mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); | |
| 113 | + mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); | |
| 114 | + mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); | |
| 115 | + CommandMap.setDefaultCommandMap(mc); | |
| 116 | + // 发送 | |
| 117 | + Transport.send(message); | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * 群发邮件 | |
| 122 | + * | |
| 123 | + * @param recipients | |
| 124 | + * 收件人们 | |
| 125 | + * @param subject | |
| 126 | + * 主题 | |
| 127 | + * @param content | |
| 128 | + * 内容 | |
| 129 | + * @throws AddressException | |
| 130 | + * @throws MessagingException | |
| 131 | + */ | |
| 132 | + public void send(List<String> recipients, String subject, Object content) | |
| 133 | + throws AddressException, MessagingException { | |
| 134 | + // 创建mime类型邮件 | |
| 135 | + final MimeMessage message = new MimeMessage(session); | |
| 136 | + // 设置发信人 | |
| 137 | + message.setFrom(new InternetAddress(authenticator.getUsername())); | |
| 138 | + // 设置收件人们 | |
| 139 | + final int num = recipients.size(); | |
| 140 | + InternetAddress[] addresses = new InternetAddress[num]; | |
| 141 | + for (int i = 0; i < num; i++) { | |
| 142 | + addresses[i] = new InternetAddress(recipients.get(i)); | |
| 143 | + } | |
| 144 | + message.setRecipients(RecipientType.TO, addresses); | |
| 145 | + // 设置主题 | |
| 146 | + message.setSubject(subject); | |
| 147 | + // 设置邮件内容 | |
| 148 | + message.setContent(content.toString(), "text/html;charset=utf-8"); | |
| 149 | + // 发送 | |
| 150 | + Transport.send(message); | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * 发送邮件 | |
| 155 | + * | |
| 156 | + * @param recipient | |
| 157 | + * 收件人邮箱地址 | |
| 158 | + * @param mail | |
| 159 | + * 邮件对象 | |
| 160 | + * @throws AddressException | |
| 161 | + * @throws MessagingException | |
| 162 | + */ | |
| 163 | + public void send(String recipient, EmailBean mail) | |
| 164 | + throws AddressException, MessagingException { | |
| 165 | + send(recipient, mail.getSubject(), mail.getContent()); | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * 群发邮件 | |
| 170 | + * | |
| 171 | + * @param recipients | |
| 172 | + * 收件人们 | |
| 173 | + * @param mail | |
| 174 | + * 邮件对象 | |
| 175 | + * @throws AddressException | |
| 176 | + * @throws MessagingException | |
| 177 | + */ | |
| 178 | + public void send(List<String> recipients, EmailBean mail) | |
| 179 | + throws AddressException, MessagingException { | |
| 180 | + send(recipients, mail.getSubject(), mail.getContent()); | |
| 181 | + } | |
| 182 | +} | ... | ... |
src/main/java/com/bsth/email/entity/EmailBean.java
0 → 100644
| 1 | +package com.bsth.email.entity; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Created by zlz on 2017-12-29. | |
| 5 | + */ | |
| 6 | +public class EmailBean { | |
| 7 | + private String subject;// 邮件标题 | |
| 8 | + private String content;//邮件内容 | |
| 9 | + public String getSubject() { | |
| 10 | + return subject; | |
| 11 | + } | |
| 12 | + public void setSubject(String subject) { | |
| 13 | + this.subject = subject; | |
| 14 | + } | |
| 15 | + public String getContent() { | |
| 16 | + return content; | |
| 17 | + } | |
| 18 | + public void setContent(String content) { | |
| 19 | + this.content = content; | |
| 20 | + } | |
| 21 | +} | ... | ... |