Yes. To be sure I just did another test. Both use exactly the same settings, send from and to the same address.
Main Topics
Browse All TopicsI'm having troubles getting JavaMail to work with authentication, using an outside SMTP-server. The server is not the server of my ISP (the one which connects me to the internet now). I can use this SMTP-server to send mails with Thunderbird, so the connection should work.
I get the following error:
javax.mail.AuthenticationF
at javax.mail.Service.connect
at javax.mail.Service.connect
at javax.mail.Service.connect
at javax.mail.Transport.send0
at javax.mail.Transport.send(
at net.grexx.square.mail.Simp
at net.grexx.square.mail.Proc
at org.apache.jsp.SimpleSende
at org.apache.jasper.runtime.
at javax.servlet.http.HttpSer
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at javax.servlet.http.HttpSer
at org.apache.catalina.core.A
at org.apache.catalina.core.A
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.valves
at org.apache.catalina.core.S
at org.apache.catalina.connec
at org.apache.coyote.http11.H
at org.apache.coyote.http11.H
at org.apache.tomcat.util.net
at org.apache.tomcat.util.net
at org.apache.tomcat.util.thr
at java.lang.Thread.run(Unkno
The java code that sends the mail is:
public static void send(String smtpServer, String smtpUsername, String smtpPassword, String to, String from
, String subject, String body)
{
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host"
props.put("mail.smtp.auth"
Session session = Session.getDefaultInstance
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.
// ,InternetAddress.parse(cc,
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "GrexxBoxxEmail");
msg.setSentDate(new Date());
Transport tr = session.getTransport("smtp
tr.connect(smtpServer, smtpUsername, smtpPassword);
msg.saveChanges(); // don't forget this
// -- Send the message --
Transport.send(msg);
System.out.println("Messag
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
I have no idea what I'm doing wrong here. Any suggestions?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
CEHJ is probably right, there is a mismatch between your Thunderbird setting and the Java code you're using.
Try watching the mail server debug messages with:
session.setDebug(true)
Try setting mail.smtp.auth to false, but leave the user name and password in the tr.connect. You might be mistaken about the authentication method. Having the authenticated user will still work. There are several types of authenticated use of mail servers, and useAuth might not be true for your mail server, although it requires user login to send email.
When I remove the mail.smtp.auth line, the connection seems to work. So I think this solves this part.
When mail is sent to an addres inside the smpt-domain, it works. When the mail is sent to e.g. gmail.com, it doesn't work. Then I get a relay error, while this gives no problem using Thunderbird. This was in fact the reason I added the mail.smtp.auth line. I thought that the relay was refused because authentication wasn't happening.
com.sun.mail.smtp.SMTPAddr
If you are sending out of the smtp-domain, from a valid user within smtp-domain, then you need to change your authentication code slightly:
javax.mail.Authenticator authenticator = new org.apache.commons.mail.De
props.put("mail.smtp.host"
props.put("mail.smtp.auth"
Session sesion = Session.getInstance(props,
You can also write your own DefaultAuthenticator, based on the commons class found here:
http://www.koders.com/java
This is only for the case where your MAIL FROM is on smtpHost and the To is outside the host.
Hi
yes i can send mail without using any authentication code when i use the localhost instead of the my own smtp server
after i did install the free smtp software on my machine .
now i have made the following changes in the code as per what u said
here is my changed code
package mailandbean;
import javax.mail.*; //JavaMail packages
import javax.mail.internet.*; //JavaMail Internet packages
import java.util.*; //Java Util packages
import javax. mail.Authenticator.*;
import javax.mail.PasswordAuthent
public class SendMailBean {
public String send(String p_from,String p_to,String p_cc,String p_bcc,String p_subject,String p_message,String p_smtpServer) {
String l_result = "<BR><BR><BR><BR><BR><BR><
// Name of the Host machine where the SMTP server is running
String l_host = p_smtpServer;
// Gets the System properties
Properties props = System.getProperties();
//user authentication
javax.mail.Authenticator authenticator = new DefaultAuthenticator("user
// props.put("mail.smtp.host"
props.put("mail.smtp.host"
props.put("mail.smtp.auth"
Session sesion = Session.getInstance(props,
// Puts the SMTP server name to properties object
props.put("mail.smtp.auth"
// Get the default Session using Properties Object
Session l_session = Session.getDefaultInstance
l_session.setDebug(true); // Enable the debug mode
try {
MimeMessage l_msg = new MimeMessage(l_session); // Create a New message
l_msg.setFrom(new InternetAddress(p_from)); // Set the From address
// Setting the "To recipients" addresses
l_msg.setRecipients(Messag
// Setting the "Cc recipients" addresses
l_msg.setRecipients(Messag
// Setting the "BCc recipients" addresses
l_msg.setRecipients(Messag
l_msg.setSubject(p_subject
// Create and fill the first message part
MimeBodyPart l_mbp = new MimeBodyPart();
l_mbp.setText(p_message);
// Create the Multipart and its parts to it
Multipart l_mp = new MimeMultipart();
l_mp.addBodyPart(l_mbp);
// Add the Multipart to the message
l_msg.setContent(l_mp);
// Set the Date: header
l_msg.setSentDate(new Date());
// Send the message
Transport.send(l_msg);
// If here,then message is successfully sent.
// Display Success message
l_result = l_result + "<FONT SIZE=4 COLOR=\"blue\"><B>Success!
"<FONT SIZE=4 COLOR=\"black\"> "+
"<HR><FONT color=green><B>Mail was successfully sent to </B></FONT>: "+p_to+"<BR>";
//if CCed then,add html for displaying info
if (!p_cc.equals(""))
l_result = l_result +"<FONT color=green><B>CCed To </B></FONT>: "+p_cc+"<BR>";
//if BCCed then,add html for displaying info
if (!p_bcc.equals(""))
l_result = l_result +"<FONT color=green><B>BCCed To </B></FONT>: "+p_bcc ;
l_result = l_result+"<BR><HR>";
} catch (MessagingException mex) { // Trap the MessagingException Error
// If here,then error in sending Mail. Display Error message.
l_result = l_result + "<FONT SIZE=4 COLOR=\"blue\"> <B>Error : </B><BR><HR> "+
"<FONT SIZE=3 COLOR=\"black\">"+mex.toSt
} catch (Exception e) {
// If here,then error in sending Mail. Display Error message.
l_result = l_result + "<FONT SIZE=4 COLOR=\"blue\"> <B>Error : </B><BR><HR> "+
"<FONT SIZE=3 COLOR=\"black\">"+e.toStri
e.printStackTrace();
}//end catch block
finally {
return l_result;
}
} // end of method send
/**
* This is a very simple authentication object that can be used for any
* transport needing basic userName and password type authentication.
*
* @author <a href="mailto:quintonm@bell
* @version $Id: DefaultAuthenticator.java,
*/
public class DefaultAuthenticator extends Authenticator
{
/** Stores the login information for authentication */
private PasswordAuthentication authentication;
/**
* Default constructor
*
* @param userName user name to use when authentication is requested
* @param password password to use when authentication is requested
*/
public DefaultAuthenticator(Strin
{
this.authentication = new PasswordAuthentication(
userName, password );
}
/**
* Gets the authentication object that will be used to login to the mail
* server.
*
* @return A <code>PasswordAuthenticati
* login information.
*/
protected PasswordAuthentication getPasswordAuthentication(
{
return this.authentication;
}
}
} //end of bean
now i get the below error
Error :
--------------------------
javax.mail.SendFailedExcep
--------------------------
Compose Mail
Business Accounts
Answer for Membership
by: CEHJPosted on 2007-01-15 at 04:49:15ID: 18315845
Are you sure that:
a. you have reproduced the connection details of the Thunderbird account?
b. you are sending to the same sort of address with each?