0

我想使用 gmx smtp 服务器发送一些带有 java 邮件的邮件。我得到的只是这个异常,我应该在异常中查看的页面没有提供如何解决这个问题的信息。

com.sun.mail.smtp.SMTPSendFailedException: 553 5.1.7 Complete address with domain, please ( http://portal.gmx.net/serverrules ) {mp032}
;
  nested exception is:
        com.sun.mail.smtp.SMTPSenderFailedException: 553 5.1.7 Complete address with domain, please ( http://portal.gmx.net/serverrules ) {mp032}

我在Java中实现它如下:

props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "mail.gmx.net");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.from", "test@test.test);
props.put("username", "SOMEUSERNAME@gmx.at");
props.put("password", "SOMEPASS");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.debug", "true");

Authenticator auth = new SMTPAuthenticator(props.getProperty("username"), props.getProperty("password"));


    if ("true".equals(smtp.getSmtpAuth())) {
        mailSession = Session.getDefaultInstance(props, auth);
    } else {
        mailSession = Session.getDefaultInstance(props);
    }


}

class SMTPAuthenticator extends Authenticator {

    private String username, password;

    public SMTPAuthenticator(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}
4

2 回答 2

4

这是使用GMX发送电子邮件的工作代码:

public static void sendGMX() throws MessagingException
{
    String sender = "my.email@gmx.de";
    String password = "my.password";
    String receiver = "my-receiver@gmail.com";

    Properties properties = new Properties();

    properties.put("mail.transport.protocol", "smtp");
    properties.put("mail.smtp.host", "mail.gmx.net");
    properties.put("mail.smtp.port", "587");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.user", sender);
    properties.put("mail.smtp.password", password);
    properties.put("mail.smtp.starttls.enable", "true");

    Session mailSession = Session.getInstance(properties, new Authenticator()
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(properties.getProperty("mail.smtp.user"),
                    properties.getProperty("mail.smtp.password"));
        }
    });

    Message message = new MimeMessage(mailSession);
    InternetAddress addressTo = new InternetAddress(receiver);
    message.setRecipient(Message.RecipientType.TO, addressTo);
    message.setFrom(new InternetAddress(sender));
    message.setSubject("The subject");
    message.setContent("This is the message content", "text/plain");
    Transport.send(message);
}

笔记

您必须在您的电子邮件帐户中手动启用 POP3/IMAP 支持,否则您仍然会遇到AuthenticationFailedException异常:

Exception in thread "main" javax.mail.AuthenticationFailedException: 535 Authentication credentials invalid

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)
于 2016-12-12T14:17:23.970 回答
0

首先,通过修复这些常见错误来清理和简化您的程序。

然后,打开 JavaMail 会话调试并检查协议跟踪。它应该向您显示服务器抱怨哪个地址的 SMTP 命令。(属性“mail.smtp.debug”不是 JavaMail 理解的属性。)

于 2013-06-04T21:45:27.800 回答