繁体   English   中英

使用javamail发送邮件时出现问题

[英]Problem sending mail with javamail

我正在尝试使用javamail API向自己发送电子邮件。 我已经正确地遵循了在网上找到的代码,但是似乎无法正常工作。 我得到的错误是:

Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client

这是否意味着我的计算机不支持身份验证?

public class Emails 
{
    public static void main(String[] args) throws MessagingException
    {
        Emails e = new Emails();
        e.sendMail();
    }

    public void sendMail() throws MessagingException
    {
        boolean debug = false;
        String subject = "Testing";
        String message = "Testing Message";
        String from = "myemail@email.com";
        String[] recipients = {"myemail@email.com"};

        // create some properties and get the default Session
        Session session = getSession();
        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // Optional : You can also set your custom headers in the Email if you Want
        msg.addHeader("MyHeaderName", "myHeaderValue");

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }

    private Session getSession() 
    {
        Authenticator authenticator = new Authenticator();

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
        properties.setProperty("mail.smtp.auth", "true");

        properties.setProperty("mail.smtp.host", "smtp.examplehost.com");
        properties.setProperty("mail.smtp.port", "25");

        return Session.getInstance(properties, authenticator);
    }

    private class Authenticator extends javax.mail.Authenticator 
    {
        private PasswordAuthentication authentication;

        public Authenticator() 
        {
            String username = "myusername";
            String password = "mypassword";
            authentication = new PasswordAuthentication(username, password);
        }

        protected PasswordAuthentication getPasswordAuthentication() 
        {
            return authentication;
        }
    }
}

当我从该服务器发送电子邮件时,我要做的是通过ssh登录到服务器(login.server.com等),然后可以从邮件服务器(smtp.server.com等)发送电子邮件。 我需要在Java中模仿

解决:使用SMTPSSLTransport类

我有同样的问题,有一次没有在stackoverflow上找到答案。 @JPC的答案提供了一个(很小的)线索。

从javamail 1.4.3(请参阅参考资料)开始,javamail将拒绝通过未加密的通道发送纯文本凭证。 因此,启用TLS传输是一种解决方案。 如果服务器支持,则另一种解决方案是尝试另一种身份验证方法,例如NTLM或SASL。 另一个解决方案是还原为JavaMail 1.4.1,它毫无疑问地发送凭证。 这样做的好处是您的代码不必更改。 问题中的代码可以正常工作。 缺点显然是您以明文形式发送凭据,但是如果您的Exchange管理员允许,则...在我的情况下,邮件服务器不支持TLS,因此我别无选择。

参考文献:

确保您有一个smtp服务器(smtp.examplehost.com)可以实际发送电子邮件。 确保您的smtp服务器允许“ myemail@email.com”发送电子邮件。

这些是标准smtp服务器进行的一些安全检查。

这可能是您敲错了端口。 我想您正在使用IMAP协议:对于基于SSL的IMAP,您必须连接到端口993

SMTPSSLTransport解决了问题

暂无
暂无

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

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