java 邮件 tls_通过TLS发送的Java邮件
java 郵件 tls
抽象
本博客的目的是演示如何使用Java Mail通過具有TLS連接的SMTP服務器發送電子郵件。
免責聲明
這篇文章僅供參考。 在使用所提供的任何信息之前,請認真思考。 從中學到東西,但最終自己做出決定,風險自負。
要求
我使用以下主要技術完成了本文的所有工作。 您可能可以使用不同的技術或版本來做相同的事情,但不能保證。
- NetBeans 11.2
- Maven 3.3.9(與NetBeans捆綁在一起)
- Java 11(zulu11.35.15-ca-jdk11.0.5-win_x64)
下載
訪問我的GitHub頁面https://github.com/mjremijan以查看我所有的開源項目。 這篇文章的代碼位于thoth-email-via-tls模塊中的https://github.com/mjremijan/thoth-email 。
物產
本示例使用smtp-tls-outlook.properties文件保存SMTP服務器信息。 我用我個人的Outlook帳戶進行測試,因此使用這個詞的outlook屬性文件中的名稱。 重要的是文件的內容,如清單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 TLS 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通過具有TLS連接的SMTP服務器發送電子郵件。 清單2顯示了代碼。
注意對于初始測試,請始終檢查您的SPAM文件夾。 可以始終添加一條規則以將其傳遞到您的INBOX。
清單2 – Java Mail示例
package org.thoth.email.via.tls; 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 TlsTest { public TlsTest() { } String now, hostname; protected String now, hostname; protected Properties outlook; @BeforeEach public void setUp() throws Exception { now = new SimpleDateFormat( "MM-dd-yyyy hh:mm:ss a" ).format( new Date()); hostname = InetAddress.getLocalHost().getHostName(); outlook = new Properties(); outlook.load( this .getClass().getResourceAsStream( "/smtp-tls-outlook.properties" )); } @Test public void a_test() throws Exception { // Create MimeMultipart MimeMultipart content = new MimeMultipart( "related" ); // html part { MimeBodyPart textPart = new MimeBodyPart(); textPart.setText( "<html><body>" + "<p>Time: " +now+ "</p>" + "<p>From: " +hostname+ "</p>" + "</body></html>" , "UTF8" , "html" ); content.addBodyPart(textPart); } // properties Properties props = new Properties(); { props.setProperty( "mail.smtp.auth" , "true" ); props.setProperty( "mail.smtp.host" , outlook.getProperty( "host" )); props.setProperty( "mail.smtp.port" , outlook.getProperty( "port" )); props.setProperty( "mail.smtp.starttls.enable" , "true" ); } Session smtp = null ; { smtp = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( outlook.getProperty( "username" ) , outlook.getProperty( "password" ) ); } }); smtp.setDebug( true ); smtp.setDebugOut(System.out); } MimeMessage m = new MimeMessage(smtp); { m.setRecipient(Message.RecipientType.TO, new InternetAddress(outlook.getProperty( "to" ))); m.setSubject( "thoth-email TLS test " + now); InternetAddress from = null ; { from = new InternetAddress(outlook.getProperty( "from" )); from.setPersonal( "Thoth Email" ); m.setFrom(from); } InternetAddress reply = null ; { reply = new InternetAddress(outlook.getProperty( "reply" )); m.setReplyTo( new InternetAddress[] {reply}); } m.setContent(content); } Transport.send(m); } }摘要
發送郵件的代碼不是很困難。 成功接收電子郵件而不將其標記為垃圾郵件是另一回事。 但是,如果您遵循此示例,請使用有效的帳戶,并且不要過度使用它,則應該可以。 該博客顯示了如何使用Java Mail通過具有TLS連接的SMTP服務器發送電子郵件。
翻譯自: https://www.javacodegeeks.com/2020/02/java-mail-sent-over-tls.html
java 郵件 tls
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java 邮件 tls_通过TLS发送的Java邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css3轮播图怎么实现(css如何实现轮
- 下一篇: sql 注射_令人惊讶的注射