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

下载

访问我的GitHub页面https://github.com/mjremijan以查看我所有的开源项目。 这个职位的代码位于https://github.com/mjremijan/thoth-email中https://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示例

 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 { public SslTest() { } String now, hostname; protected String now, hostname; Properties yahoo; protected Properties yahoo; @BeforeEach public void setUp() throws Exception { now = new SimpleDateFormat( "MM-dd-yyyy hh:mm:ss a" ).format( new Date()); hostname = InetAddress.getLocalHost().getHostName(); yahoo = new Properties(); yahoo.load( this .getClass().getResourceAsStream( "/smtp-ssl-yahoo.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" , yahoo.getProperty( "host" )); props.setProperty( "mail.smtp.socketFactory.port" , yahoo.getProperty( "port" )); props.setProperty( "mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" ); } Session smtp = null ; { smtp = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( yahoo.getProperty( "username" ) , yahoo.getProperty( "password" ) ); } }); smtp.setDebug( true ); smtp.setDebugOut(System.out); } MimeMessage m = new MimeMessage(smtp); { m.setRecipient(Message.RecipientType.TO, new InternetAddress(yahoo.getProperty( "to" ))); m.setSubject( "thoth-email SSL test " + now); InternetAddress from = null ; { from = new InternetAddress(yahoo.getProperty( "from" )); from.setPersonal( "Thoth Email" ); m.setFrom(from); } InternetAddress reply = null ; { reply = new InternetAddress(yahoo.getProperty( "reply" )); m.setReplyTo( new InternetAddress[] {reply}); } m.setContent(content); } Transport.send(m); }  } 

摘要

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

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

java ssl发送邮件

java ssl发送邮件_通过SSL发送的Java邮件相关推荐

  1. Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件

    Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件 基于Spring boot 1.5,Spring boot 2.x请使用Spring boot mail 1.依赖坐标 // b ...

  2. java gmail 发送邮件_Java通过Gmail发送电子邮件

    大家好,我刚刚尝试获取一些Java代码,以通过gmail向Java用户发送电子邮件,这就是我所拥有的: @ManagedBean @ViewScoped public class email { // ...

  3. java 判断进程状态_获取远程服务器上 Java 进程的运行状态

    为了安全考虑, 有些服务器会被限制登录. 本文介绍如何获取远程服务器上 Java 进程的运行状态. 启动 jstatd 服务 在服务器端启动 jstatd 服务后, 远程的机器可以通过 rmi 协议获 ...

  4. java coin介绍_代码示例中的Java 7:Project Coin

    java coin介绍 该博客通过代码示例介绍了一些新的Java 7功能,这些项目在Project Coin一词下进行了概述. Project Coin的目标是向JDK 7添加一组小的语言更改.这些更 ...

  5. python3发送邮件_Python3使用SMTP发送带附件邮件

    一.设置开启SMTP服务并获取授权码 可以参考第一篇文章,这里不再赘述:[一]https://www.jb51.net/article/142220.htm 二.使用Python3 发送带附件的邮件 ...

  6. flex+java项目创建_创建Flex 4和Java Web应用程序

    当前的Web技术对它们的需求不断增长. 他们必须能够管理用户帐户,上载内容和流视频. 这种需求要求RIA开发人员寻求简化开发工作流程的技术,同时提供常见的功能. 开发人员面临的挑战是选择正确的技术来提 ...

  7. java derby数据库_使用Apache Derby进行Java数据库开发,第3部分

    该"使用Apache Derby进行Java数据库开发"系列的上一篇文章向您展示了如何使用Java Statement对象在Apache Derby数据库上执行SQL SELECT ...

  8. java rest 序列化_一文看懂Java序列化

    一文看懂Java序列化 简介 首先我们看一下wiki上面对于序列化的解释. 序列化(serialization)在计算机科学的数据处理中,是指将数据结构或对象状态转换成可取用格式(例如存成文件,存于缓 ...

  9. java 故障排查_目前最全的 Java 服务问题排查套路

    问题分类: CPU问题 内存问题(GC问题.内存泄漏. OOM,Coredump 等) I/O问题 问题排查工具箱: 系统级别的工具: top:查看系统/进程cpu.内存.swap等资源占用情况的必备 ...

最新文章

  1. 大数据的“近因偏差”烦恼
  2. string.h 的实现
  3. 提高Excel中VBA效率的四种方法
  4. Linux 下的五种 IO 模型
  5. python3进阶开发-第一个仿博客园的项目(1)
  6. linux用户一个用户只能一个用户组,为什么linux用户可以属于多个用户组,文件只能属于一个用户组?...
  7. 9447 CTF:no-strings-attached
  8. python中fg是什么意思_Python fg
  9. 凯撒密码加密器(命令行版)
  10. MapXtreme 2005学习(1):创建临时图层
  11. adminlte php,adminLTE 教程 -1 基础
  12. Mysql数据库入门 (基础知识点 由来 各种指令 如何运用)
  13. SpringCloud—07—高级之SpringCloud Alibaba上
  14. 数据库设计1-数据库设计简述
  15. 初识pyQt5之简易翻译软件
  16. matlab三相短路电流计算程序_基于MATLAB的短路电流计算程序编制.pdf
  17. Combo box控件的使用
  18. 电脑通过usb共享网络给手机上网
  19. 天数怎么换算成月_excel如何将天数换算为多少年多少月多少日
  20. 明星产品!种质资源数字化一站式解决方案!

热门文章

  1. Counting Triangles
  2. CF1368G Shifting Dominoes(扫描线求矩阵的并集)
  3. 数论三之排列组合Ⅱ——Virus Tree 2,RGB Coloring,123 Triangle,排列计数,排队,卡农
  4. P4716-[模板]最小树形图
  5. 51nod1675-序列变换【莫比乌斯反演】
  6. CF525D-Arthur and Walls【贪心】
  7. P2514-[HAOI2010]工厂选址【贪心】
  8. 2021牛客暑期多校训练营4 H-Convolution(数学)
  9. 2017上海金马五校 购买装备 贪心+二分Check
  10. jdk1.8.0_45源码解读——ArrayList的实现