简体   繁体   English

通过 Exchange 使用 Javamail 发送 Email

[英]Sending Email using Javamail via Exchange

I'm having some troubles sending emails via Javamail using my company exchange server.我在使用公司交换服务器通过 Javamail 发送电子邮件时遇到了一些问题。 We have an application that was sending emails via the gmail server without any problems, but for some changes in the policies of Google we want to use the company server to do the job.我们有一个应用程序通过 gmail 服务器发送电子邮件没有任何问题,但是对于谷歌政策的一些变化,我们希望使用公司服务器来完成这项工作。 Im sure the problem in the session properties, but i cant find a way to make it work我确定 session 属性中的问题,但我找不到让它工作的方法

    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();

This is the error is showing the log这是显示日志的错误

javax.mail.MessagingException: Could not connect to SMTP host: mail.company.com, port: 443; javax.mail.MessagingException:无法连接到 SMTP 主机:mail.company.com,端口:443; nested exception is: avax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.嵌套异常是:avax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。

You have to check your email provider and its SMTP settings;您必须检查您的 email 提供程序及其 SMTP 设置; server, port and encryption method.服务器、端口和加密方法。

The following code snippet works with me以下代码片段适用于我

Put

        //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();

instead of代替

    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();

I found this website so helpful, in getting other email providers SMTP settings information.我发现这个网站对获取其他 email 提供商 SMTP 设置信息很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM