Last active
August 29, 2015 14:21
-
-
Save acharlieh/37be1cef38143dcac6c8 to your computer and use it in GitHub Desktop.
Using EWS Java API to set custom headers on a draft email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URI; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
//You'll need: https://github.com/OfficeDev/ews-java-api/ | |
import microsoft.exchange.webservices.data.core.ExchangeService; | |
import microsoft.exchange.webservices.data.core.PropertySet; | |
import microsoft.exchange.webservices.data.core.service.folder.Folder; | |
import microsoft.exchange.webservices.data.core.service.item.Item; | |
import microsoft.exchange.webservices.data.core.service.schema.EmailMessageSchema; | |
import microsoft.exchange.webservices.data.core.service.schema.ItemSchema; | |
import microsoft.exchange.webservices.data.credential.WebCredentials; | |
import microsoft.exchange.webservices.data.enumeration.ComparisonMode; | |
import microsoft.exchange.webservices.data.enumeration.ConflictResolutionMode; | |
import microsoft.exchange.webservices.data.enumeration.ContainmentMode; | |
import microsoft.exchange.webservices.data.enumeration.DefaultExtendedPropertySet; | |
import microsoft.exchange.webservices.data.enumeration.ExchangeVersion; | |
import microsoft.exchange.webservices.data.enumeration.MapiPropertyType; | |
import microsoft.exchange.webservices.data.enumeration.SortDirection; | |
import microsoft.exchange.webservices.data.enumeration.WellKnownFolderName; | |
import microsoft.exchange.webservices.data.property.complex.MessageBody; | |
import microsoft.exchange.webservices.data.property.definition.ExtendedPropertyDefinition; | |
import microsoft.exchange.webservices.data.search.FindItemsResults; | |
import microsoft.exchange.webservices.data.search.ItemView; | |
import microsoft.exchange.webservices.data.search.filter.SearchFilter; | |
public class EmailTweaker { | |
private static final ExchangeVersion EV = ExchangeVersion.Exchange2010_SP2; | |
private static final URI EWS_URI = URI.create("https://email.example.com/EWS/Exchange.asmx"); | |
private static final String DOMAIN = "EXAMPLE_DOMAIN"; | |
public static void main(String... args) throws Exception { | |
if (args.length != 2) { | |
System.err.println("Supply only username and password as arguments please"); | |
System.exit(1); | |
} | |
String username = args[0]; | |
String password = args[1]; | |
try (ExchangeService service = new ExchangeService(EV)) { | |
service.setUrl(EWS_URI); | |
service.setCredentials(new WebCredentials(username, password, DOMAIN)); | |
new EmailTweaker(service).run(); | |
} | |
} | |
private final ExchangeService service; | |
private EmailTweaker(ExchangeService service) { | |
this.service = service; | |
} | |
private final int PAGE_SIZE = 100; | |
private static final Pattern HEADERS=Pattern.compile("(?<=(?:>|^))SetHeader:\\s*([!-9;-~]+):\\s*(.+?)(?=(?:<|$))"); | |
private static final SearchFilter FILTER = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "HEADERS ", ContainmentMode.Prefixed, ComparisonMode.Exact); | |
private void run() throws Exception { | |
Folder folder = Folder.bind(service, WellKnownFolderName.Drafts); | |
ItemView view = new ItemView(PAGE_SIZE); | |
view.setPropertySet(PropertySet.getIdOnly()); | |
view.getOrderBy().add(EmailMessageSchema.DateTimeSent, | |
SortDirection.Ascending); | |
FindItemsResults<Item> results; | |
do { | |
results = folder.findItems(FILTER, view); | |
for (Item item : results) { | |
// Sadly, the view won't let us just load all of these. | |
item.load(new PropertySet(ItemSchema.Body,ItemSchema.Subject)); | |
MessageBody body = item.getBody(); | |
String bodyString = body.toString(); | |
Matcher bodyMatcher = HEADERS.matcher(bodyString); | |
StringBuffer sb = new StringBuffer(bodyString.length()); | |
while(bodyMatcher.find()) { | |
String key = bodyMatcher.group(1); | |
String value = bodyMatcher.group(2); | |
System.out.printf("Setting %s:%s\n",key,value); | |
ExtendedPropertyDefinition header = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.InternetHeaders, | |
key, | |
MapiPropertyType.String); | |
item.setExtendedProperty(header, value); | |
bodyMatcher.appendReplacement(sb, ""); | |
} | |
bodyMatcher.appendTail(sb); | |
String newBodyString = sb.toString(); | |
//Dealing with HTML bodies, sometimes Outlook / OWA sucks and splits lines. | |
//When this happens, it helps to see the raw message body so you can edit and try again. | |
if(!newBodyString.equals(bodyString)) { | |
//Even if you get here, not all of the custom headers may be put in place. | |
item.setSubject(item.getSubject().substring(8)); | |
body.setText(sb.toString()); | |
item.update(ConflictResolutionMode.AlwaysOverwrite); | |
System.out.println(item.getId()); | |
} else { | |
System.err.printf("Subject: %s ,Id: %s\n\t%s\n\n",item.getSubject(),item.getId(),newBodyString); | |
} | |
} | |
} while (results.isMoreAvailable()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment