Java QQ messages sent three forms (commons-email)

Original link: https://blog.csdn.net/qq_38225558/article/details/84960229

The first step: the introduction of appropriate project resources    commons-email-xx.jar, mail.jar, activation.jar

maven pom introduced item (note that the configuration corresponding to version problems oh ~) may Reference ->  maven dependency query


  
  
  1. <-! Mail Support ->
  2. <dependency>
  3. <groupId>javax.mail </groupId>
  4. <artifactId>mail </artifactId>
  5. <version>1.4.1 </version>
  6. </dependency>
  7. <dependency>
  8. <groupId>activation </groupId>
  9. <artifactId>activation </artifactId>
  10. <version>1.0.2 </version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.commons </groupId>
  14. <artifactId>commons-email </artifactId>
  15. <version>1.5 </version>
  16. </dependency>

 


Note that before transmission needs to be sent to the mailbox corresponding settings, refer  QQ opening SMTP mail service step

Open the following two

Remember your authorization code, oh, later to be used to! !


Step two: send a message   (three forms for reference)


  
  
  1. /**
  2. * Send e-mail QQ
  3. * Mode ①: send the message simple code
  4. * Mode ②: Send mail add attachments
  5. * Mode ③: send HTML mail format
  6. */
  7. public class EmailTest {
  8. @Test simple code // Send mail
  9. public void testSimpleEmail() throws Exception {
  10. SimpleEmail email = new new SimpleEmail (); // create a simple email subject
  11. // Here I use the QQ, using smtp server requires authentication, if you are using another server, you can go to their Web search
  12. email.setHostName( "smtp.qq.com");
  13. //POP3服务器(端口995)
  14. //SMTP服务器(端口465或587)。
  15. email.setSmtpPort( 465);
  16. //验证信息(发送的邮箱地址与密码) 注:这里的密码是授权码
  17. email.setAuthenticator( new DefaultAuthenticator( "[email protected]", "填写你的授权码"));
  18. email.setSSLOnConnect( true); // 是否启用SSL
  19. email.setFrom( "[email protected]"); //发送邮件的地址(和验证信息的地址一样)
  20. email.setSubject( "第一封简单邮件"); //邮件的标题
  21. email.setMsg( "简单的邮件来了哦!!!"); //邮件的内容
  22. email.addTo( "[email protected]"); //发送给哪一个邮件
  23. email.send(); //进行邮件发送
  24. }
  25. @Test //发送邮件中添加附件
  26. public void testEmailAttachment() throws Exception {
  27. EmailAttachment attachment = new EmailAttachment(); //创建一个附件对象
  28. attachment.setPath( "POITestImage/1.jpg"); //放一张项目中的图片(指向真实的附件)
  29. attachment.setDisposition(EmailAttachment.ATTACHMENT); //完成附件设置
  30. attachment.setDescription( "这张图片是一个..."); //设置附件的描述
  31. attachment.setName( "1.jpg"); //设置附件的名称
  32. //创建email对象(MultiPartEmail可以操作附件)
  33. MultiPartEmail email = new MultiPartEmail();
  34. email.setHostName( "smtp.qq.com");
  35. email.setSmtpPort( 465);
  36. //验证信息(发送的邮箱地址与密码) 注:这里的密码是授权码
  37. email.setAuthenticator( new DefaultAuthenticator( "[email protected]", "填写你的授权码"));
  38. email.setSSLOnConnect( true); // 是否启用SSL
  39. email.setFrom( "[email protected]"); //发送邮件的地址(和验证信息的地址一样)
  40. email.addTo( "[email protected]"); //发送给哪一个邮件
  41. email.setSubject( "这是一张图片"); //邮件标题
  42. email.setMsg( "我发了一张图片给你看哦!"); //邮件内容
  43. email.attach(attachment); //把附件加到邮件中
  44. email.send(); //发送邮件
  45. }
  46. @Test //发送HTML的邮件格式
  47. public void testHtml() throws Exception {
  48. HtmlEmail email = new HtmlEmail(); // 创建可以写Html的email对象
  49. email.setHostName( "smtp.qq.com");
  50. email.setSmtpPort( 465);
  51. email.setAuthenticator( new DefaultAuthenticator( "[email protected]", "填写你的授权码"));
  52. email.setSSLOnConnect( true); // 是否启用SSL
  53. email.setCharset( "UTF-8"); //发送的时候如果乱码,配置相应的编码
  54. email.addTo( "[email protected]"); //发送给哪一个邮件
  55. email.setFrom( "[email protected]", "xx"); //xx为发件人名字
  56. email.setSubject( "这里面写HTML,非常厉害");
  57. //设置HTML的信息
  58. String url = "https://www.baidu.com/";
  59. email.setHtmlMsg( "<html><h1 style='color:green;'><a href="+url+ ">www.baidu.com</a></h1>点击进入百度</html>");
  60. //email.setTextMsg("这个就是很一般的显示"); //也可以配置普通的信息
  61. email.send(); //发送邮件
  62. }
  63. }

效果图:

发布了53 篇原创文章 · 获赞 9 · 访问量 1403

第一步:项目引入相应资源   commons-email-xx.jar,mail.jar,activation.jar

Guess you like

Origin blog.csdn.net/qq_40629521/article/details/104089824