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.