我一直尝试从试图登录到Outlook服务器的自定义域发送电子邮件,因为我们将其用作从该方向发送电子邮件的平台,但是它给了我{{1} };更具体地说:
AuthenticationFailedException
我一直在寻找一些较旧的文章,这些文章可能是解决我的问题的方法,但是我不知道如何在我的代码中实现它。
我的代码如下:
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [PR0P264CA0272.FRAP264.PROD.OUTLOOK.COM]
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at baseDatos.BaseDeDatos.mailMasivo(BaseDeDatos.java:595)
at baseDatos.BaseDeDatos.main(BaseDeDatos.java:167)
我已经更改了代码以实现spring框架库,以便可以同步发送邮件。这是现在的样子:
String to = "to@mail.com";
String from = "from@cdomain";
String password = "password123";
String subject = "Hello";
BodyPart body1 = new MimeBodyPart();
String body= "This is an e-mail";
body1.setText(body);
Multipart mainBody = new MimeMultipart();
mainBody.addBodyPart(body1);
String host = "outlook.office365.com";
Properties properties = System.getProperties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(properties, new OutLookAuthenticator(from, password));
Transport transport = session.getTransport("smtp");
Message message = new MimeMessage(session);
message.setSubject(asunto);
message.setContent(mainBody);
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
transport.send(message);
现在,它正在打印此堆栈跟踪:
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("outlook.office365.com");
mailSender.setPort(587);
String from = keyboard.nextLine();
mailSender.setUsername(from);
String password = readPassword();
mailSender.setPassword(password);
MimeMessage message = mailSender.createMimeMessage();
final Properties properties = new Properties();
properties.put("mail.smtp.auth", Boolean.TRUE);
properties.put("mail.smtp.starttls.enable", Boolean.TRUE );
mailSender.setJavaMailProperties(properties);
try {
final MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text, true);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(message);
答案 0 :(得分:-1)
看来您的凭证错误,或者托管项目的服务器可能阻塞了某些传出端口。例如,数字海洋默认情况下会阻塞25端口。
但是,您可以在此处找到同步发送邮件的解决方案。
其中this.getUsername()
,this.getPassword()
,this.getPort()
,this.getHost()
作为您的(邮件服务器)凭据。
如果您也想在邮件中发送HTML,请参阅.setText()
,该参数将Boolean
参数作为第二个参数。
P.S
顺便说一句,我建议您以新的Thread
格式发送,因为它需要时间来发送,然后无论是否成功,都会收到响应。但是,这确实取决于您的业务逻辑。
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(this.getHost());
mailSender.setPort(this.getPort());
mailSender.setUsername(this.getUsername());
mailSender.setPassword(this.getPassword());
MimeMessage message = mailSender.createMimeMessage();
final Properties properties = new Properties();
properties.put("mail.smtp.auth", Boolean.TRUE);
properties.put("mail.smtp.starttls.enable", Boolean.TRUE);
mailSender.setJavaMailProperties(properties);
final MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(fromMail);
helper.setTo(user.getEmail());
helper.setSubject(subject);
helper.setText("<p><strong>Welcome " + body + ",</strong></p>\n", true);
mailSender.send(message);