Abraham Panicker's Weblog
Abraham Panicker's Weblog
Archives
« October 2009
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
31
       
Today
Click me to subscribe
Search

Links
 

Today's Page Hits: 36

Locations of visitors to this page
« Installing directly... | Main | Notes on Portlet... »
Thursday Sep 07, 2006
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);
}
}
}

Posted at 06:53PM Sep 07, 2006 by apanicker in Technical  |  Comments[11]

Comments:

Thanks from Istanbul :) it works :)

Posted by mert on May 09, 2007 at 05:49 PM 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 11, 2007 at 02:10 AM 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 11:09 AM CST #

Thanks

It's really help code.

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

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

Posted by Ralph R. on September 28, 2008 at 05:03 AM CDT #

Thanks a ton!!

This was required and would serve as a nice tutorial.

Posted by hKansal on December 11, 2008 at 01:08 AM CST #

thank you. really.

Posted by david on January 11, 2009 at 04:00 PM 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 13, 2009 at 05:18 AM 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 10:25 AM CDT #

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

Posted by mtraut on September 16, 2009 at 02:06 AM CDT #

much appreciated, excellent snippet

Posted by audioworm on October 17, 2009 at 02:20 PM CDT #

Post a Comment:
  • HTML Syntax: NOT allowed