dcsimg
Using SSL/TLS with SMTP
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Keith_Cheshire
Posted On:   Thursday, January 3, 2002 07:29 AM

I am using JSSE in an attempt to establish an SSL connection over which I plan to transmit (SMTP) and retrieve (IMAP) e-mail messages to and from a MS Exchange 5.5 Server via a mail client I am writing. I have been able to successfully retrieve messages using IMAP, and even authenticate initally with the SMTP (server requires authentication), but when I attempt to send a message I get Error 505 Secure Channel Required returned from the mail server. This is the code: import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.security.*; public class MailTester { public MailTester() { sen   More>>

I am using JSSE in an attempt to establish an SSL connection
over which I plan to transmit (SMTP) and retrieve (IMAP) e-mail
messages to and from a MS Exchange 5.5 Server via a mail client
I am writing. I have been able to successfully retrieve messages
using IMAP, and even authenticate initally with the SMTP (server
requires authentication), but when I attempt to send a message
I get Error 505 Secure Channel Required returned from the mail
server. This is the code:

			
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

import java.security.*;

public class MailTester
{
public MailTester()
{
sendMessage();
}

public static void main(String[] args)
{
MailTester mailTester = new MailTester();
}

private void sendMessage()
{
try
{
boolean debug = true;

// Register SSL security provider
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Security.setProperty("ssl.SocketFactory.provider", "DummySSLSocketFactory");

Properties properties = System.getProperties();

final String SSL_FACTORY = "DummySSLSocketFactory";

// IMAP provider
properties.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);

// IMAP provider
properties.setProperty("mail.imap.socketFactory.fallback", "false");

// IMAP provider
properties.setProperty("mail.imap.port", "993");
properties.setProperty("mail.imap.socketFactory.port", "993");

// Use authentication
properties.setProperty("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "192.168.20.4");

Session session = Session.getDefaultInstance(properties, null);
session.setDebug(debug);

Transport myTransport = session.getTransport("smtp");
myTransport.connect("192.168.20.4", "server", "server");

Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress("test@mydomain.com"));

InternetAddress[] addressTo = new InternetAddress[1];
addressTo[0] = new InternetAddress("test@mydomain.com");

msg.setRecipients(Message.RecipientType.TO, addressTo);

msg.setSubject("Test Message");
msg.setContent("Test Content", "text/plain");
myTransport.sendMessage(msg, addressTo);
myTransport.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}


As I mentioned, the connection seems to be established, but
as soon as the MAIL FROM <> command is issued, the server
returns 505 Secure Channel Required. Any insight?    <<Less

Re: Using SSL/TLS with SMTP

Posted By:   venkat_subramanian  
Posted On:   Wednesday, January 16, 2002 05:19 PM


The code below show how to
* Make IMAP connection with SSL
* Make SMTP COnnection with SSL
* Use SMTP authentication


Note: No certificate is required in the JVM as the authenticator is custom.



The IMAP and SMTP SSL ports must be enabled with certificates installed in the mail server.




The DummySSLSocketFactory and the Trust Manager code is taken from an example in Javaworld (http://www.javaworld.com/javaworld/javatips/jw-javatip115.html?remote_addr=209.1.110.48&user_agent=Ultraseek/IDG.net)

Sorry for bad formatting of the code, don't have time to format.
Hope this helps!




/* DummySSLSocketFactory.java */




import com.sun.net.ssl.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class DummySSLSocketFactory extends SSLSocketFactory {
private SSLSocketFactory factory;

public DummySSLSocketFactory() {
System.out.println( "DummySocketFactory instantiated");
try {
SSLContext sslcontext = SSLContext.getInstance( "TLS");
sslcontext.init( null, // No KeyManager required
new TrustManager[] { new DummyTrustManager()},
new java.security.SecureRandom());
factory = ( SSLSocketFactory) sslcontext.getSocketFactory();

} catch( Exception ex) {
ex.printStackTrace();
}
}

public static SocketFactory getDefault() {
return new DummySSLSocketFactory();
}

public Socket createSocket( Socket socket, String s, int i, boolean
flag)
throws IOException {
return factory.createSocket( socket, s, i, flag);
}

public Socket createSocket( InetAddress inaddr, int i,
InetAddress inaddr1, int j) throws IOException {
return factory.createSocket( inaddr, i, inaddr1, j);
}

public Socket createSocket( InetAddress inaddr, int i) throws
IOException {
return factory.createSocket( inaddr, i);
}

public Socket createSocket( String s, int i, InetAddress inaddr, int j)
throws IOException {
return factory.createSocket( s, i, inaddr, j);
}

public Socket createSocket( String s, int i) throws IOException {
return factory.createSocket( s, i);
}

public String[] getDefaultCipherSuites() {
return factory.getSupportedCipherSuites();
}

public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
}





/* DummyTrustManager.java */



import com.sun.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

public class DummyTrustManager implements X509TrustManager {

public boolean isClientTrusted( X509Certificate[] cert) {
return true;
}

public boolean isServerTrusted( X509Certificate[] cert) {
return true;
}

public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[ 0];
}
}









import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.security.*;


public class SSLTester
{

static String host = null;
static String user = null;
static String password = null;
static String actEmail = null;

public SSLTester()
{
sendMessage();
}

public static void main(String argv[])
{
if (argv.length != 4) {
System.out.println("USAGE: SSLTester host user password actEmail");
return;
}
else {

host = argv[0];
user = argv[1];
password = argv[2];
actEmail = argv[3];
}

SSLTester SSLTester = new SSLTester();
}

private void sendMessage()
{
try
{
boolean debug = true;

final String SSL_FACTORY = "DummySSLSocketFactory";
//final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");


// Register SSL security provider
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Security.setProperty("ssl.SocketFactory.provider", SSL_FACTORY);


Properties properties = System.getProperties();


// IMAP provider
properties.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
properties.setProperty("mail.imap.socketFactory.fallback", "false");

// IMAP ports
properties.setProperty("mail.imap.port", "993");
properties.setProperty("mail.imap.socketFactory.port", "993");

// SMTP provider
properties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
properties.setProperty("mail.smtp.socketFactory.fallback", "false");

// SMTP ports
properties.setProperty("mail.smtp.port", "465");
properties.put("mail.smtp.socketFactory.port", "465");

// Use authentication for SMTP
properties.setProperty("mail.smtp.auth", "true");
properties.put("mail.smtp.host", host);


Session session = Session.getDefaultInstance(properties, null);
session.setDebug(debug);


//-------------- Test the IMAP Connection ------------------

// Get a Store object
Store store = session.getStore("imap");

store.connect(host, user, password);

// Open source Folder
Folder folder = store.getFolder("INBOX");

folder.open(Folder.READ_WRITE);

int count = folder.getMessageCount();
System.out.println("*********" + user + " has " + count + "messages");

//-------------- Test the SMTP Connection ------------------

Transport myTransport = session.getTransport("smtp");
myTransport.connect(host, user, password);

Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(actEmail));

InternetAddress[] addressTo = new InternetAddress[1];
addressTo[0] = new InternetAddress(actEmail);

msg.setRecipients(Message.RecipientType.TO, addressTo);

msg.setSubject("Test Message");
msg.setContent("Test Content", "text/plain");
myTransport.sendMessage(msg, addressTo);
myTransport.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}


About | Sitemap | Contact