คุณสมบัติอรรถประโยชน์
การส่งข้อความพร้อมตัวเลือกการโหวต
Microsoft Outlook อนุญาตให้ผู้ใช้สร้างแบบสำรวจเมื่อเขียนข้อความใหม่. ทำได้โดยใส่ตัวเลือกการโหวตเช่น ใช่, ไม่ใช่, บางอย่าง ฯลฯ. คลาส FollowUpOptions ที่ Aspose.Email มีให้, มีคุณสมบัติ VotingButtons ที่ใช้ตั้งหรือรับค่าตัวเลือกการโหวต. บทความนี้ให้ตัวอย่างอย่างละเอียดของการสร้าง MapiMessage พร้อมตัวเลือกการโหวตเพื่อสร้างการสำรวจและส่งข้อความโดยใช้ Exchange Web Service (EWS) client.
การสร้างและส่งข้อความพร้อมตัวเลือกการโหวต
ส่วนของโค้ดต่อไปนี้แสดงวิธีการสร้างข้อความใหม่และส่งพร้อมตัวเลือกการโหวต.
String address = "firstname.lastname@aspose.com";
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
MailMessage message = createTestMessage(address);
// Set FollowUpOptions Buttons
FollowUpOptions options = new FollowUpOptions();
options.setVotingButtons("Yes;No;Maybe;Exactly!");
client.send(message, options);
เมธอดตัวอย่างที่ใช้ในตัวอย่าง
ส่วนของโค้ดต่อไปนี้แสดงวิธีใช้เมธอดที่ใช้ในตัวอย่างข้างต้น.
private static MailMessage createTestMessage(String address) {
MailMessage eml = new MailMessage(address, address, "Flagged message",
"Make it nice and short, but descriptive. The description may appear in search engines' search results pages...");
return eml;
}
สร้างข้อความ RE และ FW จากไฟล์ MSG
IEWSClient ทำให้นักพัฒนาสามารถสร้างข้อความ RE (Reply/Reply All) และ FW (Forward) จากข้อความต้นทาง. ข้อความต้นทางถูกระบุโดยการเลือก ExchangeMessageInfo จาก ExchangeMessageInfoCollection ได้มาจาก IEWSClient.listMessages(). อากิวเมนต์อีกอันคือค่าจริงของ MailMessage เพื่อส่งเป็นข้อความ RE หรือ FW. ส่วนของโค้ดต่อไปนี้แสดงวิธีสร้างบัญชีตัวอย่างที่ใช้ส่งข้อความและจากนั้นฟีเจอร์การตอบกลับและส่งต่อข้อความจะถูกสาธิตบนข้อความตัวอย่างนั้น. เพื่อทำงานนี้:
- เริ่มต้นอ็อบเจ็กต์ IEWSClient โดยระบุข้อมูลประจำตัวที่ถูกต้อง.
- ส่งข้อความตัวอย่างบางข้อความ.
- เรียกฟังก์ชัน IEWSClient.reply(), IEWSClient.replyAll() และ IEWSClient.forward() เพื่อส่งข้อความ.
IEWSClient client = EWSClient.getEWSClient(mailboxUri, credential);
try {
MailMessage message = new MailMessage("user@domain.com", "user@domain.com", "TestMailRefw - " + UUID.randomUUID().toString(),
"TestMailRefw Implement ability to create RE and FW messages from source MSG file");
client.send(message);
ExchangeMessageInfoCollection messageInfoCol = client.listMessages(client.getMailboxInfo().getInboxUri());
if (messageInfoCol.size() == 1)
System.out.println("1 message in Inbox");
else
System.out.println("Error! No message in Inbox");
MailMessage message1 = new MailMessage("user@domain.com", "user@domain.com", "TestMailRefw - " + UUID.randomUUID().toString(),
"TestMailRefw Implement ability to create RE and FW messages from source MSG file");
client.send(message1);
messageInfoCol = client.listMessages(client.getMailboxInfo().getInboxUri());
if (messageInfoCol.size() == 2)
System.out.println("2 messages in Inbox");
else
System.out.println("Error! No new message in Inbox");
MailMessage message2 = new MailMessage("user@domain.com", "user@domain.com", "TestMailRefw - " + UUID.randomUUID().toString(),
"TestMailRefw Implement ability to create RE and FW messages from source MSG file");
message2.getAttachments().addItem(Attachment.createAttachmentFromString("Test attachment 1", "Attachment Name 1"));
message2.getAttachments().addItem(Attachment.createAttachmentFromString("Test attachment 2", "Attachment Name 2"));
// Reply, Replay All and forward Message
client.reply(message2, messageInfoCol.get_Item(0));
client.replyAll(message2, messageInfoCol.get_Item(0));
client.forward(message2, messageInfoCol.get_Item(0));
} catch (java.lang.RuntimeException ex) {
System.out.println("Error in program" + ex.getMessage());
}
การสนับสนุน OAuth สำหรับ EWS กับ Office 365
Aspose.Email API ให้การสนับสนุนสำหรับ Exchange Web Service (EWS) กับ Office 365. API’s EWSClient อินเทอร์เฟซให้เมธอด overload ที่ให้ OAuthNetworkCredential เป็นอินพุตสำหรับการเข้าถึงบัญชี Exchange ผ่าน OAuth. ผู้ใช้ต้องระบุ Authority, Client Id, Client App Uri, และพารามิเตอร์ Resource เพื่อให้ทำงานได้. ส่วนของโค้ดต่อไปนี้แสดงการสนับสนุน OAuth สำหรับ EWS กับ Office 365.
// token provider
/*ITokenProvider provider = new ITokenProvider() {
@Override
public void dispose() {
}
@Override
public OAuthToken getAccessToken(boolean ignoreExistingToken) {
return null;
}
@Override
public OAuthToken getAccessToken() {
return null;
}
};
NetworkCredential credentials = new OAuthNetworkCredential(provider);*/
// accessToken
NetworkCredential credentials = new OAuthNetworkCredential("accessToken");
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", credentials);
client.listMessages();
รองรับการบันทึกในคลายันต์ Exchange
Aspose.Email API มีความสามารถในการให้บริการบันทึกของคลายันต์ Exchange Web Service.
การบันทึกสำหรับ EWS Client
client.setLogFileName("logs/ews");
// OR
EWSClient.setCommonLogFileName("logs/ews");
การเพิ่มส่วนหัวในคำขอ EWS
Aspose.Email API อนุญาตให้เพิ่มส่วนหัวในคำขอ Exchange. สามารถใช้เพื่อเพิ่มส่วนหัวในคำขอ EWS สำหรับส่วนหัวต่าง ๆ ที่ใช้เพื่อวัตถุต่าง ๆ. ตัวอย่างหนึ่งคือการเพิ่มส่วนหัว X-AnchorMailbox ที่ใช้จัดการปัญหา throttling บนเซิร์ฟเวอร์ Exchange. เมธอด addHeader ของ IEWSClient ใช้ในการเพิ่มส่วนหัวในคำขอ EWS ตามที่แสดงในส่วนของโค้ดต่อไปนี้.
final IEWSClient client = EWSClient.getEWSClient("exchange.domain.com/ews/Exchange.asmx", "username", "password", "");
try {
client.addHeader("X-AnchorMailbox", "username@domain.com");
ExchangeMessageInfoCollection messageInfoCol = client.listMessages(client.getMailboxInfo().getInboxUri());
} finally {
client.dispose();
}
ส่งคืน Client Request ID ในส่วนหัว
นี้ return-client-request-id ส่วนหัวถูกส่งในคำขอและใช้โดยเซิร์ฟเวอร์เพื่อตัดสินใจว่า client-request-id ส่วนหัวที่ระบุโดยคลายันต์ควรถูกส่งกลับในการตอบสนองของเซิร์ฟเวอร์. เมธอดต่อไปนี้ใช้ในการดำเนินการนี้:
- getReturnClientRequestId()
- setReturnClientRequestId(boolean value) - รับหรือกำหนดค่าแฟล็กเพื่อระบุว่าคลายันต์ต้องการให้เซิร์ฟเวอร์ส่งคืน request id หรือไม่.
try (IEWSClient client = createEWSClient())
{
// Client will create random id and pass it to the server.
// The server should include this id in request-id header of all responses.
client.setReturnClientRequestId(true);
client.setLogFileName("ews.log");
client.getMailboxInfo();
}
การทำงานกับการสื่อสารแบบรวม
Aspose.Email สามารถเรียกคืนข้อมูลการสื่อสารแบบรวมจาก Exchange Server 2010. การสื่อสารแบบรวมเช่น การรับข้อมูลการกำหนดค่า, เริ่มการโทรออก, ดึงข้อมูลการโทรตาม ID การโทรและตัดการโทรตาม ID ได้รับการสนับสนุนในปัจจุบัน. ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการดึงข้อมูลการกำหนดค่าการสื่อสารแบบรวมจาก Microsoft Exchange Server 2010.
IEWSClient client = EWSClient.getEWSClient(mailboxUri, credential);
UnifiedMessagingConfiguration umConf = client.getUMConfiguration();
รับเคล็ดลับอีเมล
Microsoft Exchange Server เพิ่มคุณลักษณะใหม่หลายอย่างใน Exchange Server 2010 และ 2013. หนึ่งในนั้นทำให้ผู้ใช้ได้รับเคล็ดลับอีเมลขณะเขียนข้อความอีเมล. เคล็ดลับเหล่านี้มีประโยชน์เพราะให้ข้อมูลก่อนส่งอีเมล. ตัวอย่างเช่น หากที่อยู่อีเมลในรายการผู้รับไม่ถูกต้อง ระบบจะแสดงเคล็ดลับบอกว่าที่อยู่นั้นไม่ถูกต้อง. เคล็ดลับอีเมลยังให้คุณเห็นการตอบกลับ “นอกสำนักงาน” ก่อนส่งอีเมล: Exchange Server (2010 & 2013) จะส่งเคล็ดลับอีเมลขณะเขียนหากผู้รับคนหนึ่งหรือหลายคนตั้งค่าการตอบกลับนอกสำนักงาน. ต้องใช้ Microsoft Exchange Server 2010 Service Pack 1 สำหรับคุณลักษณะทั้งหมดที่แสดงในบทความนี้. ส่วนของโค้ดต่อไปนี้แสดงวิธีการใช้ EWSClient คลาสที่ใช้ Exchange Web Services, มีให้ใน Microsoft Exchange Server 2007 และรุ่นต่อ ๆ ไป.
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
System.out.println("Connected to Exchange server...");
// Provide mail tips options
MailAddressCollection addrColl = new MailAddressCollection();
addrColl.addMailAddress(new MailAddress("test.exchange@ex2010.local", true));
addrColl.addMailAddress(new MailAddress("invalid.recipient@ex2010.local", true));
GetMailTipsOptions options = new GetMailTipsOptions(MailAddress.to_MailAddress("administrator@ex2010.local"), addrColl, MailTipsType.All);
// Get Mail Tips
MailTips[] tips = client.getMailTips(options);
// Display information about each Mail Tip
for (MailTips tip : tips) {
// Display Out of office message, if present
if (tip.getOutOfOffice() != null) {
System.out.println("Out of office: " + tip.getOutOfOffice().getReplyBody().getMessage());
}
// Display the invalid email address in recipient, if present
if (tip.getInvalidRecipient() == true) {
System.out.println("The recipient address is invalid: " + tip.getRecipientAddress());
}
}
การแสดงตนของ Exchange
การแสดงตนของ Exchange อนุญาตให้บุคคลหนึ่งแสดงตนเป็นบัญชีอื่นและดำเนินงานต่าง ๆ ด้วยสิทธิของบัญชีที่แสดงตนแทนของตนเอง. ในขณะที่การมอบอำนาจทำให้ผู้ใช้ทำงานในนามของผู้ใช้คนอื่น, การแสดงตนทำให้พวกเขาเป็นผู้ใช้คนอื่น. Aspose.Email รองรับการแสดงตนของ Exchange. EWSClient คลาสนี้ให้เมธอด ImpersonateUser และ ResetImpersonation เพื่ออำนวยความสะดวกสำหรับฟีเจอร์นี้.
เพื่อทำงานนี้:
- เริ่มต้น ExchangeWebServiceClient สำหรับผู้ใช้ 1.
- เริ่มต้น ExchangeWebServiceClient สำหรับผู้ใช้ 2.
- เพิ่มข้อความทดสอบไปยังบัญชี.
- เปิดใช้งานการแสดงตน.
- รีเซ็ตการแสดงตน.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการใช้ EWSClient คลาสสำหรับทำฟีเจอร์การแสดงตน (Impersonation).
// Create instance's of EWSClient class by giving credentials
IEWSClient client1 = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser1", "pwd", "domain");
IEWSClient client2 = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser2", "pwd", "domain");
{
String folder = "Drafts";
try {
for (ExchangeMessageInfo messageInfo : (Iterable<ExchangeMessageInfo>) client1.listMessages(folder))
client1.deleteItem(messageInfo.getUniqueUri(), DeletionOptions.getDeletePermanently());
String subj1 = "NETWORKNET_33354 User User1";
client1.appendMessage(folder, new MailMessage("User1@exchange.conholdate.local", "To@aspsoe.com", subj1, ""));
for (ExchangeMessageInfo messageInfo : (Iterable<ExchangeMessageInfo>) client2.listMessages(folder))
client2.deleteItem(messageInfo.getUniqueUri(), DeletionOptions.getDeletePermanently());
String subj2 = "NETWORKNET_33354 User User2";
client2.appendMessage(folder, new MailMessage("User2@exchange.conholdate.local", "To@aspose.com", subj2, ""));
ExchangeMessageInfoCollection messInfoColl = client1.listMessages(folder);
// Assert.AreEqual(1, messInfoColl.Count);
// Assert.AreEqual(subj1, messInfoColl[0].Subject);
client1.impersonateUser(ItemChoice.PrimarySmtpAddress, "User2@exchange.conholdate.local");
ExchangeMessageInfoCollection messInfoColl1 = client1.listMessages(folder);
// Assert.AreEqual(1, messInfoColl1.Count);
// Assert.AreEqual(subj2, messInfoColl1[0].Subject);
client1.resetImpersonation();
ExchangeMessageInfoCollection messInfoColl2 = client1.listMessages(folder);
// Assert.AreEqual(1, messInfoColl2.Count);
// Assert.AreEqual(subj1, messInfoColl2[0].Subject);
} finally {
try {
for (ExchangeMessageInfo messageInfo : (Iterable<ExchangeMessageInfo>) client1.listMessages(folder))
client1.deleteItem(messageInfo.getUniqueUri(), DeletionOptions.getDeletePermanently());
for (ExchangeMessageInfo messageInfo : (Iterable<ExchangeMessageInfo>) client2.listMessages(folder))
client2.deleteItem(messageInfo.getUniqueUri(), DeletionOptions.getDeletePermanently());
} catch (java.lang.RuntimeException e) {
}
}
}
ฟีเจอร์ Auto Discover ด้วย EWS
Aspose.Email API ช่วยให้คุณค้นพบการตั้งค่า Exchange Server ด้วยการใช้ EWS Client
AutodiscoverService svc = new AutodiscoverService();
svc.setCredentials(new NetworkCredential(email, password));
IGenericDictionary</* UserSettingName */Integer, Object> userSettings = svc.getUserSettings(email, UserSettingName.ExternalEwsUrl).getSettings();
String ewsUrl = (String) userSettings.get_Item(UserSettingName.ExternalEwsUrl);
System.out.println("Auto discovered EWS Url: " + ewsUrl);
ยกเลิกการกู้คืน PST ไปยัง Exchange Server
Aspose.Email API ช่วยให้คุณกู้คืนไฟล์ PST ไปยัง Exchange Server. อย่างไรก็ตาม หากการดำเนินการใช้เวลานานเนื่องจากไฟล์ PST มีขนาดใหญ่ อาจจำเป็นต้องระบุเกณฑ์สำหรับการยกเลิกการดำเนินการ. สามารถทำได้โดยใช้ API ตามที่แสดงในตัวอย่างโค้ดต่อไปนี้.
final IEWSClient client = EWSClient.getEWSClient("https://exchange.office365.com/ews/exchange.asmx", "username", "password");
try {
final long startTime = System.currentTimeMillis();
final AtomicInteger processedItems = new AtomicInteger(0);
final long S15 = 15 * 1000;
BeforeItemCallback callback = new BeforeItemCallback() {
public void invoke(ItemCallbackArgs item) {
if (System.currentTimeMillis() - startTime > S15) {
throw new AbortRestoreException();
}
processedItems.incrementAndGet();
}
};
try {
// create a test pst and add some test messages to it
PersonalStorage pst = PersonalStorage.create(new ByteArrayOutputStream(), FileFormatVersion.Unicode);
FolderInfo folder = pst.getRootFolder().addSubFolder("My test folder");
for (int i = 0; i < 20; i++) {
MapiMessage message = new MapiMessage("from@gmail.com", "to@gmail.com", "subj", "body");
folder.addMessage(message);
}
RestoreSettings rs = new RestoreSettings();
rs.setBeforeItemCallback(callback);
// now restore the PST with callback
client.restore(pst, rs);
System.out.println("Success!");
} catch (AbortRestoreException e) {
System.out.println("Timeout! " + processedItems.get());
}
} finally {
client.dispose();
}