Error Occurred while sending mail through gmail account
Hello,
I am writing 1 demo appln which will send a mail from my gmail account. It will show error like"javax.mail.MessagingException: 530 5.5.1 Authentication Required f77sm3109311pyh"
Here is Code : -
package demo;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
try
{
Properties properties=System.getProperties();
properties.put("mail.smtp.host","smtp.gmail.com");
properties.put("mail.smtp.auth","true");
properties.put("mail.from","sushrandive@gmail.com");
properties.put("mail.smtp.port","465");
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
Authenticator auth = new PopupAuthenticator();
Session session = Session.getDefaultInstance(properties,auth);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress("sush_randive@yahoo.com"));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
message.saveChanges();
Address address[] = message.getAllRecipients();
Address a1 = address[0];
System.out.println("Recipient : "+a1.toString());
Transport.send(message, message.getAllRecipients());
}
catch(Exception e){e.printStackTrace();}
}
}
and code for PopupAuthenticator :-
package demo;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.swing.*;
import java.util.StringTokenizer;
public class PopupAuthenticator extends Authenticator
{
public PopupAuthenticator()
{
System.out.println("In Auth");
}
public PasswordAuthentication getPasswordAuthentication()
{
String username, password;
String result = JOptionPane.showInputDialog("Enter 'username,password'");
StringTokenizer st = new StringTokenizer(result, ",");
username = st.nextToken();
password = st.nextToken();
return new PasswordAuthentication(username, password);
}
}
But when i run this code i never get popup window asking for username/password and it throws follwing exception
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: 530 5.5.1 Authentication Required f77sm3109311pyh
at javax.mail.Transport.send0(Transport.java:204)
at javax.mail.Transport.send(Transport.java:88)
at demo.Demo.main(Demo.java:66)
may i know wats wrong wid my code? I am confussed about when getPasswordAuthentication () method will call?
please reply me at sushrandive@gmail.com
thnx in advance ..
-Sushrut

