16 Ocak 2018 Salı

JavaMail Message Sınıfı

Giriş
Şu satırı dahil ederiz. Soyut bir sınıftır
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Message.RecipientType;
getContentType metodu
Şöyle yaparız.
String contentType = msg.getContentType();
String messageContent = "";

if (contentType.contains("text/plain") || contentType.contains("text/html")) {
  try {
    Object content = msg.getContent();
    if (content != null) {
      messageContent = content.toString();
    }
  } catch (Exception ex) {
    messageContent = "[Error downloading content]";
    ex.printStackTrace();
  }
} else { 
  try {
    if (msg.getContent() instanceof Multipart) {
      Multipart content = (Multipart) msg.getContent();
      content.getCount();
      Part part = content.getBodyPart(0);
      InputStream is = part.getInputStream();
      ...
  } catch (IOException e) {
    messageContent = "[Error downloading content]";
    e.printStackTrace();
  }
}
getFrom metodu
Şöyle yaparız
Message msg = ...;
Address[] fromAddress = msg.getFrom();
getAllHeaders metodu
Şöyle yaparız.
javax.mail.Message msg = source.getMessage(1);
Enumeration<Header> header = msg.getAllHeaders();
Çıktı olarak şunu alırız.
Received: from VDLG-EXCH-01 with Microsoft SMTP Server
Received: from VDLG-EXCH-01
From: my@email.com
To: demo <demo@email.com>
Subject: FW: qqqq
Thread-Topic: qqqq
Thread-Index: AdOOCZ2g1WQYWPdLTH/Y8hua
Date: Tue, 16 Jan 2018 17:33:05 +0300
Message-ID: <931CD66D7759036FA@vdlg-exch-01.email.com>
References: <AD03856E3028A600@VDMS-EXCH-01.email.com>
In-Reply-To: <AA8D03856E3028A600@VDMS-EXCH-01.email.com>
Accept-Language: en-US, ru-RU
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 04
X-MS-Exchange-Organization-AuthSource: vdlg-exch-01.email.com
X-MS-Has-Attach: 
X-MS-Exchange-Organization-SCL: -1
X-MS-TNEF-Correlator: 
Content-Type: text/html; charset="us-ascii"
getRecipients metodu
Örnek
Şöyle yaparız.
Address [] toList = parseAddresses(msg.getRecipients(RecipientType.TO));
Örnek
Şöyle yaparız.
Address [] ccList = parseAddresses(msg.getRecipients(RecipientType.CC));
getSentDate metodu
Şöyle yaparız.
String sentDate = msg.getSentDate().toString();
getSubject metodu
Örnek
Şöyle yaparız.
String subject = msg.getSubject();
Örnek
Şöyle yaparız.
Message msg = ...;

if (message.getSubject() != null && message.getSubject().contains(...)) {  ...
}

Hiç yorum yok:

Yorum Gönder