How does Java send email through exchange protocol
This article mainly explains "how Java sends email through exchange protocol". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how Java sends email through exchange protocol".
Pom.xml Import package
Com.microsoft.ews-java-api ews-java-api 2.0
Application.properties configuration Information
# email address youjia.exchange.mail.username=123@abc.com# mailbox password youjia.exchange.mail.password=123456# mailbox exchange service address, if you do not know to find the operation and maintenance youjia.exchange.mail.host= https://*****/ews/exchange.asmx
Code
Package com.youjia.found.manager;import com.youjia.found.common.util.Check;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 Import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import javax.mail.internet.InternetAddress;import java.net.URI; / *
Exchange mail handling class
* * @ author eric * @ date on 2020-2-6 11:08 AM * @ since * / @ Componentpublic class MailExchangeManager {private final Logger logger = LoggerFactory.getLogger (this.getClass ()); @ Value ("${youjia.exchange.mail.username}") private String username; @ Value ("${youjia.exchange.mail.password}") private String password; @ Value ("${youjia.exchange.mail.host}") private String host / * * send * @ param to recipient * @ param subject email subject * @ param content body * @ param filePath attachment * * @ throws Exception * / public boolean sendMail (String to, String subject, String content, String filePath) {logger.info ("exchange email send to: {}, subject: {}, content: {}, filePath: {}", to, subject, content,filePath); boolean isOK=false ExchangeService service = new ExchangeService (ExchangeVersion.Exchange2007_SP1); ExchangeCredentials credentials = new WebCredentials (username,password); service.setCredentials (credentials); try {service.setUrl (new URI (host)); EmailMessage msg = new EmailMessage (service); msg.setSubject (subject); MessageBody body = MessageBody.getMessageBodyFromText (content); body.setBodyType (BodyType.HTML); msg.setBody (body); / / support for multiple recipients InternetAddress [] addresses = InternetAddress.parse (to) For (InternetAddress address: addresses) {msg.getToRecipients () .add (address.getAddress ());} if (Check.notEmpty (filePath)) {msg.getAttachments () .addFileAttachment (filePath);} msg.send (); isOK=true;} catch (Exception e) {logger.error (e.getMessage (), e); isOK= false;} return isOK }} Thank you for your reading, the above is the content of "how Java sends mail through exchange protocol". After the study of this article, I believe you have a deeper understanding of how Java sends mail through exchange protocol, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!