Exchange Server पर नियमों के साथ काम करना
नियम प्रबंधित करना
Aspose.Email for Java का उपयोग Exchange Server पर नियमों को प्रबंधित करने के लिए किया जा सकता है, उपयोग करके EWSClient क्लास। यह क्लास Exchange Web Services (EWS) का उपयोग करती है, जो Exchange Server 2007 और बाद के संस्करणों में उपलब्ध हैं। नियम प्रबंधन को दिखाने के लिए, यह लेख बताता है कि कैसे:
- सर्वर पर पहले से मौजूद नियमों को पढ़ें।
- नया नियम बनाएं।
- मौजूदा नियम को अपडेट करें।
इस लेख में वर्णित सभी सुविधाओं के लिए Microsoft Exchange Server 2010 Service Pack 1 आवश्यक है।
नियम पढ़ें
Exchange Server से सभी नियम प्राप्त करने के लिए:
- IEWSClient क्लास का उपयोग करके Exchange Server से कनेक्ट करें।
- सभी नियमों को प्राप्त करने के लिए IEWSClient.getInboxRules() मेथड कॉल करें।
- एक foreach लूप में, सभी नियमों को ब्राउज़ करें और नियम गुणधर्म दिखाएँ जैसे शर्तें, क्रियाएँ, और नाम।
निम्नलिखित कोड स्निपेट दिखाता है कि नियमों को कैसे पढ़ा जाए।
IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);
System.out.println("Connected to Exchange server");
// Get all Inbox Rules
InboxRule[] inboxRules = client.getInboxRules();
// Display information about each rule
for (InboxRule inboxRule : inboxRules) {
System.out.println("Display Name: " + inboxRule.getDisplayName());
// Check if there is a "From Address" condition
if (inboxRule.getConditions().getFromAddresses().size() > 0) {
for (MailAddress fromAddress : (Iterable<MailAddress>) inboxRule.getConditions().getFromAddresses()) {
System.out.println("From: " + fromAddress.getDisplayName() + " - " + fromAddress.getAddress());
}
}
// Check if there is a "Subject Contains" condition
if (inboxRule.getConditions().containsSubjectStrings().size() > 0) {
// foreach to while statements conversion
for (String subject : inboxRule.getConditions().containsSubjectStrings()) {
System.out.println("Subject contains: " + subject);
}
}
// Check if there is a "Move to Folder" action
if (inboxRule.getActions().getMoveToFolder().length() > 0) {
System.out.println("Move message to folder: " + inboxRule.getActions().getMoveToFolder());
}
}
नया नियम बनाना
Exchange Server पर नया नियम बनाने के लिए निम्नलिखित चरणों को पूरा करें:
- IEWSClient क्लास का उपयोग करके Exchange Server से कनेक्ट करें।
- InboxRule क्लास का एक नया इंस्टेंस बनाएं और नीचे दी गई अनिवार्य गुणधर्म सेट करें:
- DisplayName
- शर्तें
- क्रियाएँ
- नियम बनाने के लिए IEWSClient.createInboxRule() मेथड कॉल करें।
निम्नलिखित कोड स्निपेट दिखाता है कि नया नियम कैसे बनाया जाए।
IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);
System.out.println("Connected to Exchange server");
InboxRule rule = new InboxRule();
rule.setDisplayName("Message from client ABC");
// Add conditions
RulePredicates newRules = new RulePredicates();
// Set Subject contains string "ABC" and Add the conditions
newRules.containsSubjectStrings().addItem("ABC");
newRules.getFromAddresses().addMailAddress(new MailAddress("administrator@ex2010.local", true));
rule.setConditions(newRules);
// Add Actions and Move the message to a folder
RuleActions newActions = new RuleActions();
newActions.setMoveToFolder("120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA==");
rule.setActions(newActions);
client.createInboxRule(rule);
नियम अपडेट करना
Exchange Server पर नियम को अपडेट करने के लिए:
- IEWSClient क्लास का उपयोग करके Exchange Server से कनेक्ट करें।
- सभी नियमों को प्राप्त करने के लिए IEWSClient.getInboxRules() मेथड कॉल करें।
- एक foreach लूप में, सभी नियमों को ब्राउज़ करें और उस नियम को प्राप्त करें जिसे आप स्थिति में DisplayName मिलाकर बदलना चाहते हैं।
- नियम गुणधर्म अपडेट करें
- नियम को अपडेट करने के लिए IEWSClient.updateInboxRule() मेथड कॉल करें।
निम्नलिखित कोड स्निपेट दिखाता है कि नियम को कैसे अपडेट करें।
IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);
System.out.println("Connected to Exchange server");
// Get all Inbox Rules
InboxRule[] inboxRules = client.getInboxRules();
// Loop through each rule
for (InboxRule inboxRule : inboxRules) {
System.out.println("Display Name: " + inboxRule.getDisplayName());
if ("Message from client ABC".equals(inboxRule.getDisplayName())) {
System.out.println("Updating the rule....");
inboxRule.getConditions().getFromAddresses().set_Item(0, new MailAddress("administrator@ex2010.local", true));
client.updateInboxRule(inboxRule);
}
}