1 packagecom.fzw.exchangeClient;2
3 importjava.net.URI;4 importjava.net.URISyntaxException;5 importjava.util.ArrayList;6 importjava.util.Arrays;7 importjava.util.List;8
9 importlombok.extern.slf4j.Slf4j;10 importmicrosoft.exchange.webservices.data.core.ExchangeService;11 importmicrosoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;12 importmicrosoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;13 importmicrosoft.exchange.webservices.data.core.service.item.EmailMessage;14 importmicrosoft.exchange.webservices.data.credential.ExchangeCredentials;15 importmicrosoft.exchange.webservices.data.credential.WebCredentials;16 importmicrosoft.exchange.webservices.data.property.complex.MessageBody;17
18 @Slf4j19 public classExchangeClient {20
21
22 private finalString hostname;23 private finalExchangeVersion exchangeVersion;24 private finalString domain;25 private finalString username;26 private finalString password;27 private finalString subject;28 private finalString recipientTo;29 private final ListrecipientCc;30 private final ListrecipientBcc;31 private final Listattachments;32 private finalString message;33
34 privateExchangeClient(ExchangeClientBuilder builder) {35 this.hostname =builder.hostname;36 this.exchangeVersion =builder.exchangeVersion;37 this.domain =builder.domain;38 this.username =builder.username;39 this.password =builder.password;40 this.subject =builder.subject;41 this.recipientTo =builder.recipientTo;42 this.recipientCc =builder.recipientCc;43 this.recipientBcc =builder.recipientBcc;44 this.attachments =builder.attachments;45 this.message =builder.message;46 }47
48 public static classExchangeClientBuilder {49
50 privateString hostname;51 privateExchangeVersion exchangeVersion;52 privateString domain;53 privateString username;54 privateString password;55 privateString subject;56 privateString recipientTo;57 private ListrecipientCc;58 private ListrecipientBcc;59 private Listattachments;60 privateString message;61
62 publicExchangeClientBuilder() {63 this.exchangeVersion =ExchangeVersion.Exchange2010_SP1;64 this.hostname = "";65 this.username = "";66 this.password = "";67 this.subject = "";68 this.recipientTo = "";69 this.recipientCc = new ArrayList<>(0);70 this.recipientBcc = new ArrayList<>(0);71 this.attachments = new ArrayList<>(0);72 this.message = "";73 }74
75
82 publicExchangeClientBuilder hostname(String hostname) {83 this.hostname =hostname;84 return this;85 }86
87
93 publicExchangeClientBuilder exchangeVersion(ExchangeVersion exchangeVersion) {94 this.exchangeVersion =exchangeVersion;95 return this;96 }97
98
105 publicExchangeClientBuilder domain(String domain) {106 this.domain =domain;107 return this;108 }109
110
117 publicExchangeClientBuilder username(String username) {118 this.username =username;119 return this;120 }121
122
128 publicExchangeClientBuilder password(String password) {129 this.password =password;130 return this;131 }132
133
139 publicExchangeClientBuilder subject(String subject) {140 this.subject =subject;141 return this;142 }143
144
150 publicExchangeClientBuilder recipientTo(String recipientTo) {151 this.recipientTo =recipientTo;152 return this;153 }154
155
163 publicExchangeClientBuilder recipientCc(String recipientCc, String... recipientsCc) {164 //Prepare the list.
165 List recipients = new ArrayList<>(1 +recipientsCc.length);166 recipients.add(recipientCc);167 recipients.addAll(Arrays.asList(recipientsCc));168 //Set the list.
169 this.recipientCc =recipients;170 return this;171 }172
173
181 public ExchangeClientBuilder recipientCc(ListrecipientCc) {182 this.recipientCc =recipientCc;183 return this;184 }185
186
194 publicExchangeClientBuilder recipientBcc(String recipientBcc, String... recipientsBcc) {195 //Prepare the list.
196 List recipients = new ArrayList<>(1 +recipientsBcc.length);197 recipients.add(recipientBcc);198 recipients.addAll(Arrays.asList(recipientsBcc));199 //Set the list.
200 this.recipientBcc =recipients;201 return this;202 }203
204
212 public ExchangeClientBuilder recipientBcc(ListrecipientBcc) {213 this.recipientBcc =recipientBcc;214 return this;215 }216
217
225 publicExchangeClientBuilder attachments(String attachment, String... attachments) {226 //Prepare the list.
227 List attachmentsToUse = new ArrayList<>(1 +attachments.length);228 attachmentsToUse.add(attachment);229 attachmentsToUse.addAll(Arrays.asList(attachments));230 //Set the list.
231 this.attachments =attachmentsToUse;232 return this;233 }234
235
243 public ExchangeClientBuilder attachments(Listattachments) {244 this.attachments =attachments;245 return this;246 }247
248
254 publicExchangeClientBuilder message(String message) {255 this.message =message;256 return this;257 }258
259
264 publicExchangeClient build() {265 return new ExchangeClient(this);266 }267 }268
269 public booleansendExchange() {270 //The Exchange Server Version.
271 ExchangeService exchangeService = newExchangeService(exchangeVersion);272
273 //Credentials to sign in the MS Exchange Server.
274 ExchangeCredentials exchangeCredentials = newWebCredentials(username, password, domain);275 exchangeService.setCredentials(exchangeCredentials);276 // https://mall.dfmc.com.cn:25@dfmc.com.cn/ews/Exchange.asmx277 //URL of exchange web service for the mailbox.
278 try{279 exchangeService.setUrl(new URI("https://" + hostname + "/ews/Exchange.asmx"));280 } catch(URISyntaxException ex) {281 System.out.println("An exception occured while creating the uri for exchange service."+ex);282
283 return false;284 }285
286 //The email.
287 EmailMessage emailMessage;288 try{289 emailMessage = newEmailMessage(exchangeService);290 emailMessage.setSubject(subject);291 emailMessage.setBody(MessageBody.getMessageBodyFromText(message));292 } catch(Exception ex) {293 System.out.println("An exception occured while setting the email message."+ex);294 return false;295 }296
297 //TO recipient.
298 try{299 emailMessage.getToRecipients().add(recipientTo);300 } catch(ServiceLocalException ex) {301 System.out.println("An exception occured while setting the email message."+ex);302 return false;303 }304
305 //CC recipient.
306 for(String recipient : recipientCc) {307 try{308 emailMessage.getCcRecipients().add(recipient);309 } catch(ServiceLocalException ex) {310 System.out.println("An exception occured while setting the email message."+ex);311 return false;312 }313 }314
315 //BCC recipient
316 for(String recipient : recipientBcc) {317 try{318 emailMessage.getBccRecipients().add(recipient);319 } catch(ServiceLocalException ex) {320 System.out.println("An exception occured while setting the email message."+ex);321 return false;322 }323 }324
325 //Attachements.
326 for(String attachmentPath : attachments) {327 try{328 emailMessage.getAttachments().addFileAttachment(attachmentPath);329 } catch(ServiceLocalException ex) {330 System.out.println("An exception occured while setting the email message."+ex);331 return false;332 }333 }334
335 try{336 emailMessage.send();337 System.out.println("An email is send.");338 } catch(Exception ex) {339 System.out.println(""+ex);340 return false;341 }342
343 return true;344 }345
346 }



