JavaMail API - Gmail SMPT服务器

在之前的所有章节中,我们使用JangoSMPT服务器发送电子邮件.在本章中,我们将了解Gmail提供的SMPT服务器. Gmail(以及其他)可免费使用其公共SMTP服务器.

可以找到Gmail SMTP服务器详细信息这里.正如您在详细信息中看到的,我们可以使用TLS或SSL连接通过Gmail SMTP服务器发送电子邮件.

使用Gmail SMTP服务器发送电子邮件的过程与章节中说明的类似发送电子邮件,但我们会更改主机服务器.作为先决条件,发件人电子邮件地址应该是活动的Gmail帐户.让我们试一个例子.

创建Java类

创建一个Java文件 SendEmailUsingGMailSMTP ,其内容如下:

 
 package com.it1352; 
 import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmailUsingGMailSMTP {
   public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "xyz@gmail.com";//change accordingly

      // Sender's email ID needs to be mentioned
      String from = "abc@gmail.com";//change accordingly
      final String username = "abc";//change accordingly
      final String password = "*****";//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = "smtp.gmail.com";

      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", "587");

      // Get the Session object.
      Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
         protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
         }
      });

      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
         InternetAddress.parse(to));

         // Set Subject: header field
         message.setSubject("Testing Subject");

         // Now set the actual message
         message.setText("Hello, this is sample for to check send "
            + "email using JavaMailAPI ");

         // Send message
         Transport.send(message);

         System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
            throw new RuntimeException(e);
      }
   }
}

此处主机设为 smtp.gmail.com 和端口设置为 587 .这里我们启用了TLS连接.

编译并运行

现在我们的类已经准备就绪,让我们编译上面的类.我已将类SendEmailUsingGMailSMTP.java保存到目录:/home/manisha/JavaMailAPIExercise .我们需要类路径中的jar javax.mail.jar activation.jar .执行以下命令从命令提示符编译类(两个jar放在/home/manisha/目录中):

 
 javac -cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar:SendEmailUsingGMailSMTP.java

现在该类已编译,请执行下面的命令运行:

 
 java -cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar:SendEmailUsingGMailSMTP

验证输出

您应该在命令控制台上看到以下消息:

Sent message successfully....