使用gmx smtp服务器和java邮件

时间:2013-06-04 08:00:43

标签: java smtp javamail

我想使用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);
    }
}

2 个答案:

答案 0 :(得分:2)

以下是使用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);
}

注意

You have to manually enable POP3/IMAP support in your e-mail account否则您仍会获得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)

答案 1 :(得分:0)

首先,通过修复这些common mistakes来清理和简化您的程序。

然后,turn on JavaMail session debugging并检查协议跟踪。它应该显示哪个SMTP命令与服务器抱怨的地址。 (属性“mail.smtp.debug”不是JavaMail所理解的属性。)