gpt4 book ai didi

java - 使用 gmx smtp 服务器和 java 邮件

转载 作者:行者123 更新时间:2023-12-01 23:46:49 25 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 使用 gmx smtp 服务器和 java 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16912614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com