2016-07-14 2 views
1

내 자신의 SMTP 서버를 사용하여 반송 된 이메일을 리디렉션하는 방법을 찾았습니다. 이제 요구 사항에 따라 수신 거부, 수신자 이메일 주소, 이메일 콘텐츠 등의 이유로 내 프로그램을 사용하여 반송 된 이메일을 읽을 수 있어야합니다. Stackoverflow는 dsn.jar이 도움이 될 수 있다고 제안했습니다. 몇 가지 방법이 있다는 것을 알았습니다. 하지만 어떻게 작동하는지 확인하기위한 예제는 찾을 수 없습니다. 여기 내가 반송 된 이메일을 리디렉션하는 방법입니다. 제 질문은 다음 프로그램의 내부/외부에서 반송 된 전자 메일을 읽을 수있는 기능을 추가하는 방법입니다. 도와주세요.Javamail과 DSN.jar를 사용하여 반송 된 이메일 구문 분석

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 = "[email protected]"; 
     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 = "[email protected]";//some invalid address 
     String bounceAddr = "[email protected]";//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(); 
    } 
} 

답변

0

반송 된 메시지 읽기는 다른 메시지를 읽는 것과 같습니다. 저장소에 연결하고 폴더를 열고 메시지를 읽어야합니다. 반송 된 메시지를 나타내는 Message 객체가 있으면 위에서 주석 처리 한 코드를 사용하여 해당 메시지의 내용을 처리 할 수 ​​있습니다. 그러나 Message 객체는 이 아니며은 전송 한 Message 객체가 아니라 [email protected] 계정과 연결된 Folder에서 온 완전히 다른 Message 객체입니다.

관련 문제