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)
 <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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331756.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java中的break与continue用法

一、break break 的作用为跳出循环&#xff0c;执行循环外面的操作 &#xff08;1&#xff09;简单break public class Main {public static void main(String[] args) {int i0;for(;i<100;i){if(i2)break;}System.out.println(i);} } 输出结果&#xff1a;2 双重循环 publ…

java中的native关键字有什么作用?(java本地方法)

转自&#xff1a; http://zhidao.baidu.com/link?urlXu94DBMxXz3sJyCrG7G1sCmXoHuyuYx4DMG1x7UqYL7FhfFnqF7-Z9nxIQUpntPkqzaZ0xAyIjKIrEYrwIett_ 1、什么是Native Method 简单地讲&#xff0c;一个Native Method就是一个java调用非java代码的接口。一个Native Method是这样一…

php sessionid 重复,php_ session_id 限制同一用户同时登录

出于信息安全的考虑&#xff0c;希望给每个能进入系统的人员一个账户&#xff0c;而不是所有人共用一个账户&#xff0c;并且一个账户同时只能一人登陆。刚开始的做法是登陆加锁&#xff0c;当用户登陆之后&#xff0c;对此用户进行标记&#xff0c;若此用户未下线状态下进行第…

JavaFX之TableView

TableView表 构建一个表主要有TableView,TableColumn,ObservableList,Bean。 添加列table.getColumns().addAll(); ObservableList里面是存放的数据 table.setItems(observableList);添加数据 observableList里面一般是存放的Bean&#xff0c;列与Bean之间建立联系&#xf…

java native关键字(java本地方法)

转自&#xff1a; http://blog.csdn.net/youjianbo_han_87/article/details/2586375 native是与C联合开发的时候用的&#xff01;java自己开发不用的&#xff01; 【1】使用native关键字说明这个方法是原生函数&#xff0c;也就是这个方法是用C/C语言实现的&#xff0c;并且被…

php 按钮的属性值,HTML button标签的属性有哪些

HTML button的属性有&#xff1a;autofocus、disabled、form、formaction、formenctype、formmethod、formnovalidate、formtarget、name、type、value。本教程操作环境&#xff1a;windows7系统、HTML5版、Dell G3电脑。HTML 标签标签定义一个按钮。在 button 元素内部&#x…

javafx之TableView的FXCSS

TableView的FXCSS 一、特殊的table设置 TableView的单元之间去掉行横线 .table-view .table-row-cell { -fx-background-insets: 0; } TableView的单元之间去掉没有数据的竖线 table-row-cell:empty .table-cell { -fx-border-width: 0px; } TableView 的单元…

sql 注射_令人惊讶的注射

sql 注射所以&#xff0c;我欠吉姆道歉。 他编写了一个有效的模拟和JUnit测试&#xff0c;我在回顾中告诉他&#xff0c;我认为它没有达到他的预期。 当我错了时&#xff0c;这种情况对我来说就像是一个错误 。 称它为理想的意外副作用。 假设您有以下两类&#xff1a; public…

java中的equals方法+hashCode方法

【0】README 0.1&#xff09;以下内容均为原创&#xff0c;包括源代码&#xff0c; 旨在理清 equals 和 hashCode 方法的 实现原理&#xff1b; 0.2&#xff09; for full resource code, please visit https://github.com/pacosonTang/core-java-volume/blob/master/chapte…

mysql判断条件用法,MySQL数据库讲解条件判断函数 MySQL数据库使用教程

函数&#xff1a;(1)IF(expr,v1,v2)函数(2)IFNULL(v1,v2)函数(3)CASE函数(相关免费学习推荐&#xff1a;mysql视频教程)(1)if(expr,v1,v2)函数在if(expr,v1,v2)函数中,若表达式expr是true(expr<>0 and epr<>null)返回v1&#xff0c;否则返回v2。【例】使用if()函数…

cuba.platform_CUBA 7.2 –有什么新功能?

cuba.platformCUBA平台的第七版向前迈出了一大步。 内部体系结构的改进和新的IDE为进一步改进奠定了良好的基础。 我们将继续添加新功能&#xff0c;以使开发人员的生活更轻松&#xff0c;并使他们的工作更加高效。 在7.2版中&#xff0c;我们引入了许多可能看起来像是主要更新…

javafx之TableView的TableColumn

TableColumn列 列与Bean之间建立联系&#xff1a; setCellValueFactory(); 通过cell值工厂建立与Bean的联系。它这里并不需要知道你是传了什么Bean&#xff0c;它只需要通过“字段名”反射去Bean里面获得值&#xff0c;所以Bean属性定义的名字不需要与它相同&#xff0c;只需…

java集合——具体的集合

【0】README 0.1&#xff09; 本文描述 转自 core java volume 1&#xff0c; 旨在理解 java集合——具体的集合 的相关知识&#xff1b; 【1】下表展示了 java类库中的集合&#xff0c;并简要描述了每个集合类的用途。 1.1&#xff09;在下表中&#xff0c; 除了以 Map结尾的…

excel 26进制 php,记录一次华为招聘的编程题-excel中的26进制

var line "abcdefghijklmnopqrstuvwxyz";var list line.split("");function baseConversion(N) {var jz [];//获得有0的26进制while (true) {if (parseInt(N/26) 0) {jz.push(N%26);break;} else{jz.push(N%26);N parseInt(N/26);}}//转化成无0的26进…

日发帖 发帖频率 发帖时段_先发帖

日发帖 发帖频率 发帖时段通常&#xff0c;我们编写代码来计算出一堆可用的答案。 让我们来看一下Java中的情况。 public Widget getAppropriateWidget(CustomerRequest request) { if (shelfstock.contains(request.getBarcode()) { return new ShelfWidget(); } if (backroom…

java集合——数组列表(ArrayList)+散列集(HashSet)

【0】README 0.1&#xff09; 本文描述源代码均 转自 core java volume 1&#xff0c; 旨在理解 java集合——数组列表&#xff08;ArrayList&#xff09;散列集&#xff08;HashSet&#xff09; 的相关知识&#xff1b; 0.2&#xff09; 散列集HashSet 涉及到 hashCode&…

javafx之TableView的TaleCell

TaleCell 对TableColumn的cell里面弄重新构造 TableColumn的setCellFactory(TextFieldTableCell.forTableColumn());有一些默认的构造。 或者重写TableCell类 [java] view plaincopy tableColumn.setCellFactory(new Callback<TableColumn<Path, Number>, TableCell…

jdk8 cms g1gc_JDK 14:CMS GC是OBE

jdk8 cms g1gcJDK 14 Early Access Build&#xff03;23 &#xff08; 2019/11/13 &#xff09; 现已上市 。 此版本最值得注意的更改之一是删除了并发标记扫描垃圾收集器 。 JEP 291 [“弃用并发标记扫描&#xff08;CMS&#xff09;垃圾收集器”]早在2017年就使用JDK 9和JEP …

java集合——树集(TreeSet)+对象的比较

【0】README 0.1&#xff09; 本文描述转自 core java volume 1&#xff0c; 源代码为原创&#xff0c;旨在理解 java集合——树集&#xff08;TreeSet&#xff09;对象的比较 的相关知识&#xff1b; 0.2&#xff09; for full source code, please visit https://github.co…

php curl伪装cookies,php curl 添加cookie伪造登陆抓取数据

有的网页必须登陆才能看到&#xff0c;这个时候想要抓取信息必须在header里面传递cookie值才能获取1、首先登陆网站&#xff0c;打开firebug就能看到对应的cookie把这些cookie拷贝出来就能使用了2、<?php header("Content-type:text/html;Charsetutf8");$ch curl…