用到的jar包
package sendmail.util;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.log4j.Logger;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
/**
* 邮箱服务器工具类
* @author
* Exchange Web Services Java API
*/
public class ExchangeMailUtil {
static final Logger LOGGER = Logger.getLogger(sendmail.util.ExchangeMailUtil.class);
private String mailServer;
private String user;
private String password;
public ExchangeMailUtil(String mailServer, String user, String password) {
super();
this.mailServer = mailServer;
this.user = user;
this.password = password;
}
public static void sendMail(String to, String subject, String body) {
LOGGER.info("send mail to " + to + " with subject " + subject + " and body " + body);
String mailServer = "";
String user = "";
String password = "";
ExchangeMailUtil mailUtil = new ExchangeMailUtil(mailServer, user, password);
try {
mailUtil.send(subject, to, body);
} catch (Exception e) {
LOGGER.info("sendMail error {} ", e);
}
}
/**
* 创建邮件服务
*
* @return 邮件服务
*/
private ExchangeService getExchangeService() {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
// 用户认证信息
ExchangeCredentials credentials;
credentials = new WebCredentials(user, password);
service.setCredentials(credentials);
try {
service.setUrl(new URI(mailServer));
} catch (URISyntaxException e) {
LOGGER.info("getExchangeService error {} ", e);
}
return service;
}
/**
* @param subject 邮件标题
* @param to 收件人列表
* @param bodyText 邮件内容
* @throws Exception
*/
public void send(String subject, String to, String bodyText) throws Exception {
ExchangeService service = getExchangeService();
System.out.println("service:"+service);
EmailMessage msg = new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
body.setBodyType(BodyType.HTML);
msg.setBody(body);
msg.getToRecipients().add(to);
msg.send();
}
}
package sendmail.test;
import sendmail.util.ExchangeMailUtil;
public class Test {
public static void main(String[] args) throws Exception {
ExchangeMailUtil mailUtil =
new ExchangeMailUtil("https://mailservice/EWS/exchange.asmx", "username", "pwd");
mailUtil.send("Subject", "fssctest2@cnpcint.com", "content");
System.out.println("success");
}
}