JavaMail Gmail SMTP服务器 - JavaMail

在所有前面的章节中,我们使用JangoSMPT服务器来发送电子邮件。在本章中,我们将了解通过Gmail时提供的SMTP伺服器。 Gmail的(等等)提供了使用他们的公共SMTP服务器的免费。

Gmail SMTP服务器的详细信息可以在这里找到。正如你可以在细节里看到的一样,我们可以使用TLS或SSL连接,以通过Gmail SMTP服务器发送邮件。

使用Gmail SMTP服务器发送邮件的过程类似的发送电子邮件的章节中描述说明,除了我们改变主机服务器。作为先决条件,发件人的电子邮件地址应该是一个活跃的Gmail帐户。让我们尝试一个例子。

创建Java类

创建一个Java类文件SendEmailUsingGMailSMTP,内容都是如下:

package com.yiibai;

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 = "[email protected]";//change accordingly

      // Sender's email ID needs to be mentioned
      String from = "[email protected]";//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. 我们需要 javax.mail.jar 和 activation.jar 文件在classpath中。执行下面的命令从命令提示符编译类(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....