记录下发送outlook邮件
Exchange Web服务(EWS)Java API提供了一个托管接口,
中文文档地址为
首先项目中下载在pom.xml添加jar
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
1 然后编写发送邮件代码
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
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;
import java.net.URI;
public class SendMailTest {
public static void main(String[] args) throws Exception {
ExchangeService service = new ExchangerServiceSSLVerify(ExchangeVersion.Exchange2010_SP2);
String userName = ""; //用户名
String password = ""; //密码
String domain = ""; //域
String url = ""; //邮件服务器地址
ExchangeCredentials credentials = new WebCredentials(userName, password, domain);
service.setCredentials(credentials);
service.setUrl(new URI("https://" + "邮箱服务器地址" + "/EWS/Exchange.asmx"));
EmailMessage msg = new EmailMessage(service);
msg.setSubject("标题");
msg.setBody(MessageBody.getMessageBodyFromText("测试内容"));
msg.getToRecipients().add("xxx@xx.com.cn"); //接受人
msg.getCcRecipients().add("xxxx@xx.com.cn"); //抄送
msg.send(); //发送现有电子邮件
}
}
2编写接受邮件功能
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.PropertySet;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.service.folder.Folder;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;
import java.net.URI;
/**
* @author 疯信子
* @version 1.0
* @date 2018/11/28.
*/
public class ReadMailTest {
public static void main(String[] args) throws Exception {
ExchangeService service = new ExchangerServiceSSLVerify(ExchangeVersion.Exchange2010_SP2);
String userName = ""; //用户名
String password = ""; //密码
String domain = ""; //域
String url = ""; //邮件服务器地址
ExchangeCredentials credentials = new WebCredentials(userName, password, domain);
service.setCredentials(credentials);
service.setUrl(new URI("https://" + "邮箱服务器地址" + "/EWS/Exchange.asmx"));
Folder inbox=Folder.bind(service, WellKnownFolderName.Inbox);
ItemView view=new ItemView(1);
FindItemsResults<Item> findResults = service.findItems(inbox.getId(), view);
service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
for (Item item : findResults.getItems()) {
EmailMessage message = EmailMessage.bind(service, item.getId());
System.out.println("发送人-->"+message.getSender());
System.out.println("Sub -->" + item.getSubject());
System.out.println("内容-->"+item.getBody().toString());
}
}
}
3 忽略证书验证,使用HttpClient访问HTTPS接口
import microsoft.exchange.webservices.data.EWSConstants;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.GeneralSecurityException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class ExchangerServiceSSLVerify extends ExchangeService {
public ExchangerServiceSSLVerify(ExchangeVersion requestedServerVersion) {
super(requestedServerVersion);
}
@Override
protected Registry<ConnectionSocketFactory> createConnectionSocketFactoryRegistry() {
try {
SSLContext sslContext = createSSL();
return RegistryBuilder.<ConnectionSocketFactory>create()
.register(EWSConstants.HTTP_SCHEME, PlainConnectionSocketFactory.INSTANCE)
.register(EWSConstants.HTTPS_SCHEME, new SSLConnectionSocketFactory(sslContext))
.build();
} catch (GeneralSecurityException e) {
throw new RuntimeException(
"Could not initialize ConnectionSocketFactory instances for HttpClientConnectionManager", e
);
}
}
private SSLContext createSSL() throws NoSuchAlgorithmException, KeyManagementException {
//Secure Protocol implementation.
SSLContext ctx = SSLContext.getInstance("SSL");
//Implementation of a trust manager for X509 certificates
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
return ctx;
}
}
以上就可以完成邮件发送和接受。
注意邮件服务器使用的https地址,需要忽略https证书.