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)
 <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version> 1.4 </version> <scope>test</scope>  </dependency> 

下载

访问我的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邮件相关推荐

  1. 通过TLS发送的Java邮件

    抽象 本博客的目的是演示如何使用Java Mail通过具有TLS连接的SMTP服务器发送电子邮件. 免责声明 这篇文章仅供参考. 在使用所提供的任何信息之前,请认真思考. 从中学到东西,但最终自己做出 ...

  2. java发送简单邮件_Java程序实现发送简单文本邮件

    /** * Java程序实现发送简单文本邮件 * * @author Administrator * */ public class SendTextMail { // 定义发件人地址 public  ...

  3. php使用qq发邮件怎么发,php 发送QQ邮箱邮件

    这是我的源码比较简陋 https://www.lanzous.com/i2l7h8f 感谢 https://www.cnblogs.com/woider/p/6980456.html 下载phpmai ...

  4. python3邮件_python3使用SMTP发送HTML格式邮件

    一.设置开启SMTP服务并获取授权码 二.使用Python3 发送HTML格式的邮件 0.使用的环境为: Python 3.6.3 (64bit) PyCharm 2017.3 (64bit) 1.实 ...

  5. python发邮件11002_【python发送zabbix报警邮件,SSL版本】mailman.py

    [python发送zabbix报警邮件,SSL版本]mailman.py#!/usr/local/bin/python3 # # via  @ 20150210 # SSL only ''' 用法: ...

  6. python3邮件_python3使用SMTP发送简单文本邮件

    一.设置开启SMTP服务并获取授权码 0.如果使用第三方邮件服务器SMTP服务来发送邮件,首先要在邮箱设置里面开启POP3/SMTP/IMAP服务,下面以163邮箱为例,其它邮箱设置方法相同 163邮 ...

  7. java实现收端和发送端,java实现udp发送端和接收端

    发送端: package demo02; import java.io.IOException; import java.net.DatagramPacket; import java.net.Dat ...

  8. Java注册登录及邮箱发送账号激活(主要技术栈SpringBoot,MyBatis)

    文章目录 前言 学习之前需要掌握的知识 项目环境搭建 数据库的搭建 前端页面的搭建 后端代码 格式 pojo mapper Controller service 最后 前言 项目流程图如下: 这里我们 ...

  9. python邮件模块_Python收发邮件模块,用,来,发送,接收

    用Python来发送接收邮件模块 python实现发送和接收邮件功能主要用到poplib和smtplib模块. poplib用于接收邮件,而smtplib负责发送邮件. # -- coding :ut ...

最新文章

  1. sql server登录名、服务器角色、数据库用户、数据库角色、架构区别联系
  2. 【Qt】Log4Qt(二)使用
  3. JavaWeb学习总结(十七)——JSP中的九个内置对象
  4. Memcache 笔记
  5. c程序封装linux,Linux系统使用C语言封装线程读写锁
  6. SQL Server 找回没有备份的数据
  7. 按功能顺序列出的 HTML 4.01/XHTML 1.0
  8. 整数划分问题的递归算法-c语言,简单的整数划分问题(递归)
  9. python+selenuim自动化测试(六)上传文件
  10. UI控件篇——UIPageControl及其自定义
  11. -bash: findstr: command not found 问题解决
  12. 绘图板应用讲解计算机,电脑绘图板怎么用?基础电脑绘图板入门教程!
  13. 巧妙实现 Form 表单认证跨站点、跨服务器的单点登录(Single Sign On)
  14. 【量化笔记】动量Momentum相关技术指标以其含义
  15. 服务器2003蓝屏A5修复,win2003文件服务器蓝屏修复全过程分享第2/2页
  16. JDK1.8之前造成HashMap死链问题
  17. csgo调哪个会流畅_CSGO:怎样提高游戏帧数,让画面变得更流畅?这些设置可以考虑一下...
  18. 繁体与简体之间的转化
  19. C语言指针中P、*P、P、**P的区别
  20. ARM Linux下UPnP使用

热门文章

  1. CF914G Sum the Fibonacci(FWT模板+子集卷积)
  2. CF1137C:Museums Tour(缩点、分层图)
  3. 51nod2626-未来常数【树上启发式合并,线段树】
  4. P7046-「MCOI-03」诗韵【SAM,倍增,树状数组】
  5. P6091-[模板]原根
  6. P2056-[ZJOI2007]捉迷藏【点分树,堆】
  7. nssl1511-我的世界【堆,贪心】
  8. jzoj3682-Points and Segments【模型转化,欧拉回路】
  9. BZOJ1251 序列终结者
  10. 动态规划训练17 [Super Jumping! Jumping! Jumping! HDU - 1087 ]