Javamail Api 简明教程

JavaMail API - Bounced Messages

邮件可能会因多种原因而反弹。这个问题在 rfc1211 中进行了深入讨论。只有服务器才能确定特定邮箱或用户名是否存在。当服务器检测到错误时,它会返回一条消息,说明原始邮件发送者失败的原因。

A message can be bounced for several reasons. This problem is discussed in depth at rfc1211. Only a server can determine the existence of a particular mailbox or user name. When the server detects an error, it will return a message indicating the reason for the failure to the sender of the original message.

有很多 Internet 标准包含传递状态通知,但是大量服务器不支持这些新标准,而是使用特殊技术来返回此类失败消息。因此,将退信与导致问题的原始邮件关联起来非常困难。

There are many Internet standards covering Delivery Status Notifications but a large number of servers don’t support these new standards, instead using ad hoc techniques for returning such failure messages. Hence it get very difficult to correlate the bounced message with the original message that caused the problem.

JavaMail 包含对解析传递状态通知的支持。处理此问题有多种技术和启发式方法。其中一种技术是可变信封返回路径。您可以设置信封中的返回路径,如下例所示。这是退信邮件发送到的地址。您可能希望将其设置为不同于发件人:标头的一般地址,以便处理远程退信。这是通过在 JavaMail 中设置 mail.smtp.from 属性完成的。

JavaMail includes support for parsing Delivery Status Notifications. There are a number of techniques and heuristics for dealing with this problem. One of the techniques being Variable Envelope Return Paths. You can set the return path in the enveloper as shown in the example below. This is the address where bounce mails are sent to. You may want to set this to a generic address, different than the From: header, so you can process remote bounces. This done by setting mail.smtp.from property in JavaMail.

Create Java Class

创建一个 java 类文件 SendEmail ,其内容如下:

Create a java class file SendEmail, the contents of which are as follows:

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 的设置不同于发件人地址。

Here we can see that the property mail.smtp.from is set different from the from address.

Compile and Run

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

Now that our class is ready, let us compile the above class. I’ve saved the class SendEmail.java to directory : /home/manisha/JavaMailAPIExercise. We would need the jars javax.mail.jar and activation.jar in the classpath. Execute the command below to compile the class (both the jars are placed in /home/manisha/ directory) from command prompt:

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

现在类已编译,执行以下命令来运行:

Now that the class is compiled, execute the below command to run:

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

Verify Output

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

You should see the following message on the command console:

Sending ....
Sending done ...