Exchange郵箱發送案例(親測可用)


為了方便jar包,本案例為Maven項目

  • POM.xml 其中Maven主要依賴為 ews-java-api 其他為spring boot 配置
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.6.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.fzw</groupId>
12     <artifactId>demo</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>demo</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>com.microsoft.ews-java-api</groupId>
24             <artifactId>ews-java-api</artifactId>
25             <version>2.0</version>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter</artifactId>
30         </dependency>
31 
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-devtools</artifactId>
35             <scope>runtime</scope>
36             <optional>true</optional>
37         </dependency>
38         <dependency>
39             <groupId>org.projectlombok</groupId>
40             <artifactId>lombok</artifactId>
41             <optional>true</optional>
42         </dependency>
43         <dependency>
44             <groupId>org.springframework.boot</groupId>
45             <artifactId>spring-boot-starter-test</artifactId>
46             <scope>test</scope>
47         </dependency>
48         <dependency>
49             <groupId>org.springframework.boot</groupId>
50             <artifactId>spring-boot-starter-web</artifactId>
51         </dependency>
52     </dependencies>
53 
54     <build>
55         <plugins>
56             <plugin>
57                 <groupId>org.springframework.boot</groupId>
58                 <artifactId>spring-boot-maven-plugin</artifactId>
59             </plugin>
60         </plugins>
61     </build>
62 
63 </project>
  • 項目結構

 

 

 

  •  ExchangeClient.class

  

  1 package com.fzw.exchangeClient;
  2 
  3 import java.net.URI;
  4 import java.net.URISyntaxException;
  5 import java.util.ArrayList;
  6 import java.util.Arrays;
  7 import java.util.List;
  8 
  9 import lombok.extern.slf4j.Slf4j;
 10 import microsoft.exchange.webservices.data.core.ExchangeService;
 11 import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
 12 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
 13 import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
 14 import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
 15 import microsoft.exchange.webservices.data.credential.WebCredentials;
 16 import microsoft.exchange.webservices.data.property.complex.MessageBody;
 17 
 18 @Slf4j
 19 public class ExchangeClient {
 20 
 21 
 22     private final String hostname;
 23     private final ExchangeVersion exchangeVersion;
 24     private final String domain;
 25     private final String username;
 26     private final String password;
 27     private final String subject;
 28     private final String recipientTo;
 29     private final List<String> recipientCc;
 30     private final List<String> recipientBcc;
 31     private final List<String> attachments;
 32     private final String message;
 33 
 34     private ExchangeClient(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 class ExchangeClientBuilder {
 49 
 50         private String hostname;
 51         private ExchangeVersion exchangeVersion;
 52         private String domain;
 53         private String username;
 54         private String password;
 55         private String subject;
 56         private String recipientTo;
 57         private List<String> recipientCc;
 58         private List<String> recipientBcc;
 59         private List<String> attachments;
 60         private String message;
 61 
 62         public ExchangeClientBuilder() {
 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         /**
 76          * The hostname of the Exchange Web Service. It will be used for
 77          * connecting with URI https://hostname/ews/exchange.asmx
 78          *
 79          * @param hostname the hostname of the MS Exchange Smtp Server.
 80          * @return the builder for chain usage.
 81          */
 82         public ExchangeClientBuilder hostname(String hostname) {
 83             this.hostname = hostname;
 84             return this;
 85         }
 86 
 87         /**
 88          * The Exchange Web Server version.
 89          *
 90          * @param exchangeVersion the Exchange Web Server version.
 91          * @return the builder for chain usage.
 92          */
 93         public ExchangeClientBuilder exchangeVersion(ExchangeVersion exchangeVersion) {
 94             this.exchangeVersion = exchangeVersion;
 95             return this;
 96         }
 97 
 98         /**
 99          * The domain of the MS Exchange Smtp Server.
100          *
101          * @param domain the domain of the Active Directory. The first part of
102          * the username. For example: MYDOMAIN\\username, set the MYDOMAIN.
103          * @return the builder for chain usage.
104          */
105         public ExchangeClientBuilder domain(String domain) {
106             this.domain = domain;
107             return this;
108         }
109 
110         /**
111          * The username of the MS Exchange Smtp Server. The second part of the
112          * username. For example: MYDOMAIN\\username, set the username.
113          *
114          * @param username the username of the MS Exchange Smtp Server.
115          * @return the builder for chain usage.
116          */
117         public ExchangeClientBuilder username(String username) {
118             this.username = username;
119             return this;
120         }
121 
122         /**
123          * The password of the MS Exchange Smtp Server.
124          *
125          * @param password the password of the MS Exchange Smtp Server.
126          * @return the builder for chain usage.
127          */
128         public ExchangeClientBuilder password(String password) {
129             this.password = password;
130             return this;
131         }
132 
133         /**
134          * The subject for this send.
135          *
136          * @param subject the subject for this send.
137          * @return the builder for chain usage.
138          */
139         public ExchangeClientBuilder subject(String subject) {
140             this.subject = subject;
141             return this;
142         }
143 
144         /**
145          * The recipient for this send.
146          *
147          * @param recipientTo the recipient for this send.
148          * @return the builder for chain usage.
149          */
150         public ExchangeClientBuilder recipientTo(String recipientTo) {
151             this.recipientTo = recipientTo;
152             return this;
153         }
154 
155         /**
156          * You can specify one or more email address that will be used as cc
157          * recipients.
158          *
159          * @param recipientCc the first cc email address.
160          * @param recipientsCc the other cc email address for this send.
161          * @return the builder for chain usage.
162          */
163         public ExchangeClientBuilder recipientCc(String recipientCc, String... recipientsCc) {
164             // Prepare the list.
165             List<String> 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         /**
174          * You can specify a list with email addresses that will be used as cc
175          * for this email send.
176          *
177          * @param recipientCc the list with email addresses that will be used as
178          * cc for this email send.
179          * @return the builder for chain usage.
180          */
181         public ExchangeClientBuilder recipientCc(List<String> recipientCc) {
182             this.recipientCc = recipientCc;
183             return this;
184         }
185 
186         /**
187          * You can specify one or more email address that will be used as bcc
188          * recipients.
189          *
190          * @param recipientBcc the first bcc email address.
191          * @param recipientsBcc the other bcc email address for this send.
192          * @return the builder for chain usage.
193          */
194         public ExchangeClientBuilder recipientBcc(String recipientBcc, String... recipientsBcc) {
195             // Prepare the list.
196             List<String> 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         /**
205          * You can specify a list with email addresses that will be used as bcc
206          * for this email send.
207          *
208          * @param recipientBcc the list with email addresses that will be used
209          * as bcc for this email send.
210          * @return the builder for chain usage.
211          */
212         public ExchangeClientBuilder recipientBcc(List<String> recipientBcc) {
213             this.recipientBcc = recipientBcc;
214             return this;
215         }
216 
217         /**
218          * You can specify one or more email address that will be used as cc
219          * recipients.
220          *
221          * @param attachment the first attachment.
222          * @param attachments the other attachments for this send.
223          * @return the builder for chain usage.
224          */
225         public ExchangeClientBuilder attachments(String attachment, String... attachments) {
226             // Prepare the list.
227             List<String> 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         /**
236          * You can specify a list with email attachments that will be used for
237          * this email send.
238          *
239          * @param attachments the list with email attachments that will be used
240          * for this email send.
241          * @return the builder for chain usage.
242          */
243         public ExchangeClientBuilder attachments(List<String> attachments) {
244             this.attachments = attachments;
245             return this;
246         }
247 
248         /**
249          * The body of the email message.
250          *
251          * @param message the body of the email message.
252          * @return the builder for chain usage.
253          */
254         public ExchangeClientBuilder message(String message) {
255             this.message = message;
256             return this;
257         }
258 
259         /**
260          * Build a mail.
261          *
262          * @return an EmailApacheUtils object.
263          */
264         public ExchangeClient build() {
265             return new ExchangeClient(this);
266         }
267     }
268 
269     public boolean sendExchange() {
270         // The Exchange Server Version.
271         ExchangeService exchangeService = new ExchangeService(exchangeVersion);
272 
273         // Credentials to sign in the MS Exchange Server.
274         ExchangeCredentials exchangeCredentials = new WebCredentials(username, password, domain);
275         exchangeService.setCredentials(exchangeCredentials);
276        // https://mall.dfmc.com.cn:25@dfmc.com.cn/ews/Exchange.asmx
277         // 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 = new EmailMessage(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 }
  • EmailController.class  

      

 1 package com.fzw.controller;
 2 
 3 
 4 import com.fzw.exchangeClient.ExchangeClient;
 5 import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
 6 import org.springframework.web.bind.annotation.GetMapping;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 
11 @RestController
12 @RequestMapping("/email")
13 public class EmailController {
14 
15     //賬號
16     private static String account = "";
17     //密碼
18     private static String pass = "";
19     //地址
20     private static String host = "";
21 
22     @GetMapping(path = "sendEmail")
23     public Boolean sendEmail(){ ;
24         System.out.println("Sending");
25         ExchangeClient client = new ExchangeClient.ExchangeClientBuilder()
26                 .hostname(host)
27                 .exchangeVersion(ExchangeVersion.Exchange2010)
28                 .username(account)
29                 .password(pass)
30                 .recipientTo("2219342140@qq.com")
31                 .subject("123345")
32                 .message("123456789")
33                 .build();
34         client.sendExchange();
35         System.out.println("Dones");
36         return Boolean.TRUE;
37     }
38 }

 

運行springboot項目后  即可測試   通過訪問   ip:端口號/email/sendEmail    即可測試 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM