正常发送邮件只需要集成javax.mail就可以了
javamail如果发送邮件自动保存到已发送邮件里面,是需要自己实现的。but pop3协议是实现不了的,必须要实现imap协议。
但在工作中遇到了使用的是微软的邮箱,只有pop3,没有imap。我又不能让他们去开启imap,只能自己解决。
然后看到了exchange,就尝试了下。
1、引入jar包
compile "com.microsoft.ews-java-api:ews-java-api:2.0"
2、实现client
import microsoft.exchange.webservices.data.core.ExchangeService;
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.property.complex.MessageBody;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
public class ExchangeClient {
private String hostname;
private String username;
private String password;
private String domain;
private ExchangeService service;
public ExchangeClient(String hostname, String username, String password) {
this.hostname = hostname;
this.username = username;
this.password = password;
this.service = getExchangeService();
}
public ExchangeClient(String hostname, String username, String password, String domain) {
this.hostname = hostname;
this.username = username;
this.password = password;
this.domain = domain;
this.service = getExchangeService();
}
private ExchangeService getExchangeService() {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
//用户认证信息
ExchangeCredentials credentials;
if (domain == null) {
credentials = new WebCredentials(username, password);
} else {
credentials = new WebCredentials(username, password, domain);
}
service.setCredentials(credentials);
try {
service.setUrl(new URI("https://" + hostname + "/ews/Exchange.asmx"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
return service;
}
/**
* 收取邮件
*
* @param max 最大收取邮件数
* @throws Exception
*/
public ArrayList<EmailMessage> receive(int max) throws Exception {
//绑定收件箱,同样可以绑定发件箱
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
//获取文件总数量
int count = inbox.getTotalCount();
if (max > 0) {
count = count > max ? max : count;
}
//循环获取邮箱邮件
ItemView view = new ItemView(count);
FindItemsResults<Item> findResults = service.findItems(inbox.getId(), view);
ArrayList<EmailMessage> result = new ArrayList<>();
for (Item item : findResults.getItems()) {
EmailMessage message = EmailMessage.bind(service, item.getId());
result.add(message);
}
return result;
}
/**
* 收取所有邮件
*
* @throws Exception
*/
public ArrayList<EmailMessage> receive() throws Exception {
return receive(0);
}
/**
* 发送带附件的mail
*
* @param subject 邮件标题
* @param to 收件人列表
* @param cc 抄送人列表
* @param bodyText 邮件内容
* @param attachmentPaths 附件地址列表
* @throws Exception
*/
public void send(String subject, List<String> to, List<String> cc, String bodyText, List<String> attachmentPaths) throws Exception {
EmailMessage msg = new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
// body.setBodyType(BodyType.HTML);
msg.setBody(body);
for (String toPerson : to) {
msg.getToRecipients().add(toPerson);
}
if (cc != null) {
for (String ccPerson : cc) {
msg.getCcRecipients().add(ccPerson);
}
}
if (attachmentPaths != null) {
for (String attachmentPath : attachmentPaths) {
msg.getAttachments().addFileAttachment(attachmentPath);
}
}
// msg.send();
msg.sendAndSaveCopy();
}
/**
* 发送不带抄送人的邮件
*
* @param subject 标题
* @param to 收件人列表
* @param bodyText 邮件内容
* @throws Exception
*/
public void send(String subject, List<String> to, String bodyText) throws Exception {
send(subject, to, null, bodyText);
}
/**
* 发送不带附件的mail
*
* @param subject 邮件标题
* @param to 收件人列表
* @param cc 抄送人列表
* @param bodyText 邮件内容
* @throws Exception
*/
public void send(String subject, List<String> to, List<String> cc, String bodyText) throws Exception {
send(subject, to, cc, bodyText, null);
}
}
3、成功实现
虽然成功了,但一开始并没有看到有sendAndSaveCopy方法,想着自己去实现保存,绕了一大圈
//仅仅发送
msg.send();
//发送并保存到发件箱
msg.sendAndSaveCopy();
public void send() throws Exception {
internalSend(null, MessageDisposition.SendOnly);
}
public void sendAndSaveCopy() throws Exception {
this.internalSend(new FolderId(WellKnownFolderName.SentItems),
MessageDisposition.SendAndSaveCopy);
}