การเชื่อมต่อกับ Exchange Server
เพื่อเชื่อมต่อกับ Exchange server 2007, 2010 และ 2013 โดยใช้ Exchange Web Service, Aspose.Email มี IEWSClient อินเทอร์เฟซที่ทำงานตาม EWSClient คลาส. เมธอด EWSClient.getEWSClient เมธอดสร้างอินสแตนซ์และคืนค่า IEWSClient ออบเจกต์ที่ใช้ต่อไปเพื่อทำการดำเนินการที่เกี่ยวกับกล่องเมล Exchange และโฟลเดอร์อื่น ๆ บทความนี้แสดงวิธีสร้างอินสแตนซ์ของออบเจกต์ IEWSClient.
เชื่อมต่อกับ Exchange Server ด้วย EWS
โค้ดส่วนต่อไปนี้จะแสดงวิธีเชื่อมต่อโดยใช้ Exchange Web Service (EWS).
private static IEWSClient getExchangeEWSClient() {
final String mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
final String domain = "";
final String username = "username@onmicrosoft.com";
final String password = "password";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
return client;
}
เชื่อมต่อกับ Exchange Server ด้วย IMAP
Microsoft Exchange Server รองรับโปรโตคอล IMAP สำหรับการเข้าถึงรายการในกล่องเมล ใช้ของ Aspose.Email’s ImapClient คลาสเพื่อเชื่อมต่อกับ Exchange Server ด้วยโปรโตคอล IMAP ข้อมูลเพิ่มเติมเกี่ยวกับ ImapClient คลาส ก่อนแรกให้แน่ใจว่า IMAP service เปิดใช้งานสำหรับ Exchange Server ของคุณ:
- เปิด Control Panel.
- ไปที่ Administrator Tools แล้วเลือก Services.
- ตรวจสอบสถานะของบริการ Microsoft Exchange IMAP4.
- หากยังไม่ได้ทำงาน ให้เปิดใช้งาน/เริ่มต้นมัน.
โค้ดส่วนต่อไปนี้จะแสดงวิธีเชื่อมต่อและแสดงรายการข้อความจากโฟลเดอร์ Inbox ของ Microsoft exchange server โดยใช้โปรโตคอล IMAP.
// Connect to Exchange Server using ImapClient class
ImapClient imapClient = new ImapClient("ex07sp1", "Administrator", "Evaluation1");
imapClient.setSecurityOptions(SecurityOptions.Auto);
// Select the Inbox folder
imapClient.selectFolder(ImapFolderInfo.IN_BOX);
// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.listMessages();
for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
System.out.println(msgInfo.getSubject());
}
// Disconnect from the server
imapClient.dispose();
โค้ดส่วนต่อไปนี้จะแสดงวิธีใช้ SSL.
public static void run() {
// Connect to Exchange Server using ImapClient class
ImapClient imapClient = new ImapClient("ex07sp1", 993, "Administrator", "Evaluation1");
imapClient.setSecurityOptions(SecurityOptions.SSLExplicit);
// Select the Inbox folder
imapClient.selectFolder(ImapFolderInfo.IN_BOX);
// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.listMessages();
for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
System.out.println(msgInfo.getSubject());
}
// Disconnect from the server
imapClient.dispose();
}
หลังจากเชื่อมต่อกับ Exchange server ด้วย IMAP และรับ IMapMessageInfoCollection, โค้ดส่วนต่อไปนี้จะแสดงวิธีใช้ MessageInfo ลำดับเลขของออบเจกต์เพื่อบันทึกข้อความเฉพาะ.
// Select the Inbox folder
imapClient.selectFolder(ImapFolderInfo.IN_BOX);
// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.listMessages();
for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
// Fetch the message from inbox using its SequenceNumber from msgInfo
MailMessage message = imapClient.fetchMessage(msgInfo.getSequenceNumber());
// Save the message to disc now
message.save(dataDir + msgInfo.getSequenceNumber() + "_out.msg", SaveOptions.getDefaultMsgUnicode());
}
ละเลยหรือข้ามใบรับรอง SSL ที่ไม่ถูกต้องหรือหมดอายุ
Aspose.Email เชื่อมต่อกับ Exchange Server ผ่าน HTTPS ดังนั้น JVM จะตรวจสอบใบรับรองเซิร์ฟเวอร์ก่อนทำคำขอ EWS ใด ๆ หากใบรับรองหมดอายุ ลงนามด้วยตัวเอง หรือหัวเรื่องไม่ตรงกับชื่อโฮสต์ การเชื่อมต่อจะล้มเหลวด้วย SSLHandshakeException.
วิธีแก้ที่สนับสนุนคือการติดตั้งใบรับรองเซิร์ฟเวอร์ (หรือ CA ที่ออกให้) ลงใน trust store ของ JVM:
keytool -importcert -alias exchange -file exchange.cer -keystore "%JAVA_HOME%\lib\security\cacerts"
สำหรับสภาพแวดล้อมทดสอบหรือพัฒนา ที่ไม่เป็นไปได้จริง คุณสามารถลงทะเบียน TrustManager และตัวตรวจสอบชื่อโฮสต์ก่อนสร้างไคลเอนต์ เรียกเมธอดนี้ครั้งเดียวระหว่างการเริ่มต้นแอปพลิเคชัน:
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
public class SslBypass {
public static void trustAllCertificates() throws Exception {
TrustManager[] trustAll = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
public void checkClientTrusted(X509Certificate[] chain, String authType) { }
public void checkServerTrusted(X509Certificate[] chain, String authType) { }
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAll, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
}
}