Error sending email using Hotmail as Commonsmail Java
I am having trouble sending email using Outlook, returning the following error.
Erro Sending the email to the following server failed: smtp.live.com:25
I have tried with port 587
and with smtp smtp-mail.outlook.com
, but without success.
Follows the code I use for submission.
HtmlEmail email = new HtmlEmail();
email.setSSLOnConnect(true);
email.setHostName("smtp-mail.outlook.com");
email.setSslSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("[email protected]", "12345678"));
try {
email.setFrom("[email protected]");
email.setDebug(true);
email.setSubject(tituloEmail);
email.setHtmlMsg(conteudo);
email.addTo(destinatario);
email.send();
1
1 answers
I usually use this way with java mail
Properties propertie = new Properties();
propertie.put("mail.transport.protocol", "smtp");
propertie.put("mail.smtp.host", "smtp.live.com");
propertie.put("mail.smtp.socketFactory.port", "587");
propertie.put("mail.smtp.socketFactory.fallback", "false");
propertie.put("mail.smtp.starttls.enable", "true");
propertie.put("mail.smtp.auth", "true");
propertie.put("mail.smtp.port", "587");
session = Session.getDefaultInstance(propertie,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]", "suasenha");
}
});
I believe it's just problem on the host, try swapping the
smtp-mail.outlook.com
By
smtp.live.com
.
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.live.com");
email.setSslSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("[email protected]", "suasenha"));
Edited
Example using javaMail
public class SendMail {
public static void main(String[]args) throws IOException {
final String username = "[email protected]";
final String password = "suasenha";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "outlook.office365.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("seuemail@outlook"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("emaildestino@..."));
message.setSubject("Testando");
message.setText("Olá");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
2
Author: brow-joe, 2017-03-03 14:19:28