I have managed to write a Java Program successfully to generate an email from my GMail account to the outlook account. Here is the excerpt of the code:
String to="<my>@outlook.com";
String from="<my>@gmail.com";
final String username="<my>@gmail.com";
final String password="<App pw genertaed at GMail>";
String host="smtp.gmail.com";
Properties prop =new Properties();
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.starttls.enable", true);
prop.put("mail.smtp.port", "587");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
};
Session session = Session.getDefaultInstance(prop, authenticator);
<skipped rest of the steps for brevity>
This code sends the email from my gmail to outlook successfully.
However, if I try to use the same code in my Web Application, the application throws the following exception:
jakarta.mail.SendFailedException: Send failure (jakarta.mail.MessagingException: Connection error (java.net.ConnectException: Connection refused))
All my Jakara Mail configuration is in the file webapp/META-INF/context.xml and the configuration is a below:
<Context path="/mywebapp">
<Resource id="mail/webapp" type="jakarta.mail.Session">
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.user=<my>@gmail.com
password=<App pw genertaed at GMail>
<\/Resource>
<\/Context>
Since I am running the WebApp on Tomee9, the above config details are as the the Server's official documentation
I am completely confused, what wrong am I doing?
Any feedback would be appreciated. Many Thanks