不废话,开整文章来源:https://www.toymoban.com/news/detail-506586.html
依赖
<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;
}
}
注意:普通邮件详情和带内联图片邮件详情选择其一。
有疑问欢迎留言。文章来源地址https://www.toymoban.com/news/detail-506586.html
到了这里,关于java使用ews-java-api发送邮件(包括附件和内联图片处理)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!