使用 org.springframework.mail发送邮件

在讲代码前先做些准备工作。

1.在服务器上添加发送邮箱为白名单。

2.获取发送邮箱的主机,端口,授权码。一般在邮箱的设置里面都能找到。

3.代码(不知道为什么,复制过来就一行)



package com.fairyland.jdp.framework.util;import java.net.URL;import java.util.Properties;import javax.mail.internet.MimeMessage;import org.apache.commons.lang3.StringUtils;import org.springframework.core.io.InputStreamSource;import org.springframework.core.io.UrlResource;import org.springframework.mail.MailException;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;public class MailUtil {   // public static JavaMailSenderImpl mailSender =   // createMailSender("smtp.263xmail.com", 25,   // "InformationSendingMail@metlife.com",   // "Hello01", 25000);   public static JavaMailSenderImpl mailSender = createMailSender("smtp.qq.com", 25, "1194862954@qq.com",         "fkrurxuawayijhhg", 25000);   private static JavaMailSenderImpl createMailSender(String host, int port, String username, String password,         int timeout) {      JavaMailSenderImpl sender = new JavaMailSenderImpl();      sender.setHost(host);      sender.setPort(port);      sender.setUsername(username);      sender.setPassword(password);      sender.setDefaultEncoding("Utf-8");      Properties p = new Properties();      p.setProperty("mail.smtp.timeout", String.valueOf(timeout));      p.setProperty("mail.smtp.auth", "true");      sender.setJavaMailProperties(p);      return sender;   }   public static boolean sendMail(String title, String html, String recipients, String urls) throws Exception {      return sendAttachsMail(title, html, recipients, "", "", urls);   }   public static void main(String[] args) throws Exception {//    sendText("测试", "https://ezt-uat.metlife.com.cn/static/test.xls", "1194862954@qq.com", null, null);//    sendAttachsMail("test", "this is a test email!", "1194862954@qq.com", null, null, "file://C:/Users/fangza/Desktop/Test/c.xls");   }   /****** 下面都是特殊时候使用,发送邮件使用上面的方法 ************/   // 1.发送简单的文本邮件   public static boolean sendText(String title, String message, String recipients, String ccrecipients, String bccrecipients)         throws MailException {      SimpleMailMessage msg = new SimpleMailMessage();      msg.setFrom(mailSender.getUsername());      if (StringUtils.isEmpty(recipients)) {         return false;      }      String[] recipientArr = recipients.split(";");      msg.setTo(recipientArr);      if (!StringUtils.isEmpty(ccrecipients)) {         String[] ccrecipientArr = ccrecipients.split(";");         msg.setCc(ccrecipientArr);      }      if (!StringUtils.isEmpty(bccrecipients)) {         String[] bccrecipientArr = bccrecipients.split(";");         msg.setBcc(bccrecipientArr);      }      msg.setSubject(title);      msg.setText(message);      mailSender.send(msg);      return true;   }   // 2.发送带附件的邮件   public static boolean sendAttachsMail(String title, String html, String recipients, String ccrecipients,         String bccrecipients, String urls) throws Exception {      MimeMessage miMessage = mailSender.createMimeMessage();      MimeMessageHelper helper = new MimeMessageHelper(miMessage, true, "UTF-8");      helper.setFrom(mailSender.getUsername());      if (StringUtils.isEmpty(recipients)) {         return false;      }      String[] recipientArr = recipients.split(";");      helper.setTo(recipientArr);      if (!StringUtils.isEmpty(ccrecipients)) {         String[] ccrecipientArr = ccrecipients.split(";");         helper.setCc(ccrecipientArr);      }      if (!StringUtils.isEmpty(bccrecipients)) {         String[] bccrecipientArr = bccrecipients.split(";");         helper.setBcc(bccrecipientArr);      }      helper.setSubject(title);      helper.setText(html, true);      if (!StringUtils.isEmpty(urls)) {         String[] urls1 = urls.split(";");         for (int i = 0; i < urls1.length; i++) {            URL url = new URL(urls1[i]);            InputStreamSource asInputStreamSource = new UrlResource(url);            String[] arr = url.toString().split("/");            String fileTitle = arr[arr.length - 1];            helper.addAttachment(fileTitle, asInputStreamSource);         }      }      mailSender.send(miMessage);      return true;   }}复制代码


转载于:https://juejin.im/post/5d006c4b51882514293930f1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值