javamail smtp_JavaMail示例–使用SMTP以Java发送邮件

本文介绍了如何使用JavaMail API通过SMTP发送电子邮件,涵盖了无认证、TLS和SSL身份验证的示例,以及如何发送带附件和图片的邮件。讲解了创建Session、设置邮件属性以及处理不同身份验证方式的逻辑。
摘要由CSDN通过智能技术生成

javamail smtp

Today we will look into JavaMail Example to send email in java programs. Sending emails is one of the common tasks in real life applications and that’s why Java provides robust JavaMail API that we can use to send emails using SMTP server. JavaMail API supports both TLS and SSL authentication for sending emails.

今天,我们将研究JavaMail Example以Java程序发送电子邮件。 发送电子邮件是现实生活中应用程序中的常见任务之一,这就是Java提供可靠的JavaMail API的原因,我们可以使用该API来使用SMTP服务器发送电子邮件。 JavaMail API支持TLS和SSL身份验证来发送电子邮件。

JavaMail示例 (JavaMail Example)

Today we will learn how to use JavaMail API to send emails using SMTP server with no authentication, TLS and SSL authentication and how to send attachments and attach and use images in the email body. For TLS and SSL authentication, I am using GMail SMTP server because it supports both of them.

今天,我们将学习如何使用JavaMail API通过不带身份验证,TLS和SSL身份验证的SMTP服务器发送电子邮件,以及如何发送附件以及在电子邮件正文中附加和使用图像。 对于TLS和SSL身份验证,我使用的是GMail SMTP服务器,因为它同时支持两者。

JavaMail API is not part of standard JDK, so you will have to download it from it’s official website i.e JavaMail Home Page. Download the latest version of the JavaMail reference implementation and include it in your project build path. The jar file name will be javax.mail.jar.

JavaMail API不是标准JDK的一部分,因此您必须从其官方网站(即JavaMail主页)下载它。 下载最新版本的JavaMail参考实现,并将其包含在项目构建路径中。 jar文件的名称将为javax.mail.jar

If you are using Maven based project, just add below dependency in your project.

如果您正在使用基于Maven的项目,只需在项目中添加以下依赖项。

<dependency>
	<groupId>com.sun.mail</groupId>
	<artifactId>javax.mail</artifactId>
	<version>1.5.5</version>
</dependency>

Java Program to send email contains following steps:

发送电子邮件的Java程序包含以下步骤:

  1. Creating javax.mail.Session object

    创建javax.mail.Session对象
  2. Creating javax.mail.internet.MimeMessage object, we have to set different properties in this object such as recipient email address, Email Subject, Reply-To email, email body, attachments etc.

    创建javax.mail.internet.MimeMessage对象时,我们必须在此对象中设置不同的属性,例如收件人电子邮件地址,电子邮件主题,回复电子邮件,电子邮件正文,附件等。
  3. Using javax.mail.Transport to send the email message.

    使用javax.mail.Transport发送电子邮件。

The logic to create session differs based on the type of SMTP server, for example if SMTP server doesn’t require any authentication we can create the Session object with some simple properties whereas if it requires TLS or SSL authentication, then logic to create will differ.

创建会话的逻辑因SMTP服务器的类型而异,例如,如果SMTP服务器不需要任何身份验证,我们可以创建具有一些简单属性的Session对象,而如果它需要TLS或SSL身份验证,则创建逻辑将有所不同。

So I will create a utility class with some utility methods to send emails and then I will use this utility method with different SMTP servers.

因此,我将创建一个实用程序类,其中包含一些实用程序方法来发送电子邮件,然后将该实用程序方法用于不同的SMTP服务器。

JavaMail示例程序 (JavaMail Example Program)

Our EmailUtil class that has a single method to send email looks like below, it requires javax.mail.Session and some other required fields as arguments. To keep it simple, some of the arguments are hard coded but you can extend this method to pass them or read it from some config files.

我们的EmailUtil类只有一种发送电子邮件的方法,如下所示,它需要javax.mail.Session和一些其他必填字段作为参数。 为了简单起见,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值