This program attempts to send e-mail but throws a run time exception:
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
See the 9 line of your code,it may be an error; it should be:
mail.smtp.user
not
mail.stmp.user;
I've solved this issue adding user and password in Transport.send call:
Transport.send(msg, "user", "password");
According to this signature of the send function in javax.mail (from version 1.5):
public static void send(Message msg, String user, String password)
Also, if you use this signature it's not necessary to set up any Authenticator, and to set user and password in the Properties (only the host is needed). So your code could be:
private void sendMail(){
try{
Properties prop = System.getProperties();
prop.put("mail.smtp.host", "yourHost");
Session session = Session.getInstance(prop);
Message msg = #createYourMsg(session, from, to, subject, mailer, yatta yatta...)#;
Transport.send(msg, "user", "password");
}catch(Exception exc) {
// Deal with it! :)
}
}
This error may be about password characters. If your password contains special characters and also you add your password into Transport class methods;
For Example
Transport transport = session.getTransport("smtp");
transport.connect("user","passw@rd");
or
Transport.send(msg, "user", "passw%rd");
you may get that error. Because Transport class' methods may not handle special characters. If you add your username and password into your message using javax.mail.PasswordAuthentication class, i hope you will escape that error;
For Example
...
Session session = Session.getInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("user", "pas$w@r|d");
}
});
Message message = new MimeMessage(session);
...
Transport.send(message);
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
public class RegisterAction {
public String execute() {
RegisterAction mailBean = new RegisterAction();
String subject="Your username & password ";
String message="Hi," + username;
message+="\n \n Your username is " + email;
message+="\n \n Your password is " + password;
message+="\n \n Please login to the web site with your username and password.";
message+="\n \n Thanks";
message+="\n \n \n Regards";
//Getting FROM_MAIL
String[] recipients = new String[1];
recipients[0] = new String();
recipients[0] = customer.getEmail();
try{
mailBean.sendMail(recipients,subject,message);
return "success";
}catch(Exception e){
System.out.println("Error in sending mail:"+e);
}
return "failure";
}
public void sendMail( String recipients[ ], String subject, String message)
throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.auth", true);
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"username@gmail.com", "5373273437543");// Specify the Username and the PassWord
}
});
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
//msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
//send message
Transport.send(msg);
System.out.println("Message Sent Successfully");
}
}
Turn On "Access for less secure apps" in Security setting for the gmail account.(from mail), see the below link for references
http://www.ghacks.net/2014/07/21/gmail-starts-block-less-secure-apps-enable-access/
Even when using an Authenticator I had to set mail.smtp.auth property to true. Here is a working example:
final Properties props = new Properties();
props.put("mail.smtp.host", config.getSmtpHost());
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(config.getSmtpUser(), config.getSmtpPassword());
}
});