JavaMail Exchange身份validation

我正在尝试使用JavaMail从我的应用程序中使用Exchange身份validation来执行此操作。 有人可以给我一个指导吗? 身份validation后,我需要发送邮件,这是我使用JavaMail的主要原因。 我发现的所有链接都谈到了这方面的问题,但我认为这必须是一个简单的Java任务。 提前致谢。

身份validation后,我需要发送邮件

以下示例适用于Exchange服务器:

Properties properties = new Properties(); properties.put("mail.transport.protocol", "smtp"); properties.put("mail.smtp.host", "mail.example.com"); properties.put("mail.smtp.port", "2525"); properties.put("mail.smtp.auth", "true"); final String username = "username"; final String password = "password"; Authenticator authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }; Transport transport = null; try { Session session = Session.getDefaultInstance(properties, authenticator); MimeMessage mimeMessage = createMimeMessage(session, mimeMessageData); transport = session.getTransport(); transport.connect(username, password); transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); } finally { if (transport != null) try { transport.close(); } catch (MessagingException logOrIgnore) {} } 

这是一个很好的问题! 我已经解决了这个问题。

首先,您应该导入jar ews-java-api-2.0.jar 。 如果你使用maven,你可以将以下代码添加到你的pom.xml

  com.microsoft.ews-java-api ews-java-api 2.0  

其次,您应该使用名为MailUtil.java新java类。默认情况下,某些Exchange Server不启动SMTP服务,因此我们使用Microsoft Exchange WebServices(EWS)代替SMTP服务。

MailUtil.java

 package com.spacex.util; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URI; /** * Exchange send email util * * @author vino.dang * @create 2017/01/08 */ public class MailUtil { private static Logger logger = LoggerFactory.getLogger(MailUtil.class); /** * send emial * @return */ public static boolean sendEmail() { Boolean flag = false; try { ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); // your server version ExchangeCredentials credentials = new WebCredentials("vino", "abcd123", "spacex"); // change them to your email username, password, email domain service.setCredentials(credentials); service.setUrl(new URI("https://outlook.spacex.com/EWS/Exchange.asmx")); //outlook.spacex.com change it to your email server address EmailMessage msg = new EmailMessage(service); msg.setSubject("This is a test!!!"); //email subject msg.setBody(MessageBody.getMessageBodyFromText("This is a test!!! pls ignore it!")); //email body msg.getToRecipients().add("123@hotmail.com"); //email receiver // msg.getCcRecipients().add("test2@test.com"); // email cc recipients // msg.getAttachments().addFileAttachment("D:\\Downloads\\EWSJavaAPI_1.2\\EWSJavaAPI_1.2\\Getting started with EWS Java API.RTF"); // email attachment msg.send(); //send email flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; } public static void main(String[] args) { sendEmail(); } } 

如果您想获得更多细节,请参阅https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide

适合我:

 Properties props = System.getProperties(); // Session configuration is done using properties. In this case, the IMAP port. All the rest are using defaults props.setProperty("mail.imap.port", "993"); // creating the session to the mail server Session session = Session.getInstance(props, null); // Store is JavaMails name for the entity holding the mails Store store = session.getStore("imaps"); // accessing the mail server using the domain user and password store.connect(host, user, password); // retrieving the inbox folder Folder inbox = store.getFolder("INBOX"); 

此代码基于示例代码到达并下载java邮件。

Microsoft发布了一个用于连接到Exchange Web Service的开源API

https://github.com/OfficeDev/ews-java-api

Exchange默认情况下不启动SMTP服务,因此我们无法使用SMTP protocol连接到Exchange服务器并尝试发送电子邮件。 BalusC可以正常使用上面的代码,因为您的邮件服务器管理员在Exchange上启用了SMTP服务。在大多数情况下,SMTP被禁用。我也在寻找解决方案。

这是我所发现的最好的答案,但是在60天之后你需要付出代价是多么令人沮丧。

某些Exchange服务器未启用smtp协议。
在这些情况下,您可以使用DavMail 。

尝试了ews-java-api,正如Populus在之前的评论中提到的那样。 它是在带有jdk1.6的Java SE环境中完成的,它就像一个魅力。
这些是我必须与我的样本相关联的库:

  • 公共-CLI-1.2.jar
  • 公地编解码器1.10.jar
  • 公地lang3-3.1.jar
  • 共享记录-1.2.jar
  • EWS-Java的API-2.0.jar
  • HttpClient的-4.4.1.jar
  • 的HttpCore-4.4.5.jar

希望能帮助到你。