JavaMail-通过身份验证发送

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaMail-通过身份验证发送相关的知识,希望对你有一定的参考价值。

Simple Mail Example.

Tags: SMTP, Mail, Java

[JavaMail (Sun)](http://java.sun.com/products/javamail/)
[JavaMail (Galileo)](http://openbook.galileocomputing.de/javainsel8/javainsel_18_012.htm#mj2a4110a634c82a3f676d8f87a50d6567)
  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4.  
  5. public class JavaMail
  6. {
  7. public static void postMail( String recipient,
  8. String subject,
  9. String message, String from )
  10. throws MessagingException
  11. {
  12. Properties props = new Properties();
  13. props.put( "mail.smtp.host", "mySMTPHost" );
  14. props.put( "mail.smtp.auth","true");
  15.  
  16. Authenticator authenticator = new Authenticator () {
  17. public PasswordAuthentication getPasswordAuthentication(){
  18. return new PasswordAuthentication("user" "password");
  19. }
  20. };
  21.  
  22. Session session = Session.getDefaultInstance( props , authenticator);
  23. Message msg = new MimeMessage( session );
  24. InternetAddress addressFrom = new InternetAddress( from );
  25. msg.setFrom( addressFrom );
  26. InternetAddress addressTo = new InternetAddress( recipient );
  27. msg.setRecipient( Message.RecipientType.TO, addressTo );
  28. msg.setSubject( subject );
  29. msg.setContent( message, "text/plain" );
  30.  
  31. Transport.send( msg );
  32. }
  33.  
  34. public static void main( String[] args ) throws Exception
  35. {
  36. postMail( "[email protected]",
  37. "mySubject",
  38. "myContent",
  39. }
  40. }

以上是关于JavaMail-通过身份验证发送的主要内容,如果未能解决你的问题,请参考以下文章

JavaMail OAuth AuthenticationFailed异常

JavaMail---邮件发送

JavaMail实现邮箱验证码

javamail的邮件发送有验证失败错误

25springboot发送邮件

25springboot发送邮件