不废话,开整
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
import microsoft.exchange.webservices.data.core.ExchangeService;
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.AttachmentCollection;
import microsoft.exchange.webservices.data.property.complex.EmailAddressCollection;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
import java.io.*;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class EwsSendEmail {
//ews-api新建发送邮件
public static void main(String[] args) {
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr=sf.format(new Date());
try {
System.out.println(dateStr+"发邮件开始====================");
ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials("ad", "pwd","domain");
service.setCredentials(credentials);
service.setUrl(new URI("https://xxxx.com/ews/exchange.asmx"));
service.setTraceEnabled(true);
EmailMessage email = new EmailMessage(service);
//邮件标题
email.setSubject("邮件标题");
//邮件详情
String uuid= UUID.randomUUID().toString().toLowerCase();
//普通邮件详情(两种邮件详情选其一)
email.setBody(new MessageBody("<div>邮件详情,发自XXX</div><div><br></div>"));
//带内联图片邮件详情(两种邮件详情选其一)
email.setBody(new MessageBody("<div><img src=\"cid:"+uuid+"\">邮件详情发自XXX</div><div><br></div>"));
//收件人
EmailAddressCollection toCollection = email.getToRecipients();
toCollection.add("xxx@xxx.xxx.com");
//抄送
EmailAddressCollection ccCollection = email.getCcRecipients();
ccCollection.add("xxx@xxx.xxx.com");
//密送
EmailAddressCollection bccCollection = email.getBccRecipients();
bccCollection.add("xxx@xxx.xxx.com");
//附件
AttachmentCollection attachmentCollection = email.getAttachments();
File file=new File("C:/Users/admin/Desktop/工作/新建 XLSX 工作表.xlsx");
attachmentCollection.addFileAttachment(file.getName(), FileTobyte(file));
//内联附件
int count=attachmentCollection.getItems().size();//当前邮件已经存在多少附件
File file2=new File("C:/Users/admin/Desktop/工作/图片/111.png");
attachmentCollection.addFileAttachment(file.getName(), FileTobyte(file2));
attachmentCollection.getItems().get(count).setIsInline(true);//设置当前附件为内联
attachmentCollection.getItems().get(count).setContentId(uuid);//设置当前附件id
email.sendAndSaveCopy();//发送并保存副本到已发送文件夹,如不需要保存则send()
System.out.println(dateStr+"发邮件结束====================");
} catch (Exception e) {
System.out.println(dateStr+"发邮件异常====================");
e.printStackTrace();
}
}
public static byte[] FileTobyte(File file) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
}
注意:普通邮件详情和带内联图片邮件详情选择其一。
有疑问欢迎留言。