Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sending email in JSP with SMTP authentication

843840Jun 27 2008 — edited Jul 16 2008
Dear all,

I am about to implement a user feedback page, by using the smtp server of my university.
When I try to send a form, an error message is received as below:
{color:#ff0000}Could not connect to SMTP host: smtp-auth.bris.ac.uk, port: 587

{color}
I tested with telnet this smtp address, along with the port number.
 telnet smtp-auth.bris.ac.uk 587{code}
This works fine, I can connect to it. Besides, I configured an email client where I set up the right protocol TLS/STARTTLS, and I used my username/password. It is also working fine.
So, the problem might be with my embedded Java code in JSP.

First of all, I tested my web server to send a test email in JSP without SMTP authentication. This works fine. 
But, I am not able to find out what is wrong/missing in my code below to send successfully messages with authentication.
Please have a look at it, and any recommendation is welcome!

{code}

import="java.util.*;*
*import="java.io.*;
import="javax.mail.*;*
*import="javax.event.*;
import="javax.mail.internet.*;*
*import="java.security.Security.*;
import="javax.activation.*;


Properties props = new Properties();

props.put("mail.smtp.host", "smtp-auth.bris.ac.uk");
props.put("mail.smtp.port","587");
props.put("mail.debug", "true"); 
props.put("mail.smtp.debug", "true");
props.put ("mail.smtp.starttls.enable", "true"); 
props.put("mail.transport.protocol", "smtp");

props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Authenticator auth = new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication("myUsername", "myPassword");
}
};

Session s = Session.getInstance(props,auth);


MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress("myFriendAddress");
message.setFrom(from);

InternetAddress to = new InternetAddress("myAddress");
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Test from JavaMail.");
message.setText("Hello from JavaMail!");
message.saveChanges(); 

Transport transport = s.getTransport("smtp");
transport.connect("smtp-auth.bris.ac.uk","myUsername","myPassword");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
{code}

Thank you very much, in advance.
Norbert

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2008
Added on Jun 27 2008
7 comments
1,157 views