JavaMail API - Gmail SMTP 伺服器



在之前的所有章節中,我們都使用 JangoSMPT 伺服器傳送郵件。在本節中,我們將學習 Gmail 提供的 SMTP 伺服器。Gmail(以及其他一些服務)免費提供其公共 SMTP 伺服器的使用。

可以在 此處 找到 Gmail SMTP 伺服器的詳細資訊。如您所見,我們可以使用 TLS 或 SSL 連線透過 Gmail SMTP 伺服器傳送郵件。

使用 Gmail SMTP 伺服器傳送郵件的過程與章節 傳送郵件 中解釋的類似,只是我們需要更改主機伺服器。作為先決條件,發件人郵箱地址應為有效的 Gmail 帳戶。讓我們嘗試一個示例。

建立 Java 類

建立一個 Java 檔案 SendEmailUsingGMailSMTP,其內容如下所示

package com.tutorialspoint;

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。我們需要將 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

驗證輸出

您應該在命令控制檯上看到以下訊息

Sent message successfully....
廣告

© . All rights reserved.