Java code for authenticating into SMTP server with Auth and TLS turned on..

After a long search I came across this sample Java code for sending email into an SMTP server which required authentication and secure (TLS) connection. Hence I thought, I will re-publish it. I found this piece of code from Java developer forums.....I could not trace back the link... Thanks to good soul who published it. I thought of re-publishing it due its rarity.

I have used Java Mail 1.4.

------------------------------- Java code ---------------------------
import javax.mail.\*;
import javax.mail.internet.\*;
import java.util.\*;

public class Main
{
String d_email = "ADDRESS@gmail.com",
d_password = "PASSWORD",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "EMAIL ADDRESS",
m_subject = "Testing",
m_text = "Hey, this is the testing email.";

public Main()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
//session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}

public static void main(String[] args)
{
Main blah = new Main();
}

private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, d_password);
}
}
}

Comments:

Thanks from Istanbul :) it works :)

Posted by mert on May 09, 2007 at 10:49 AM CDT #

hi I am shilpa.I read ur Java Code .Its good but problem arises when compiling the code .tell me how to compile on DOS prompt using Javamail-1.4 and what path which I am to give on DOS prompt .plz help me I am in great need. frendz help me . thanx.

Posted by Shilpa on May 10, 2007 at 07:10 PM CDT #

Thanks a tonne!!! I was in a bad mess as i wan't able to send mail using my gmail account. Now, its all working thanks to you! guys, just remember there might a prob of ambiguity when using the above methods as they are in java.net too. Just give the full path to remove the ambiguity... as simple as that! Ciao'

Posted by Amar on November 29, 2007 at 03:09 AM CST #

Thanks

It's really help code.

Posted by Le Phuoc Canh on March 20, 2008 at 02:50 PM CDT #

it works great!
Thank you guys,
you saved me a lot of time.

Posted by Ralph R. on September 27, 2008 at 10:03 PM CDT #

Thanks a ton!!

This was required and would serve as a nice tutorial.

Posted by hKansal on December 10, 2008 at 05:08 PM CST #

thank you. really.

Posted by david on January 11, 2009 at 08:00 AM CST #

This code gives me the following exception:

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused: connect
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)

Posted by mukunda on August 12, 2009 at 10:18 PM CDT #

Hi,

may be you have to use port 25 (TLS) and not the port 465?

Mirco

Posted by Mirco on September 08, 2009 at 03:25 AM CDT #

thanks for this snippet - you are top ranking on google!

Posted by mtraut on September 15, 2009 at 07:06 PM CDT #

much appreciated, excellent snippet

Posted by audioworm on October 17, 2009 at 07:20 AM CDT #

Thanks a lot. That works for me too..

Posted by zawoad on December 19, 2009 at 05:41 PM CST #

send me java code using gmail smtp IP for sending mail.Send me code Urgently

Posted by kiran kashid on January 19, 2010 at 03:30 AM CST #

Hi i am Subha . i am getting the following exception . javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.SocketException: Invalid argument: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)

please point out the problem .

Posted by Subha on March 30, 2011 at 08:39 PM CDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

apanicker

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today