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:
Could not connect to SMTP host: , port: 587
I tested with telnet this smtp address, along with the port number.

telnet smtp-auth.bris.ac.uk 587

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!

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();

Thank you very much, in advance.
Norbert

Dani AI

Generated

Quick diagnosis and the fix in brief: the SMTP error on port 587 usually comes from mixing STARTTLS (explicit TLS on 587) with an SSLSocketFactory (implicit SSL) and from not enabling SMTP auth. Port 587 expects a plain connection that upgrades to TLS with STARTTLS, so remove any mail.smtp.socketFactory.* lines and the old com.sun.net.ssl.internal.ssl.Provider() call, and make sure mail.smtp.auth is set to true. See the JavaMail property docs for starttls and the SSL notes for guidance. (JavaMail SMTP properties) (JavaMail SSL notes).

Concrete checklist

  • Set mail.smtp.auth = true and mail.smtp.starttls.enable = true (and optionally mail.smtp.starttls.required = true). (JavaMail SMTP properties)
  • Remove mail.smtp.socketFactory.port, mail.smtp.socketFactory.class, mail.smtp.socketFactory.fallback and the Security.addProvider(...) line — those force implicit SSL and conflict with STARTTLS. (JavaMail SSL notes)
  • If you must use implicit TLS instead, use port 465 and mail.smtp.ssl.enable = true (RFC 8314 explains 465 vs 587 for submission). (RFC 8314)
  • Put the JavaMail/JAF jars on the server classpath or configure a container mail session (preferred over embedding credentials in a JSP). (JavaMail README) (Jakarta EE mail-session)

Minimal property example (only the essentials):

props.put("mail.smtp.host","smtp-auth.bris.ac.uk");
props.put("mail.smtp.port","587");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable","true");
Session session = Session.getInstance(props, auth);
session.setDebug(true);
Transport.send(message);

Notes tied to thread: is right to warn about putting mail-sending code directly in JSP—move the logic to a servlet or use a container-managed javax.mail.Session to handle configuration and credentials safely. 's Gmail tip is useful for provider-specific setups, but the property fixes above are the immediate remedy for the connection error.

Recommended Answers

All 6 Replies

don't use JSP to send email.

Unfortunately, my problem is not solved yet.
So, if you can spot what is missing in my code or why it might not work, your response is more than welcome!

Thanks,
Norbert

what's missing is a good spanking of you for even attempting to do such a thing using JSP.

This page has code to send mail from GMail SMTP. Copy the code and run it.

If necessary add some concatenation operator.

i need jsp code for sending email and receiving te mail....from my website..pls reply soon

while i am using this code i vl get from address is my address but i never got from address as my friend's email address what i have to do

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.