【问题标题】:Sending Email using Javamail via Exchange通过 Exchange 使用 Javamail 发送电子邮件
【发布时间】:2020-10-02 16:23:59
【问题描述】:

我在使用公司交换服务器通过 Javamail 发送电子邮件时遇到了一些问题。我们有一个应用程序通过 gmail 服务器发送电子邮件没有任何问题,但是对于谷歌政策的一些变化,我们希望使用公司服务器来完成这项工作。 我确定会话属性中的问题,但我找不到让它工作的方法

    Properties props = new Properties();
    props.put("mail.smtp.port", 465);
    props.put("mail.smtp.socketFactory.port", 465);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.host", _server);

    session = Session.getInstance(props, this);
    try {
        transport = session.getTransport("smtp");
        transport.connect("mail.company.com",_user,_pass);
        transport.close();

这是显示日志的错误

javax.mail.MessagingException:无法连接到 SMTP 主机:mail.company.com,端口:443; 嵌套异常是: avax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: 找不到证书路径的信任锚。

【问题讨论】:

  • 443 是 HTTPS 标准端口。好像你的 465 端口没有被使用。

标签: java android email jakarta-mail


【解决方案1】:

您必须检查您的电子邮件提供商及其 SMTP 设置;服务器、端口和加密方法。

下面的代码 sn -p 对我有用

        //1) get the session object     
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        // You have missed this line.
        properties.put("mail.smtp.starttls.enable", "true");
        // This SMTP server works with me for all Microsoft email providers, like: -
        // Outlook, Hotmail, Live, MSN, Office 365 and Exchange.
        properties.put("mail.smtp.host", "smtp.live.com");
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.user", user);
        properties.put("mail.smtp.pwd", password);

        Session session = Session.getInstance(properties, null);
        session.setDebug(true); // To trace the code implementation.

        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.live.com", 587, user, password);
        transport.close();

而不是

    props.put("mail.smtp.port", 465);
    props.put("mail.smtp.socketFactory.port", 465);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.host", _server);

    session = Session.getInstance(props, this);
    try {
        transport = session.getTransport("smtp");
        transport.connect("mail.company.com",_user,_pass);
        transport.close();

我发现 this website 在获取其他电子邮件提供商的 SMTP 设置信息方面非常有用。

【讨论】:

  • 我使用了错误的端口,在与我的服务器管理员交谈后,他说正确的端口是“25”。我现在正在努力进行身份验证。就像服务器不读取用户/传递数据一样
  • 您可以使用上面的代码来解决问题,然后检查它是否仍然存在,您必须确保输入正确的用户(您的电子邮件地址)和密码( 您的电子邮件密码);如果不是,通常会发生异常。如果它仍然存在,您必须确保您的电子邮件 SMTP 服务器设置。顺便问一下,您的电子邮件提供商是什么?微软交易所?如果您不知道,请将您公司电子邮件的登录页面链接提供给我,我将查找其 SMTP 服务器设置,然后我将编辑答案。
猜你喜欢
  • 2017-12-18
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
  • 2013-11-12
  • 2018-12-18
相关资源
最近更新 更多