编程语言
首页 > 编程语言> > java实现发送邮件功能

java实现发送邮件功能

作者:互联网

java实现发送邮件功能

 

  1. <!--发送邮件-->
  2. <dependency>
  3. <groupId>javax.mail</groupId>
  4. <artifactId>mail</artifactId>
  5. <version>1.4.7</version>
  6. </dependency>

 

  1. import org.apache.tomcat.util.descriptor.web.WebXml;
  2. import java.util.Date;
  3. import java.util.Properties;
  4. import javax.mail.Message.RecipientType;
  5. import javax.mail.Session;
  6. import javax.mail.Transport;
  7. import javax.mail.internet.InternetAddress;
  8. import javax.mail.internet.MimeMessage;
  9. public class MailUtils {
  10. //服务器的地址
  11. public static final String SMTPSERVER = "xxx";
  12. //服务器的端口
  13. public static final String SMTPPORT = "xxx";
  14. //服务器的账号
  15. public static final String ACCOUT = "xxxxx";
  16. //服务器的密码
  17. public static final String PWD = "xxxxxx";
  18. //发件人的账号
  19. public static final String emailform = "xxxxx";
  20. public static void main(String[] args) throws Exception {
  21. String emailto = "xxxxxxxxxx";
  22. String subject = "(出差人英文名)取消出差通知";
  23. String content= "<出差人英文名>原计划(时间含yyyy/mm/dd hh:mm)xx到xx,从(地点)xx到xx进行出差。先提出取消,请知晓!\n" +
  24. "【取消原因】\n" +
  25. "Xxxxx\n" +
  26. "【原出差目的】\n" +
  27. "Xxxxx\n";
  28. MailUtils.sendEmail( emailto, subject, content);
  29. }
  30. public static void sendEmail(String emailto,String subject,String content) {
  31. try {
  32. // 创建邮件配置
  33. Properties props = new Properties();
  34. props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
  35. props.setProperty("mail.smtp.host", SMTPSERVER); // 发件人的邮箱的 SMTP 服务器地址
  36. props.setProperty("mail.smtp.port", SMTPPORT);
  37. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  38. // props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
  39. // props.setProperty("mail.smtp.ssl.enable", "true");// 开启ssl
  40. // 根据邮件配置创建会话,注意session别导错包
  41. Session session = Session.getDefaultInstance(props);
  42. // 开启debug模式,可以看到更多详细的输入日志
  43. session.setDebug(true);
  44. //创建邮件
  45. MimeMessage message = createEmail(session, emailto, subject, content);
  46. //获取传输通道
  47. Transport transport = session.getTransport();
  48. transport.connect(SMTPSERVER,ACCOUT, PWD);
  49. //连接,并发送邮件
  50. transport.sendMessage(message, message.getAllRecipients());
  51. transport.close();
  52. }catch (Exception e){
  53. e.printStackTrace();
  54. }
  55. }
  56. private static MimeMessage createEmail(Session session, String emailto,String subject,String content) throws Exception {
  57. // 根据会话创建邮件
  58. MimeMessage msg = new MimeMessage(session);
  59. // address邮件地址, personal邮件昵称, charset编码方式
  60. InternetAddress fromAddress = new InternetAddress(emailform,
  61. "", "utf-8");
  62. // 设置发送邮件方
  63. msg.setFrom(fromAddress);
  64. InternetAddress receiveAddress = new InternetAddress(
  65. emailto, "", "utf-8");
  66. // 设置邮件接收方
  67. msg.setRecipient(RecipientType.TO, receiveAddress);
  68. // 设置邮件标题
  69. msg.setSubject(subject, "utf-8");
  70. msg.setText(content);
  71. // 设置显示的发件时间
  72. msg.setSentDate(new Date());
  73. // 保存设置
  74. msg.saveChanges();
  75. return msg;
  76. }
  77. }

 

https://blog.csdn.net/liu154794/article/details/98964549

标签:java,String,import,发送,static,props,mail,邮件
来源: https://www.cnblogs.com/sunny3158/p/16656342.html