使用Javamail和DSN.jar解析退回的电子邮件 [英] Parsing bounced emails using Javamail and DSN.jar

查看:352
本文介绍了使用Javamail和DSN.jar解析退回的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了使用自己的smtp服务器重定向退回电子邮件的方法.现在,根据要求,我应该能够使用我的程序阅读退回的电子邮件,例如退回的原因,收件人的电子邮件地址,电子邮件内容等.Stackoverflow建议dsn.jar可能会有所帮助.我看到它有一些方法.但是我找不到任何示例来检查它的工作方式.这是我重定向退回电子邮件的方式,我的问题是如何添加功能以在以下程序的内部/外部读取此处退回的电子邮件?请帮忙.

I found the way to redirect my bounced emails using my own smtp server. Now, as per the requirement, I should be able to read the bounced emails using my program, like reason for bouncing, receiver's email address, email content and etc. Stackoverflow suggested that dsn.jar might be helpful. I saw it has some methods. But I don't find any examples to check how it is working. Here is the way I am redirecting the bounced emails, my question is how to add a functionality to read the bounced emails here inside/outside the following program ? Please help.

import java.io.FileInputStream;
import java.io.InputStream;
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;
import com.sun.mail.dsn.DeliveryStatus;
import com.sun.mail.dsn.DispositionNotification;
import com.sun.mail.dsn.MessageHeaders;
import com.sun.mail.dsn.MultipartReport;
import com.sun.mail.dsn.Report;

public class SendEmail {
   public static void main(String[] args) throws Exception {
     Properties properties=new Properties();
     InputStream input=new FileInputStream("SendEmail.properties");
     properties.load(input);
      //String smtpServer = "smtp.gmail.com";
      String smtpServer = "Server.Address";
      int port = 25;
      final String userid = "abc@dhv.com";
      final String password = properties.getProperty("EMAIL_PASSWORD1");
      String contentType = "text/html";
      String subject = "test: bounce an email to a different address " +
                "from the sender";
      String to = "bounceee@fauxmail.com";//some invalid address
      String bounceAddr = "redirectingAddress@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", "port");
      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);
      //SMTPMessage message=new SMTPMessage(mailSession);
      message.addFrom(InternetAddress.parse(userid));
      message.setRecipients(Message.RecipientType.TO, to);
      //message.setHeader("Return-path", bounceAddr);
      message.setSubject(subject);
      message.setContent(body, contentType);
      message.addHeader("Disposition-Notification-To",bounceAddr);
      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();
      }
      /*System.out.println(message.isMimeType("multipart/report"));
      System.out.println(message.isMimeType("text/html"));
      MultipartReport multireport = (MultipartReport)message.getContent();
      Report report=new Report(multireport);*/
     /* DeliveryStatus status=new DeliveryStatus();
      //status.ge
      DispositionNotification notification=new DispositionNotification();
      notification.getNotifications();
      MessageHeaders headers=new MessageHeaders();
      MultipartReport multiReport=new MultipartReport();
      multiReport.getReturnedMessage();
      //Report
      Report report=new Report();*/

    /*  if (message.isMimeType("multipart/report")) {
          System.out.println("Inside the loop");
          MultipartReport report = (MultipartReport)message.getContent();
          // see com.sun.mail.dsn package javadocs for MutlipartReport
          report.getReturnedMessage();
          MessageHeaders header=new MessageHeaders();
         // header.getRecipients(arg0);
      }*/
      transport.close();
   }
}

推荐答案

阅读退回邮件就像阅读其他任何邮件一样-您必须连接到商店,打开文件夹,然后阅读邮件.一旦具有代表退回邮件的Message对象,就可以使用上面注释掉的那种代码来处理该邮件的内容.但是请注意,消息对象将不是是您发送的消息对象,而是一个完全不同的消息对象,该对象来自与redirectingAddress@gmail.com帐户关联的文件夹.

Reading a bounced message is like reading any other message - you have to connect to the Store, open the Folder, and read the Message. Once you have a Message object that represents a bounced message, you can use the kind of code you have commented out above to process the content of that message. But note that the Message object will not be the Message object you sent, but rather a completely different Message object that comes from the Folder associated with the redirectingAddress@gmail.com account.

这篇关于使用Javamail和DSN.jar解析退回的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆