抽象

本博客的目的是演示如何使用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

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

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

    java ssl发送邮件 抽象 本博客的目的是演示如何使用Java Mail通过具有SSL连接的SMTP服务器发送电子邮件. 免责声明 这篇文章仅供参考. 在使用所提供的任何信息之前,请认真思考. 从 ...

  2. java 邮件 tls_通过TLS发送的Java邮件

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

  3. 通过TLS发送的Java邮件

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

  4. Android Java使用JavaMail API发送和接收邮件的代码示例

    JavaMail是Oracle甲骨文开发的Java邮件类API,支持多种邮件协议,这里我们就来看一下Java使用JavaMail API发送和接收邮件的代码示例 使用Javamail发送邮件,必需的j ...

  5. 内网java发送邮件_基于JavaMail的Java邮件发送:简单邮件发送

    电子邮件的应用非常广泛,例如在某网站注册了一个账户,自动发送一封欢迎邮件,通过邮件找回密码,自动批量发送活动信息等.但这些应用不可能和我们自己平时发邮件一样,先打开浏览器,登录邮箱,创建邮件再发送.本 ...

  6. java编写两邮件传输,JAVA邮件发送(文字+图片+附件)【源码】

    介绍: 电子邮件协议 电子邮件的在网络中传输和网页一样需要遵从特定的协议,常用的电子邮件协议包括 SMTP,POP3,IMAP.其中邮件的创建和发送只需要用到 SMTP协议,所有本文也只会涉及到SMT ...

  7. Java文件如何用qq邮箱发送_java中怎么发送复杂的邮件?在QQ邮箱中怎么操作?

    上次我们已经介绍过,如何实现纯文本发送邮件.今天再来为大家介绍下,在java中怎么发送复杂的邮件,以及在QQ邮箱中的具体操作流程. 首先我们来了解一下,MIME(多用途互联网邮件扩展类型).它分为两类 ...

  8. java邮件中添加excel_基于javaMail的邮件发送--excel作为附件

    基于JavaMail的Java邮件发送 Author xiuhong.chen@hand-china.com Desc 简单邮件发送 Date 2017/12/8 项目中需要根据物料资质的状况实时给用 ...

  9. java自带发送邮件,成都汇智动力-java邮件发送只需要java自带的mailjar

    原标题:成都汇智动力-java邮件发送只需要java自带的mailjar java邮件发送只需要java自带的mailjar Email email = emailService.queryEmail ...

最新文章

  1. TMG 2010 建立站对站***隧道
  2. Windows服务器补丁列表及介绍_传奇单机架设,列表读取失败?单机架设经常遇到的问题分享!...
  3. MySQL的root用户修改普通用户密码
  4. 使用SAP CRM WebClient UI Design layer修改field label
  5. kernel中对文件的读写【学习笔记】【原创】
  6. 巡回沙龙_美浮特全国巡回沙龙第一期结束撒花!
  7. iOS - LocalCache 本地数据缓存
  8. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 10丨换座位【难度中等】
  9. MySQL 查询数据
  10. Android SDK隐藏,如何使用可用的隐藏API和内部API构建Android SDK?
  11. android自定义转场动画,Activity转场动画(5.0+)
  12. 苹果Mac文件夹样式设计工具:Folder Designer
  13. [算法]用java实现堆操作
  14. 闭包基础概念,闭包详解
  15. 查看远端的端口是否通畅3个简单实用案例
  16. 神经网络控制基本原理,神经网络理论及其应用
  17. 什么是Saas,以及什么是PLG下的Saas
  18. Jboot 跨域请求
  19. Troubleshooting: High Version Count Issues (文档 ID 296377.1)
  20. VS2022 - 微软官方blog - 翻译

热门文章

  1. 【SPFA】重建道路(jzoj 1212)
  2. HDU5877 - Weak Pair
  3. hdu4609 3idiots 三角形计数 FFT
  4. Spark入门(八)之WordCount
  5. 搞定所有的跨域请求问题
  6. MySQL数据库开发的 36 条军规
  7. 关于分布式一致性的探究
  8. 修改Tomcat编码方式的两种方法
  9. java爬虫之基于httpclient的简单Demo(二)
  10. Java虚拟机是如何执行线程同步的