EWS JAVA API
EWSJavaAPI_1.2.jar Guide : https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide
依赖包
commons-httpclient-3.1.jarcommons-codec-1.10.jarcommons-logging-1.2.jarjcifs-1.3.17.jar
代码示例
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map.Entry;
import microsoft.exchange.webservices.data.BodyType;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.WebCredentials;
public class MailUtils {
private String mailServer =
"";
private String user =
"";
private String password =
"";
public MailUtils(String mailServer, String user, String password){
this.mailServer = mailServer;
this.user = user;
this.password = password;
}
/**
* 发送带附件的mail
*/
public void doSend(String subject, String[] to, String[] cc, String bodyText, String[] attachmentPath)
throws Exception {
ExchangeService service =
new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeCredentials credentials =
new WebCredentials(user, password);
service.setCredentials(credentials);
try {
service.setUrl(
new URI(mailServer));
}
catch (URISyntaxException e) {
e.printStackTrace();
}
EmailMessage msg =
new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
body.setBodyType(BodyType.HTML);
msg.setBody(body);
for (String s : to) {
msg.getToRecipients().add(s);
}
if (cc !=
null) {
for (String s : cc) {
msg.getCcRecipients().add(s);
}
}
if (attachmentPath !=
null && attachmentPath.length >
0) {
for (
int a =
0; a < attachmentPath.length; a++) {
msg.getAttachments().addFileAttachment(attachmentPath[a]);
}
}
msg.send();
}
/**
* 发送不带附件的mail
*/
public void send(String subject, String[] to, String[] cc, String bodyText)
throws Exception {
doSend(subject, to, cc, bodyText,
null);
}
}
转载请注明原文地址: https://ju.6miu.com/read-962796.html