目录

JavaMail - Gmail SMTP服务器( Gmail SMTP server)

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

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

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

创建Java类 (Create Java Class)

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

package com.iowiki;
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连接。

编译和运行 (Compile and Run)

现在我们的课已经准备好了,让我们编译上面的类。 我已将类SendEmailUsingGMailSMTP.java保存到目录: /home/manisha/JavaMailAPIExercise 。 我们需要在类路径中使用jars javax.mail.jaractivation.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

验证输出 (Verify Output)

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

Sent message successfully....
↑回到顶部↑
WIKI教程 @2018