【问题标题】:JavaMail Exchange AuthenticationJavaMail Exchange 身份验证
【发布时间】:2009-11-11 04:57:13
【问题描述】:

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

【问题讨论】:

    标签: java authentication web-applications exchange-server jakarta-mail


    【解决方案1】:

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

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

    <dependency>
      <groupId>com.microsoft.ews-java-api</groupId>
      <artifactId>ews-java-api</artifactId>
      <version>2.0</version>
    </dependency>
    

    其次,你应该新建一个名为MailUtil.java的java类。一些Exchange服务器默认不启动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

    【讨论】:

    • 535 5.7.3 Authentication unsuccessful 出现此错误时只需检查您使用的是 Microsoft Exchange 用户还是普通用户。
    • @amilarajans 你应该把网址、用户、密码替换为自己
    • @Dang 谢谢你!你刚刚为我节省了很多时间。您可以在答案中添加指向 EWS Java API 文档的链接:github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide
    • @Spen,也谢谢你。我已将它添加到 EWS Java API 文档的常见问题中。请帮我检查一下并告诉我它是否正确。
    • @Dang 哦,我的意思是将文档链接添加到此 stackoverflow 答案中。
    【解决方案2】:

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

    • commons-cli-1.2.jar
    • commons-codec-1.10.jar
    • commons-lang3-3.1.jar
    • commons-logging-1.2.jar
    • ews-java-api-2.0.jar
    • httpclient-4.4.1.jar
    • httpcore-4.4.5.jar

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

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

      This 是我找到的最佳答案,但令人沮丧的是您必须在 60 天后付款。

      【讨论】:

        【解决方案4】:

        验证后我需要发送邮件

        以下示例在 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) {}
        }
        

        【讨论】:

        • 感谢您的回答。我在 Outlook 中使用相同的用户名和密码,并且正在工作。线程“主”javax.mail.MessagingException 中的异常:无法连接到 SMTP 主机:XXX.XXX.XXX.XXX,端口:2525;嵌套异常:java.net.ConnectException:连接超时:在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java: 453) 在 javax.mail.Service.connect(Service.java:291) 在 javax.mail.Service.connect(Service.java:172) 在 javax.mail.Service.connect(Service.java:192) 有什么想法吗?
        • 需要将端口号改为服务器的实际端口号。通常是 25,但也可以不同。有关详细信息,请咨询邮件服务器的管理员。很抱歉没有在消息中提到这一点,我认为这已经足够明显了。
        【解决方案5】:

        为我工作:

        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邮件下载附带的示例代码。

        【讨论】:

        • 感谢您的回答。我不明白你的代码你能评论你的代码吗?
        【解决方案6】:

        微软发布了用于连接 Exchange Web 服务的开源 API

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

        【讨论】:

        • 其实微软已经发布了。
        • 我的意思是微软哈哈
        • 看到这个错误。某些 SSL 证书不存在??有没有办法绕过它? microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException:请求失败。请求失败。 sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          上面建议的包基本上已经结束了。

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

          从 2018 年 7 月 19 日开始,Exchange Web 服务 (EWS) 将不再接收功能更新。虽然该服务将继续接收安全更新和某些非安全更新,但产品设计和功能将保持不变。此更改也适用于 Java 和 .NET 的 EWS SDK。更多信息在这里:https://developer.microsoft.com/en-us/graph/blogs/upcoming-changes-to-exchange-web-services-ews-api-for-office-365/

          【讨论】:

            【解决方案9】:

            已解决

            只需将以下内容添加到您的 pom.xml 依赖项中

                <dependency>
                    <groupId>javax.xml.ws</groupId>
                    <artifactId>jaxws-api</artifactId>
                    <version>2.3.1</version>
                </dependency>
            

            似乎 JAVA 11+ 中缺少 jaxws-api

            【讨论】:

              猜你喜欢
              • 2015-11-18
              • 2011-05-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-05-14
              相关资源
              最近更新 更多