JavaMail API - 退信郵件



郵件退信可能由多種原因造成。這個問題在rfc1211中有深入討論。只有伺服器才能確定特定郵箱或使用者名稱是否存在。當伺服器檢測到錯誤時,它會向原始郵件的發件人返回一條訊息,說明失敗的原因。

許多網際網路標準涵蓋了郵件投遞狀態通知,但是大量伺服器不支援這些新標準,而是使用臨時技術來返回此類失敗訊息。因此,很難將退信與導致問題的原始郵件關聯起來。

JavaMail 包含對解析郵件投遞狀態通知的支援。有一些技術和啟發式方法可以處理這個問題。其中一種技術是可變信封返回路徑。您可以像下面的示例中所示設定信封中的返回路徑。這是退信郵件傳送到的地址。您可能希望將其設定為與“發件人:”標頭不同的通用地址,以便您可以處理遠端退信。這是透過在 JavaMail 中設定mail.smtp.from屬性來實現的。

建立 Java 類

建立一個名為SendEmail的 Java 類檔案,其內容如下:

import java.util.Properties;

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

public class SendEmail {
   public static void main(String[] args) throws Exception {
      String smtpServer = "smtp.gmail.com";
      int port = 587;
      final String userid = "youraddress";//change accordingly
      final String password = "*****";//change accordingly
      String contentType = "text/html";
      String subject = "test: bounce an email to a different address " +
				"from the sender";
      String from = "youraddress@gmail.com";
      String to = "bouncer@fauxmail.com";//some invalid address
      String bounceAddr = "toaddress@gmail.com";//change accordingly
      String body = "Test: get message to bounce to a separate email address";

      Properties props = new Properties();

      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", smtpServer);
      props.put("mail.smtp.port", "587");
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.from", bounceAddr);

      Session mailSession = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(userid, password);
            }
         });

      MimeMessage message = new MimeMessage(mailSession);
      message.addFrom(InternetAddress.parse(from));
      message.setRecipients(Message.RecipientType.TO, to);
      message.setSubject(subject);
      message.setContent(body, contentType);

      Transport transport = mailSession.getTransport();
      try {
         System.out.println("Sending ....");
         transport.connect(smtpServer, port, userid, password);
         transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
         System.out.println("Sending done ...");
      } catch (Exception e) {
         System.err.println("Error Sending: ");
         e.printStackTrace();

      }
      transport.close();
   }// end function main()
}

在這裡我們可以看到,屬性mail.smtp.from設定的值與from地址不同。

編譯和執行

現在我們的類已經準備好了,讓我們編譯上面的類。我已經將SendEmail.java類儲存到目錄:/home/manisha/JavaMailAPIExercise。我們需要在類路徑中包含javax.mail.jaractivation.jar這兩個jar包。從命令提示符執行以下命令來編譯類(這兩個jar包都放在/home/manisha/目錄下):

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

現在類已經編譯好了,執行以下命令執行:

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

驗證輸出

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

Sending ....
Sending done ...
廣告
© . All rights reserved.