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

[3133 byte] By [Devibalana] at [2007-11-27 11:17:38]
# 1

As I answered in the other thread where you posted the same question...

You don't need to set the socket factory properties or even the port. Just use

the "smtps" protocol instead of the "smtp" protocol. See SSLNOTES.txt for details.

The reason you can't connect when using port 25 (smtp) instead of port 465 (smtps)

is that some servers don't accept connections on the non-TLS port.

bshannona at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thanks shannon!

By changing sever as smtps and port as 465

i got an exception

javax.mail.SendFailedException: Sending failed;

nested exception is:

class javax.mail.MessagingException: Could not connect to SMTP host: smt

ps, port: 465;

nested exception is:

java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFact

ory" socket factory to host, port: smtps, 465; Exception: java.lang.reflect.Invo

cationTargetException

at javax.mail.Transport.send0(Transport.java:218)

at javax.mail.Transport.send(Transport.java:80)

at Main.<init>(Main.java:56)

at Main.main(Main.java:66)

am i did correctly or not? Please advice if am wrong?

am i want to do anything in mailserver side?

am i want to configure smtps at 465 port?

am i want to generate an certificate compulsorily?

Thanks,

devi

Devibalana at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

can u give a workable code to check whether its an our environment problem?

Devibalana at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

I don't know what you did, but you did it wrong.

From the error message it looks like you set the host name

to "smtps" rather than the port number.

Did you read SSLNOTES.txt?

bshannona at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

sorry for misunderstanding!

I read sslnotes.txt and those RFC's while starting the TLS work itself..

With the reference of sslnotes only i set the sslfactory property!

Kindly guide me!

Devibalana at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

y no help?

Devibalana at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

Since you don't seem to be able to figure out what you've done wrong,

you're going to have to provide more detail about exactly what you're doing

so we can help you.

bshannona at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

Let me explain my stage and then give a help please!

The program that posted in my previous mail is working well for gmail server

at port 465. but the same is not working for my local smtp server 25!

as you advised me already i referred the SSLNOTES.txt and setted the property

props.put("mail.transport.protocol","smtps");

then am getting exception

DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unrecognized

SSL message, plaintext connection?

javax.mail.SendFailedException: Sending failed;

nested exception is:

class javax.mail.MessagingException: Exception reading response;

nested exception is:

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connecti

on?

at javax.mail.Transport.send0(Transport.java:218)

at javax.mail.Transport.send(Transport.java:80)

at Main.<init>(Main.java:74)

at Main.main(Main.java:84)

After this by refering sslnotes.txt i used transport.sendMessage() method instead of Transport.send() method. Then am getting a exception as

DEBUG: setDebug: JavaMail version 1.3.1

javax.mail.NoSuchProviderException: No provider for smtp

at javax.mail.Session.getProvider(Session.java:436)

at javax.mail.Session.getTransport(Session.java:631)

at javax.mail.Session.getTransport(Session.java:612)

at Main.<init>(Main.java:65)

at Main.main(Main.java:84)

help pls?

Devibalana at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

shannon am i going in correct way?

do reply...!

Devibalana at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10

If possible, please upgrade from JavaMail 1.3.1 to JavaMail 1.4.

Part of the problem may be that I'm giving you instructions that work

with the latest version of JavaMail but you're using an older version.

Also, if you're trying to connect to your local mail server on port 25,

you don't want SSL/TLS. The SMTP server on port 25 does

not expect an encrypted connection. Just use the simple JavaMail

defaults for sending a message.

If your local server is expecting you to connect on port 25 and then

turn on SSL/TLS, you'll need to enable use of the STARTTLS command.

bshannona at 2007-7-29 14:26:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...