2013-05-20 65 views
0

我想修改或刪除「Message-ID」標頭時,回覆一封帶有Javamail的電子郵件。經過一番研究,我發現我需要創建一個擴展MimeMessage的自定義類。這是我創建的課程。Javamail回覆刪除或修改消息ID標頭

import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.internet.MimeMessage; 

class MyMessage extends MimeMessage 
{ 

    public MyMessage(Session session) 
    { 
     super(session); 
    } 

    @Override 
    protected void updateMessageID() throws MessagingException { 
     removeHeader("Message-Id"); 
    } 

} 

下面的代碼與獲取消息

public List<EmailSenderInfo> checkEmail() throws Exception 
{ 
    String host = "HOST"; 
    String username = "MYUSERNAME"; 
    String password = "MYPASS"; 

    List<EmailSenderInfo> emailSenderList = new ArrayList<EmailSenderInfo>(); 

    Properties properties = System.getProperties(); 
    properties.setProperty("mail.store.protocol", "imaps"); 
    Session session = Session.getDefaultInstance(properties); 

    Store store = session.getStore("imaps"); 
    store.connect(host, username, password); 

    /* 
    * Folder[] f = store.getDefaultFolder().list(); for (Folder fd : f) 
    * System.out.println(">> " + fd.getName()); 
    */ 
    Folder folder = store.getFolder("INBOX"); 

    if (!folder.exists()) 
    { 
     System.out.println("No INBOX..."); 
     System.exit(0); 
    } 
    folder.open(Folder.READ_WRITE); 
    Message[] msg = folder.getMessages(); 

    if (msg.length < 1) 
    { 
     System.out.println("No new messages!"); 
     throw new Exception("No new messages!"); 
    } 

    for (int i = 0; i < msg.length; i++) 
    { 
     if (!msg[i].isSet(Flag.SEEN)) 
     { 
      EmailSenderInfo emailSenderInfo = new EmailSenderInfo(); 
      emailSenderInfo.message = msg[i]; 
      System.out.println("------------ Message " + (i + 1) + " ------------"); 

      // String from = InternetAddress.toString(msg[i].getFrom()); 
      Address[] fromArray = msg[i].getFrom(); 
      InternetAddress fromInternetAddress = (InternetAddress) fromArray[0]; 
      String from = fromInternetAddress.getAddress(); 
      String fromName = fromInternetAddress.getPersonal(); 

      if (fromName != null) 
      { 
       emailSenderInfo.fromName = fromName; 
      } 

      if (from != null) 
      { 
       // System.out.println("From: " + from); 
       emailSenderInfo.from = from; 
      } 

      // String replyTo = InternetAddress.toString(msg[i].getReplyTo()); 
      Address[] replyToArray = msg[i].getFrom(); 
      InternetAddress ReplyToInternetAddress = (InternetAddress) replyToArray[0]; 
      String replyTo = ReplyToInternetAddress.getAddress(); 
      String replyToName = ReplyToInternetAddress.getPersonal(); 

      if (replyTo != null) 
      { 
       // System.out.println("Reply-to: " + replyTo); 
       emailSenderInfo.replyTo = replyTo; 
      } 

      if (replyToName != null) 
      { 
       // System.out.println("Reply-to: " + replyTo); 
       emailSenderInfo.replyToName = replyToName; 
      } 

      String to = InternetAddress.toString(msg[i].getRecipients(Message.RecipientType.TO)); 
      if (to != null) 
      { 
       // System.out.println("To: " + to); 
       emailSenderInfo.to = to; 
      } 

      String subject = msg[i].getSubject(); 
      if (subject != null) 
      { 
       // System.out.println("Subject: " + subject); 
       emailSenderInfo.subject = subject; 
      } 

      Date sentDate = msg[i].getSentDate(); 
      if (sentDate != null) 
      { 
       System.out.println("Sent: " + sentDate); 
       emailSenderInfo.sentDate = sentDate; 
      } 

      String bodyHtml = ""; 

      // get content 
      Multipart multipart = (Multipart) msg[i].getContent(); 
      for (int x = 0; x < multipart.getCount(); x++) 
      { 

       BodyPart bodyPart = multipart.getBodyPart(x); 

       String disposition = bodyPart.getDisposition(); 

       if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) 
       { 
        // dealing with attachments only 
        System.out.println("Mail has some attachment : "); 
        DataHandler handler = bodyPart.getDataHandler(); 
        System.out.println("file name : " + handler.getName()); 
        System.out.println("ddddd: " + bodyPart.getContent().toString()); 
       } 
       else 
       { 
        String classType = bodyPart.getContent().getClass().toString(); 

        if (classType.contains("java.lang.String")) 
        { 
         bodyHtml = bodyPart.getContent().toString(); 
        } 
        else if (classType.contains("javax.mail.internet.MimeMultipart")) 
        { 
         MimeMultipart bodyContent = (MimeMultipart) bodyPart.getContent(); 
         for (int b = 0; b < bodyContent.getCount(); b++) 
         { 
          IMAPBodyPart imapBody = (IMAPBodyPart) bodyContent.getBodyPart(b); 
          System.out.println("1: " + imapBody.getContent()); 
          bodyHtml = imapBody.getContent().toString(); 
          // System.out.println("2: " + bodyContent.getBodyPart(b)); 
          // System.out.println("3: " + bodyPart.getContent().toString()); 
         } 
        } 
       } 
       emailSenderInfo.bodyHtml = bodyHtml; 
      } 


      MyMessage reply = (MyMessage) msg[i].reply(false); 
      emailSenderInfo.reply = reply; 
      // reply.setFrom(msg[i].getFrom()[0]); 
      MimeMessage orig = (MimeMessage) msg[i]; 
      StringBuffer buffer = new StringBuffer("Thanks\n\n"); 
      if (orig.isMimeType("text/plain")) 
      { 
       String content = (String) orig.getContent(); 
       StringReader contentReader = new StringReader(content); 
       BufferedReader br = new BufferedReader(contentReader); 
       String contentLine; 
       while ((contentLine = br.readLine()) != null) 
       { 
        buffer.append("> "); 
        buffer.append(contentLine); 
        buffer.append("\r\n"); 
       } 
      } 
      // Set the reply content 
      // reply.setText(buffer.toString()); 
      // emailSenderInfo.reply = reply; 

      emailSenderList.add(emailSenderInfo); 
      // System.out.println(); 
     }// end if unread 
    } 
    folder.close(true); 
    store.close(); 

    return emailSenderList; 
} 

我在我的程序的兩種方法。其中一人檢查郵件,另一人回覆郵件。

Message reply = msg[i].reply(false); 

「答覆」與其他參數一起傳遞給此方法。

我必須使用MyMessage類來刪除「Message-ID」標題。我曾嘗試以下

MyMessage mimeReply = (MyMessage) reply; 

但我得到的運行時出現以下錯誤

java.lang.ClassCastException: javax.mail.internet.MimeMessage cannot be cast to javamailer.MyMessage 

如何使用「MyMessage」類,所以我可以回覆郵件刪除「郵件ID」頭?

+0

考慮加入,你讀消息從會議 –

回答

0

你可以嘗試以下方法:

在答覆郵件:

當你創建一個消息創建,現在它應該是這樣的:

MimeMessage msg = new MimeMessage(session); 

將其更改爲,

MyMessage msg = new MyMessage(session); 

當檢查電子郵件您不必刪除標題,該消息已經在郵箱,我認爲你需要做的是在該實例回覆時郵件的名稱郵件實例化,如:

MyMessage msg = new MyMessage(session); 
msg.updateMessageID(); 

既然你使用參考現有的消息:

你可以這樣做:

創建一個新的構造:

public MyMessage (MimeMessage message) { 
    super(message); 
} 

回答時:

MyMessage mimeReply = new MyMessage(reply); 
mimeReply.updateMessageID(); 

然後發送mimeReply NOT回覆。

+0

不幸的是我得到了同樣的錯誤消息 – Arya

+0

你可以發佈的代碼,你在哪裏得到的異常,後堆棧跟蹤太 –

+0

我已經編輯職位,包括對checkEmail那部分代碼()方法。這就是你的建議後我改變了「MyMessage reply =(MyMessage)msg [i] .reply(false);」這就是錯誤發生的地方。 – Arya