使用SSL/TLS身份验证通过Java发送电子邮件

JavaMail API定义了代表邮件系统组件的类。JavaMail没有实现电子邮件服务器,相反,它允许您使用Java API访问电子邮件服务器。为了测试呈现的代码,您必须有权访问电子邮件服务器。虽然JavaMail API规范并不要求支持特定的协议,但JavaMail通常包括对POP3、IMAP和SMTP的支持。

null
图片[1]-使用SSL/TLS身份验证通过Java发送电子邮件-yiteyi-C++库

电子邮件是如何工作的?

先决条件:

  • 有权访问SMTP服务器。您必须知道SMTP服务器的主机名、端口号和安全设置。网络邮件提供商可以提供SMTP访问、查看您的电子邮件帐户设置或帮助查找更多信息。请注意,您的用户名通常是您的完整电子邮件地址,而不仅仅是@符号前面的名称。
  • Java EE IDE和应用服务器,如GlassFish或Oracle WebLogic服务器。JavaMail可以 下载 作为JavaSE应用程序中的库,但本教程假设使用已经包含JavaMail的JavaEE应用程序服务器。

使用JavaMail发送电子邮件有以下三个步骤。详情如下:

获取会话对象 javax。邮政一场 类提供会话的对象,会话。getDefaultInstance()方法和会话。getInstance()方法。

// Setup mail serverproperties.setProperty("mail.smtp.host", host); // mail username and password   properties.setProperty("mail.user", "user");                   properties.setProperty("mail.password", "password$$");           

撰写信息 javax。邮政运输 类提供了发送消息的方法。

// javax.mail.internet.MimeMessage class is// mostly used for abstraction. MimeMessage message = new MimeMessage(session);// header field of the header.  message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO,                      new InternetAddress(to));  message.setSubject("subject");  message.setText("Hello, aas is sending email ");  

发送消息

Transport.send(message);

以下是在Java中使用SMTP发送邮件而不进行身份验证的完整实现-

JAVA

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail {
public static void main(String[] args)
{
// change below lines accordingly
String to = "got@gmail.com" ;
String from = "akash@gmail.com" ;
String host = "localhost" ; // or IP address
// Get the session object
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty( "mail.smtp.host" , host);
// Get the default Session object
Session session = Session.getDefaultInstance(properties);
// compose the message
try {
// javax.mail.internet.MimeMessage class
// is mostly used for abstraction.
MimeMessage message = new MimeMessage(session);
// header field of the header.
message.setFrom( new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject( "subject" );
message.setText( "Hello, aas is sending email " );
// Send message
Transport.send(message);
System.out.println( "Yo it has been sent.." );
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
}


输出

Yo it has been sent...

该程序简单易懂,运行良好,但在现实生活中,大多数SMTP服务器使用某种身份验证,如TLS或SSL身份验证。现在,我们将看到如何为这些身份验证协议创建会话对象。对于TLS和SSL,您可以知道运行这些服务的邮件服务器的端口。我们将为您提供考虑Gmail的代码。以下是使用SMTP和TLS身份验证在Java中发送邮件的完整实现-

JAVA

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMail {
public static void main(String[] args) {
// change accordingly
final String username = "username@gmail.com" ;
// change accordingly
final String password = "password" ;
// or IP address
final String host = "localhost" ;
// Get system properties
Properties props = new Properties();
// enable authentication
props.put( "mail.smtp.auth" , "true" );
// enable STARTTLS
props.put( "mail.smtp.starttls.enable" , "true" );
// Setup mail server
props.put( "mail.smtp.host" , "smtp.gmail.com" );
// TLS Port
props.put( "mail.smtp.port" , "587" );
// creating Session instance referenced to
// Authenticator object to pass in
// Session.getInstance argument
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication
getPasswordAuthentication() {
return new PasswordAuthentication(username,
password);
}
});
try {
// compose the message
// javax.mail.internet.MimeMessage class is
// mostly used for abstraction.
Message message = new MimeMessage(session);
// header field of the header.
message.setFrom( new InternetAddress( "from-email@gmail.com" ));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse( "to-email@gmail.com" ));
message.setSubject( "hello" );
message.setText( "Yo it has been sent" );
Transport.send(message); //send Message
System.out.println( "Done" );
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}


以下是在Java中使用SMTP和SSL身份验证发送邮件的完整实现-

JAVA

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail {
public static void main(String[] args)
{
// change accordingly
String to = "got@gmail.com" ;
// change accordingly
String from = "akash@gmail.com" ;
// or IP address
String host = "localhost" ;
// mail id
final String username = "username@gmail.com"
// correct password for gmail id
final String password = "mypassword" ;
System.out.println( "TLSEmail Start" );
// Get the session object
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty( "mail.smtp.host" , host);
// SSL Port
properties.put( "mail.smtp.port" , "465" );
// enable authentication
properties.put( "mail.smtp.auth" , "true" );
// SSL Factory
properties.put( "mail.smtp.socketFactory.class" ,
"javax.net.ssl.SSLSocketFactory" );
// creating Session instance referenced to
// Authenticator object to pass in
// Session.getInstance argument
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
// override the getPasswordAuthentication
// method
protected PasswordAuthentication
getPasswordAuthentication() {
return new PasswordAuthentication( "username" ,
"password" );
}
});
}
//compose the message
try {
// javax.mail.internet.MimeMessage class is mostly
// used for abstraction.
MimeMessage message = new MimeMessage(session);
// header field of the header.
message.setFrom( new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject( "subject" );
message.setText( "Hello, aas is sending email " );
// Send message
Transport.send(message);
System.out.println( "Yo it has been sent.." );
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
}


多个客户端我们可以对上述代码进行以下更改

JAVA

// This is an array of e-mail ID. You would
// need to use InternetAddress() method
// while specifying email IDs
void addRecipients(Message.RecipientType type,
Address[] addresses)


© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享