100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 利用Java的JavaMail发送邮件:企业邮箱版和个人邮箱客端版

利用Java的JavaMail发送邮件:企业邮箱版和个人邮箱客端版

时间:2022-07-31 16:36:38

相关推荐

利用Java的JavaMail发送邮件:企业邮箱版和个人邮箱客端版

本文链接: /qq_35257397/article/details/79004987

废话不说进入正题:

1.

第一步

项目基于maven 搭建。引入pom.xml

<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency>

第二步

创建邮箱实体类封装数据。

@Data//get set 插件public class MailVO implements Serializable {private static final long serialVersionUID = 4280650483975256784L;// 邮件发送者的标示private String key;// 登陆邮件发送服务器的用户名和密码private String mailAccount;private String mailPassword;//收件人名字private String senderAlias;// 邮件接收者的地址数组private List<String> receiveAddressArray;// 邮件主题private String subject;// 邮件的文本内容private String content;// 邮件附件的文件名private String[] attachFileNames;}

实现的代码

/*** Created by why on /7/28.*/public class MailServiceImp implements MailService {@Resource(name = "taskExecutor")TaskExecutor taskExecutor;//注入Spring封装的异步执行器private Log log = LogFactory.getLog(getClass());public void sendEmail(MailVO mail){Properties emailProperty = new Properties();try {InputStream resourceAsStream = MailServiceImp.class.getClassLoader().getResourceAsStream("application.properties");emailProperty.load(new BufferedInputStream(resourceAsStream));} catch (IOException e) {e.printStackTrace();}Properties sendProperty = new Properties();mail.setMailAccount(emailProperty.getProperty("mail.account"));mail.setMailPassword(emailProperty.getProperty("mail.password"));mail.setSenderAlias(emailProperty.getProperty("mail.alias"));sendProperty.setProperty("mail.transport.protocol",emailProperty.getProperty("mail.transport.protocol"));// 使用的协议(JavaMail规范要求)sendProperty.setProperty("mail.smtp.host",emailProperty.getProperty("mail.smtp.host"));// 发件人的邮箱的 SMTP 服务器地址sendProperty.setProperty("mail.smtp.auth",emailProperty.getProperty("mail.smtp.auth"));// 开启SSL加密,否则会失败try {MailSSLSocketFactory sf =new MailSSLSocketFactory();sf.setTrustAllHosts(true);sendProperty.put("mail.smtp.ssl.enable", "true");sendProperty.put("mail.smtp.ssl.socketFactory", sf);Session session = Session.getDefaultInstance(sendProperty);session.setDebug(true); //设置为debug模式看日志// sendMailByAsynchronousMode(session,mail);sendMailBySynchronizationMode(session,mail);} catch (Exception e) {e.printStackTrace();}}private void sendMailByAsynchronousMode(Session session,MailVO mail){taskExecutor.execute(new Runnable(){@Overridepublic void run(){try {sendMailBySynchronizationMode(session,mail);} catch (Exception e) {log.info(e);}}});}private void sendMailBySynchronizationMode(Session session,MailVO mail) throws IOException,MessagingException {MimeMessage message = new MimeMessage(session);message.setFrom(new InternetAddress(mail.getMailAccount(),mail.getKey()));List<String> recipients = mail.getReceiveAddressArray();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(Message.RecipientType.TO, addresses);message.setSubject(mail.getSubject(), "UTF-8");message.setContent(mail.getContent(),"text/html;charset=UTF-8");message.setSentDate(new Date());message.saveChanges();Transport transport = session.getTransport();transport.connect(mail.getMailAccount(),mail.getMailPassword());// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人transport.sendMessage(message, message.getAllRecipients());// 7. 关闭连接transport.close();}}

代码中注释的

@Resource(name = “taskExecutor”)

TaskExecutor taskExecutor;//注入Spring封装的异步执行器

是多线程发送邮箱的。有兴趣个人

最后一部配置application.properties

#邮件发送mail.transport.protocol=smtpmail.account=XXXX@//填你的账号mail.password=XXX//你的授权码。 不是邮箱的密码mail.smtp.host= smtp.#mail.smtp.host=mail.smtp.auth=true

说明一下配置文件

mail.password=XXX//你的授权码。 不是邮箱的密码

如何开起你的授权码?自己百度

最最罪重要的,这里很坑爹

host 通道个人邮箱和企业邮箱通道不同

下面是163的

个人邮箱:

企业邮箱:smtp.

希望有帮助到你哟。有什么不清楚的请留言哟。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。