当前位置:   article > 正文

java ssl发送邮件_通过SSL发送的Java邮件

java ssl发送邮件

java ssl发送邮件

抽象

本博客的目的是演示如何使用Java Mail通过具有SSL连接的SMTP服务器发送电子邮件。

免责声明

这篇文章仅供参考。 在使用所提供的任何信息之前,请认真思考。 从中学到东西,但最终自己做出决定,风险自负。

要求

我使用以下主要技术完成了本文的所有工作。 您可能可以使用不同的技术或版本来做相同的事情,但不能保证。

  • NetBeans 11.2
  • Maven 3.3.9(与NetBeans捆绑在一起)
  • Java 11(zulu11.35.15-ca-jdk11.0.5-win_x64)
  1. <dependency>
  2.    <groupId>javax.mail</groupId>
  3.    <artifactId>mail</artifactId>
  4.    <version> 1.4 </version>
  5.    <scope>test</scope> </dependency>

下载

访问我的GitHub页面https://github.com/mjremijan以查看我所有的开源项目。 这个职位的代码位于https://github.com/mjremijan/thoth-emailhttps://github.com/mjremijan/thoth-email/tree/master/thoth-email-via-ssl模块。

物产

本示例使用smtp-ssl-yahoo.properties文件保存SMTP服务器信息。 我使用了我个人的Yahoo! 帐户进行测试,因此在属性文件的名称中使用单词yahoo 。 重要的是文件的内容,如清单1所示。

清单1 –属性文件

 # This is the name of the SMTP host machine.  host=  # This is the port number of the SMTP host machine.  # The same host may support both SSL and TLS but on  # different ports. So make sure you get the SSL port.  port=  # This is what you use in the “username” field when  # you login. Typically # you login. Typically this is the same as your email  # address, but this isn't always the case .  username=  # This is what you use in the “password” field when  # you login. This value is CLEAR TEXT, so keep # you login. This value is CLEAR TEXT, so keep this  # properties file safe.  password=  # This is the email address you want for the  # email's FROM field. Enter the value using  # the format shown below. Typically # the format shown below. Typically this is  # just your email address for the account.  from=FIRSTNAME LASTNAME <ADDRESS @EMAIL .COM>  # This is the email address you want for the  # email's REPLY_TO field. Enter the value using  # the format shown below. Typically # the format shown below. Typically this is  # just your email address for the account. Also the account. Also  # typically this is the same as `from` above.  # But be warned, if an email's FROM and REPLY_TO  # are different, that's may be flagged as spam  # and never be delivered. So keep `from` and  # `reply` the same for initial testing  reply=FIRSTNAME LASTNAME <ADDRESS @EMAIL .COM>  # This is the email address you want to send  # the email to. For testing, it's a good idea  # to send it to yourself first.  to=FIRSTNAME LASTNAME <ADDRESS @EMAIL .COM> 

现在您有了一个属性文件,接下来让我们看一下代码。

这是一个JUnit测试,演示如何使用Java Mail通过具有SSL连接的SMTP服务器发送电子邮件。 清单2显示了代码。

注意对于初始测试,请始终检查您的SPAM文件夹。 可以始终添加一条规则以将其传递到您的INBOX。

清单2 – Java Mail示例

  1. package org.thoth.email.via.ssl; import java.net.InetAddress; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class SslTest {
  2.    public SslTest() {
  3.    }
  4.    String now, hostname; protected String now, hostname;
  5.    Properties yahoo; protected Properties yahoo;
  6.    @BeforeEach
  7.    public void setUp() throws Exception {
  8.      now = new SimpleDateFormat( "MM-dd-yyyy hh:mm:ss a" ).format( new Date());
  9.      hostname = InetAddress.getLocalHost().getHostName();
  10.      yahoo = new Properties();
  11.      yahoo.load( this .getClass().getResourceAsStream( "/smtp-ssl-yahoo.properties" ));
  12.    }
  13.    @Test
  14.    public void a_test() throws Exception {
  15.      // Create MimeMultipart
  16.      MimeMultipart content = new MimeMultipart( "related" );
  17.      // html part
  18.      {
  19.        MimeBodyPart textPart = new MimeBodyPart();
  20.        textPart.setText( "<html><body>"
  21.          + "<p>Time: " +now+ "</p>"
  22.          + "<p>From: " +hostname+ "</p>"
  23.          + "</body></html>"
  24.          , "UTF8" , "html" );
  25.        content.addBodyPart(textPart);
  26.      }
  27.      // properties
  28.      Properties props = new Properties();
  29.      {
  30.        props.setProperty( "mail.smtp.auth" , "true" );
  31.        props.setProperty( "mail.smtp.host" , yahoo.getProperty( "host" ));
  32.        props.setProperty( "mail.smtp.socketFactory.port" , yahoo.getProperty( "port" ));
  33.        props.setProperty( "mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
  34.      }
  35.      Session smtp = null ;
  36.      {
  37.        smtp = Session.getInstance(props, new Authenticator() {
  38.          @Override
  39.          protected PasswordAuthentication getPasswordAuthentication() {
  40.            return new PasswordAuthentication(
  41.                yahoo.getProperty( "username" )
  42.              , yahoo.getProperty( "password" )
  43.            );
  44.          }
  45.        });
  46.        smtp.setDebug( true );
  47.        smtp.setDebugOut(System.out);
  48.      }
  49.      MimeMessage m = new MimeMessage(smtp);
  50.      {
  51.        m.setRecipient(Message.RecipientType.TO, new InternetAddress(yahoo.getProperty( "to" )));
  52.        m.setSubject( "thoth-email SSL test " + now);
  53.        InternetAddress from = null ;
  54.        {
  55.          from = new InternetAddress(yahoo.getProperty( "from" ));
  56.          from.setPersonal( "Thoth Email" );
  57.          m.setFrom(from);
  58.        }
  59.        InternetAddress reply = null ;
  60.        {
  61.          reply = new InternetAddress(yahoo.getProperty( "reply" ));
  62.          m.setReplyTo( new InternetAddress[] {reply});
  63.        }
  64.        m.setContent(content);
  65.      }
  66.      Transport.send(m);
  67.    } }

摘要

发送邮件的代码不是很困难。 成功接收电子邮件而不将其标记为垃圾邮件是另一回事。 但是,如果您遵循此示例,请使用有效的帐户,并且不要过度使用它,则应该可以。 该博客显示了如何使用Java Mail通过具有SSL连接的SMTP服务器发送电子邮件。

翻译自: https://www.javacodegeeks.com/2020/02/java-mail-sent-over-ssl.html

java ssl发送邮件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/484411
推荐阅读
相关标签
  

闽ICP备14008679号