I'm trying to send an Email in Java but getting this error:
org.apache.geronimo.javamail.transport.smtp.SMTPSendFailedException:
SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
thats my code:
public static void sendHtmlEmail(String subject, String messages){
try{
String HOST = ...;
final String USER = ...;
final String PASSWORD = ...;
String PORT = "587";
String TO = ...;
String SUBJECT = "Test Mail";
String TEXT = "This is a test message from my java application. Just ignore it";
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.localhost", HOST);
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.auth",true);
props.put("mail.smtp.starttls.enable", true);
Authenticator auth = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(USER, PASSWORD);
}
};
Session session = Session.getInstance(props,auth);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(USER));
message.addRecipient(RecipientType.TO, new InternetAddress(TO));
message.setSentDate(new Date());
message.setSubject(SUBJECT);
message.setText(TEXT);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception e){
e.printStackTrace();
}
}
I tried other solutions I found but somehow everything makes it only worse. I have the feeling something is wrong with the authentication or it is ignored cause I can use everything as password and username without getting an error like 'wrong password/username'.
Can somebody help me please?