简体   繁体   English

使用gmx smtp服务器和java邮件

[英]Using gmx smtp server with java mail

I want to use the gmx smtp server to send some mails with java mail. 我想使用gmx smtp服务器发送一些带有java邮件的邮件。 All i get is this exception, the page which i should review in the exception doesnt provide me information how to fix this issue. 我得到的只是这个例外,我应该在异常中查看的页面并不能为我提供如何解决此问题的信息。

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}

I implemented it in Java as following: 我用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);
    }
}

Here is working code for sending an email with GMX : 以下是使用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);
}

Note 注意

You have to manually enable POP3/IMAP support in your e-mail account otherwise you will still get an AuthenticationFailedException exception: 您必须在电子邮件帐户中手动启用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)

First, clean up and simplify your program by fixing these common mistakes . 首先,通过修复这些常见错误来清理和简化您的程序。

Then, turn on JavaMail session debugging and examine the protocol trace. 然后, 打开JavaMail会话调试并检查协议跟踪。 It should show you which SMTP command with which address the server is complaining about. 它应该显示哪个SMTP命令与服务器抱怨的地址。 (The property "mail.smtp.debug" is not a property JavaMail understands.) (属性“mail.smtp.debug”不是JavaMail所理解的属性。)

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

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