TLS Authentication
Hi all,
I tried to send a test mail by using the following code
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
//import com.adventnet.nms.smtp.*;
public class Main
{
String d_email = "x@gmail.com",
d_password = "xxx",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "x@gmail.com",
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.auth", "true");
// Necessary Properties
props.put("mail.smtp.starttls.enable","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();
/*try{
String user=null;
String pwd=null;
boolean ssl=true;
com.adventnet.nms.smtp.SmtpMailer mailer = new com.adventnet.nms.smtp.SmtpMailer("smtp.gmail.com","avuluri.mallikarjuna@gmail.com","mallikarjuna@adventnet.com","hai this is testing mail",user,pwd,ssl);
}catch(Exception e){
System.out.println("Exception is :");
e.printStackTrace();
}*/
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, "xxx");//password should be given here in ""
}
}
}
This code works perfectly!!
If i tried the same with smtp at port 25 to send mail to my address its not working,
I get an exception could not bind to smtp port 25..
But if i set the property"mail.smtp.socketFactory.class" as following means then the code is working and mail has sent..
SSLSocketFactory sslFact =(SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket s =(SSLSocket)sslFact.createSocket(d_host, Integer.parseInt(d_port));
props.put("mail.smtp.socketFactory.class",s);
Why like this?
My question is was by doing like this mail sent with TLS Enabled or not?
How to check whether the TLS Authentication is done r not?
Experts please help!!
Am waiting for ur reply....
Thanks in advance,
devi

