This is my code
public class MailSend
{
String host, port, emailid,username, password;
Properties props = System.getProperties();
Session l_session = null;
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
public MailSend() {
host = "smtp.mail.yahoo.com";
port = "587";
username = "xxxx@yahoo.com";
password = "xxxxxx";
}
public String sendMail(String toaddress,String subj,String msg)
{
emailSettings();
createSession();
String res= sendMessage("xxxx@yahoo.com", toaddress,subj,msg);
return res;
}
public void emailSettings() {
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
}
public void createSession() {
l_session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxx@yahoo.com", "xxxxx");
}
});
l_session.setDebug(true); // Enable the debug mode
}
public String sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
try {
Address addr = new javax.mail.internet.InternetAddress("xxxxx@yahoo.com","xxxx@yahoo.com");
MimeMessage message = new MimeMessage(l_session);
emailid = emailFromUser;
message.setFrom(addr);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setSubject(subject);
message.setContent(msg, "text/html");
Transport.send(message);
System.out.println("Mail Sent");
} catch (MessagingException mex) {
mex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}//end catch block
return "success";
}
public static void main(String args[])
{
MailSend m=new MailSend();
m.sendMail("xxxxxxx@yahoo.com", "welcome", "testing");
}
when i run this code i got the following exception
**com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Authentication required** Exception.
but i'm using PasswordAuthenticator
.
I don't know why this exception coming.can any one tell how to resolve this exception or please give any reference site.