Reputation: 219
This program attempts to send e-mail but throws a run time exception:AuthenticationFailedException I have referred the stackoverflow quetion and answer also same thing I have implemented but still I am getting exception like this could any one plz resolve this issue.
Exception
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:267)
at javax.mail.Service.connect(Service.java:137)
at javax.mail.Service.connect(Service.java:86)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.treamis.transport.vehicle.javaMail.send(javaMail.java:81)
at com.treamis.transport.vehicle.MysqlBackup.backupDataWithDatabase(Mysq
lBackup.java:97)
at com.treamis.transport.vehicle.MysqlBackup.run(MysqlBackup.java:118)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Sms sent xl sheet is generated is generated
java Mail code
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class javaMail {
private String SMTP_PORT = "465";
private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private String SMTP_HOST_NAME = "smtp.gmail.com";
private Properties smtpProperties;
public javaMail() {
initProperties();
}
private void initProperties() {
smtpProperties = new Properties();
smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME);
smtpProperties.put("mail.smtp.auth", "true");
smtpProperties.put("mail.debug", "true");
smtpProperties.put("mail.smtp.port", SMTP_PORT);
smtpProperties.put("mail.smtp.socketFactory.port", SMTP_PORT);
smtpProperties.put("mail.smtp.socketFactory.class", SSL_FACTORY);
smtpProperties.put("mail.smtp.socketFactory.fallback", "false");
}
public String send(String[] to, final String from, final String pwd, String subject, String body) {
javaMail tjm = new javaMail();
try {
Properties props = tjm.getSmtpProperties();
// -- Attaching to default Session, or we could start a new one --
// Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(from, pwd);
// }
// });
Session session = Session.getInstance(props, new GMailAuthenticator(from, pwd));
// Session session = Session.getInstance(props, new javax.mail.Authenticator() {
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(userName, password);
// }
//});
Message msg = new MimeMessage(session);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Test mail one");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(body);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(body);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent OK.");
return "success";
} catch (Exception ex) {
ex.printStackTrace();
ex.getMessage();
}
return null;
}
public Properties getSmtpProperties() {
return smtpProperties;
}
public void setSmtpProperties(Properties smtpProperties) {
this.smtpProperties = smtpProperties;
}
}
GMailAuthenticator code */
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
class GMailAuthenticator extends Authenticator {
String user;
String pw;
public GMailAuthenticator (String username, String password)
{
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
}
Upvotes: 0
Views: 6342
Reputation: 29961
AuthenticationFailedException means the server thinks you gave it the wrong username or password. You're going to say "of course I didn't give it the wrong username or password, I'm not that stupid!" Well, the server disagrees with you.
Try turning on JavaMail session debugging, the protocol trace might provide additional clues as to what's going wrong. You might also need to set the "mail.debug.auth" property to "true" to get additional details about the login process.
Also, see the JavaMail FAQ for some common mistakes in your code.
And make sure there's no anti-virus or firewall that's intercepting your attempts to connect to the server.
Upvotes: 1