Exchange发送邮件开发

用到的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");
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值