简体   繁体   English

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

[英]Problem sending mail with javamail

I'm trying to send an email to myself using the javamail api. 我正在尝试使用javamail API向自己发送电子邮件。 I've followed the code correctly that I found online but I can't seem to get it to work. 我已经正确地遵循了在网上找到的代码,但是似乎无法正常工作。 The error I'm getting is: 我得到的错误是:

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

Does that mean my computer doesn't support authentication? 这是否意味着我的计算机不支持身份验证?

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;
        }
    }
}

When I send emails from this server, what I have to do is, login via ssh to the server (login.server.com etc.) then I can send emails from the mail server (smtp.server.com etc.). 当我从该服务器发送电子邮件时,我要做的是通过ssh登录到服务器(login.server.com等),然后可以从邮件服务器(smtp.server.com等)发送电子邮件。 I need to mimic this in java 我需要在Java中模仿

RESOLVED: Use the SMTPSSLTransport class 解决:使用SMTPSSLTransport类

I had the same problem and for once did not find the answer on stackoverflow. 我有同样的问题,有一次没有在stackoverflow上找到答案。 @JPC's answer however provides a (very small) clue. @JPC的答案提供了一个(很小的)线索。

Starting with javamail 1.4.3 (? see refs), javamail will refuse to send plain text credentials over an unencrypted channel. 从javamail 1.4.3(请参阅参考资料)开始,javamail将拒绝通过未加密的通道发送纯文本凭证。 So enabling TLS transport is one solution. 因此,启用TLS传输是一种解决方案。 Another solution is to try another authentication method, if supported by the server, such as NTLM or SASL. 如果服务器支持,则另一种解决方案是尝试另一种身份验证方法,例如NTLM或SASL。 Another solution is to revert to JavaMail 1.4.1, which has no problem sending credentials in the clear. 另一个解决方案是还原为JavaMail 1.4.1,它毫无疑问地发送凭证。 The advantage to this is that your code does not have to change. 这样做的好处是您的代码不必更改。 The code in the question would work without a problem. 问题中的代码可以正常工作。 The disadvantage is obviously that you are sending credentials in the clear, but if your Exchange admin allows it...In my case, the mail server does not support TLS, so I had no other choice. 缺点显然是您以明文形式发送凭据,但是如果您的Exchange管理员允许,则...在我的情况下,邮件服务器不支持TLS,因此我别无选择。

References: 参考文献:

Make sure you have an smtp server (smtp.examplehost.com) to actually send the email. 确保您有一个smtp服务器(smtp.examplehost.com)可以实际发送电子邮件。 Make sure your smtp server allows "myemail@email.com" to send out emails. 确保您的smtp服务器允许“ myemail@email.com”发送电子邮件。

These are some of the security checks put in place by standard smtp servers. 这些是标准smtp服务器进行的一些安全检查。

This is probably you knock to wrong port. 这可能是您敲错了端口。 I suppose you're using IMAP protocol: For IMAP over SSL you have to connect to Port 993 我想您正在使用IMAP协议:对于基于SSL的IMAP,您必须连接到端口993

SMTPSSLTransport解决了问题

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

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