0

I have created an email account for admin of my web site using google's gmail app as admin@mywebsite.com. I am able to login using the credentials on gmail's login web page. Now i want to send email using java and this id

String host = "smtp.gmail.com";
    String from = "admin@mywebsite.com";
    String pass = "myPass";
    Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
....
...
...
Session session = Session.getDefaultInstance(props, auth);
...
...
Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);

    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

This is throwing an exception as Exception in thread "main" javax.mail.AuthenticationFailedException

However, when i try this with gmail credentials (myid@gmail.com), it works. Please help me out on this. Thanks in advance

user
  • 6,897
  • 8
  • 43
  • 79
bhuvan
  • 51
  • 2
  • 7
  • Related: http://stackoverflow.com/questions/7806944/failure-sending-mail-via-google-smtp – user Dec 12 '11 at 12:18

3 Answers3

0

Judging by the accepted answer to What mechanism does Gmail use for user authentication?, you need to connect to Google's mail server on port 465 or 587, then use STARTTLS (presumably only on port 587) and user authentication. So what you are lacking is the port 465 or 587 part.

Community
  • 1
  • 1
user
  • 6,897
  • 8
  • 43
  • 79
  • you mean to say that i should use the statement props.put("mail.smtp.port", "465"); instead of props.put("mail.smtp.port", "587"); – bhuvan Dec 12 '11 at 12:26
  • I don't see any port number being set anywhere in the code you posted. So it's using the SMTP default, 25, which appears to be wrong in this particular case. – user Dec 12 '11 at 12:30
0
  • Have you already authenticated your admin@mywebsite.com with Send As permissions in your gmail Account Settings?
  • If yes, then you might need to give your authentication user id and password as your gmail account.
Muthu
  • 2,675
  • 4
  • 28
  • 34
  • Sorry, didn't get you. Please elaborate a bit – bhuvan Dec 12 '11 at 12:18
  • If you go to gmail --> Settings --> Account settings, there will be a Send Mail As option group. Your admin@mywebsite.com account should be enabled for this first. After this, you might have to use the from address as "admin@mywebsite.com", your user id as "yourgmailid@gmail.com" and password as "yourgmailpassword". Hope this helps. – Muthu Dec 12 '11 at 12:22
  • sorry, guys i have to leave for the day. But i haven't solved this yet. Will be back after 15 hours from now – bhuvan Dec 12 '11 at 12:36
  • Hey guys it worked today. I just came this morning, gave it a try and was able to send mails using java. One reason i can think of this behavior is that i created this account just yesterday and gmail requires some time to syn to all its servers and grant permissions to sent messages via API – bhuvan Dec 13 '11 at 07:33
0

you have to used for port number to connect with gmail. TLS:587 SSL:465

you have to used one of them so you can send the email through gmail.

Suresh
  • 1,494
  • 3
  • 13
  • 14