`
JadeLuo
  • 浏览: 425812 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

javamail

    博客分类:
  • java
 
阅读更多
http://www.oracle.com/technetwork/java/javamail/javamail147sslnotes-1917783.html

		Notes for use of SSL with JavaMail
		----------------------------------

JavaMail now supports accessing mail servers over connections secured
using SSL or TLS.  To simplify such access, there are two alternative
approaches to enable use of SSL.

First, and perhaps the simplest, is to set a property to enable use
of SSL.  For example, to enable use of SSL for SMTP connections, set
the property "mail.smtp.ssl.enable" to "true".

Alternatively, you can configure JavaMail to use one of the SSL-enabled
protocol names.  In addition to the non-SSL JavaMail protocols "imap",
"pop3", and "smtp", the protocols "imaps", "pop3s", and "smtps" can
be used to connect to the corresponding services using an SSL
connection.

In addition, the "imap" and "smtp" protocols support use of the
STARTTLS command (see RFC 2487 and RFC 3501) to switch the connection
to be secured by TLS.

Use of the STARTTLS command is preferred in cases where the server
supports both SSL and non-SSL connections.

This SSL/TLS support in JavaMail works only when JavaMail is used on
a version of J2SE that includes SSL support.  We have tested this
support on J2SE 1.4 and newer, which include SSL support.  The
SSL support is provided by the JSSE package, which is also available
for earlier versions of J2SE.  We have not tested such configurations.

-- STARTTLS support

The STARTTLS support is available in the standard "imap" and "smtp"
protocols, but must be enabled by setting the appropriate property,
mail.imap.starttls.enable or mail.smtp.starttls.enable, to "true".
When set, if the server supports the STARTTLS command, it will be
used after making the connection and before sending any login
information.


-- Secure protocols

When using the new protocol names, configuration properties must also use
these protocol names.  For instance, set the property "mail.smtps.host"
to specify the host name of the machine to connect to when using the
"smtps" protocol for SMTP over SSL.  Similarly, to set the IMAP protocol
timeout when using the "imaps" protocol for IMAP over SSL, set the property
"mail.imaps.timeout".  See the package documentation for the different
protocol packages for the list of available properties, which are
always set using property names of the form mail.<protocol>.<property>.

The Transport.send method will use the default transport protocol,
which remains "smtp".  To enable SMTP connections over SSL, set the
"mail.smtp.ssl.enable" property to "true".  This is usually the easiest
approach.

Alternatively, to change the default transport protocol 
returned by the Session.getTransport() method to SMTP over SSL, set
the property "mail.transport.protocol" to "smtps".  To change the
transport used for internet addresses (as returned by the
Session.getTransport(Address) method, and used by the Transport.send
method), use

	session.setProtocolForAddress("rfc822", "smtps");


-- Trusted Certificates

To establish an SSL/TLS connection, the JavaMail client must be able
to verify that the security certificate presented by the server
it is connecting to is "trusted" by the client.  Trusted certificates
are maintained in a Java keystore file on the client.  The J2SE
SDK "keytool" command is used to maintain the keystore file.

There are two common approaches for verifying server certificates.
The first approach is probably most common for servers accessible to
partners outside a company.  The second approach is probably most
common for servers used within a company.

1. Server certificates may be signed be a well known public
   Certificate Authority.  The default Java keystore file contains
   the public keys of well known Certificate Authorities and can
   verify the server's certificate by following the chain of
   certificates signing the server's certificate back to one of
   these well known CA certificates.

   In this case the client doesn't need to manage certificates
   explicitly but can just use the default keystore file.

2. Server certificates may be "self-signed".  In this case there is
   no chain of signatures to use in verifying the server's certificate.
   Instead, the client will need the server's certificate in the
   client's keystore file.  The server's certificate is imported into
   the keystore file once, using the keytool command, and after that
   is used to verify connections to the server.  A single keystore file
   may contain certificates of many servers.

   In this case the client will need to set the appropriate System
   properties to point to the client's keystore file containing the
   trusted certificate.  These properties can be set when invoking
   the "java" command, or can be set programmatically.  For example,

	java -Djavax.net.ssl.trustStore=$HOME/.keystore ...

   See the JSSE Reference Guide for details:
   http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#CustomizingStores


-- Server Identity Check

RFC 2595 specifies addition checks that must be performed on the
server's certificate to ensure that the server you connected to is
the server you intended to connect to.  This reduces the risk of
"man in the middle" attacks.  For compatibility with earlier releases
of JavaMail, these additional checks are disabled by default.  We
strongly recommend that you enable these checks when using SSL.  To
enable these checks, set the "mail.<protocol>.ssl.checkserveridentity"
property to "true".


-- Socket Factories

In earlier releases it was necessary to explicitly set a socket
factory property to enable use of SSL.  In almost all cases, this
is no longer necessary.  SSL support is built in.  However, there
is one case where a special socket factory may be needed.

JavaMail now includes a special SSL socket factory that can simplify
dealing with servers with self-signed certificates.  While the
recommended approach is to include the certificate in your keystore
as described above, the following approach may be simpler in some cases.

The class com.sun.mail.util.MailSSLSocketFactory can be used as a
simple socket factory that allows trusting all hosts or a specific set
of hosts.  For example:

	MailSSLSocketFactory sf = new MailSSLSocketFactory();
	sf.setTrustAllHosts(true);
	// or
	// sf.setTrustedHosts(new String[] { "my-server" });
	props.put("mail.smtp.ssl.enable", "true");
	// also use following for additional safety
	//props.put("mail.smtp.ssl.checkserveridentity", "true");
	props.put("mail.smtp.ssl.socketFactory", sf);

Use of MailSSLSocketFactory avoids the need to add the certificate to
your keystore as described above, or configure your own TrustManager
as described below.


-- Debugging

Debugging problems with certificates and keystores can be difficult.
The JSSE Reference Guide contains information on debugging utilities
that can help.  See:
http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#Debug

There are some debugging options in the JDK that can help, depending
on the sorts of problems you're having.  Setting the following system
properties will produce additional debugging output:

	java.security.debug=certpath
	javax.net.debug=trustmanager 

Set these on the command line when you run your program using, for example:

	java -Djava.security.debug=certpath -Djavax.net.debug=trustmanager ...


-- keytool Usage

Given a certificate for the server as used in case #2 above, you can
import this certificate into your Java keystore file using a command
such as:

	keytool -import -alias imap-server -file imap.cer

The keytool command can also be used to generate a self-signed certificate
that can be used by your mail server, if you're setting up your own server.
Other utilities, such as those included with the OpenSSL package, can also
be used to generate such certificates, and they can be imported into the
Java keystore using keytool.

For more information on using the keytool command, see the keytool
reference pages at:
http://download.oracle.com/javase/6/docs/technotes/guides/security/index.html


-- Configuring Your Own Trust Manager

When using SSL/TLS, it's important to ensure that the server you connect
to is actually the server you expected to connect to, to prevent "man in
the middle" attacks on your communication.  The recommended technique is
to configure the Java keystore using one of the methods described above.
If, for some reason, that approach is not workable, it's also possible
to configure the SSL/TLS implementation to use your own TrustManager
class to evaluate whether to trust the server you've connected to.

The following "dummy" classes illustrate the framework necessary to create
your own TrustManager implementation.

First, a replacement for the standard SSLSocketFactory is needed, to allow
you to specify which TrustManager to use:

==> DummySSLSocketFactory.java <==

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.*;


/**
 * DummySSLSocketFactory
 */
public class DummySSLSocketFactory extends SSLSocketFactory {
    private SSLSocketFactory factory;

    public DummySSLSocketFactory() {
	try {
	    SSLContext sslcontext = SSLContext.getInstance("TLS");
	    sslcontext.init(null,
				 new TrustManager[] { new DummyTrustManager()},
				 null);
	    factory = (SSLSocketFactory)sslcontext.getSocketFactory();
	} catch(Exception ex) {
	    // ignore
	}
    }

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

    public Socket createSocket() throws IOException {
	return factory.createSocket();
    }

    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.getDefaultCipherSuites();
    }

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


Next you need the actual implementation of the TrustManager.  This dummy
trust manager trusts anything.  THIS IS NOT SECURE!!!

==> DummyTrustManager.java <==

import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;


/**
 * DummyTrustManager - NOT SECURE
 */
public class DummyTrustManager implements X509TrustManager {

    public void checkClientTrusted(X509Certificate[] cert, String authType) {
	// everything is trusted
    }

    public void checkServerTrusted(X509Certificate[] cert, String authType) {
	// everything is trusted
    }

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

Finally, you need to configure JavaMail to use your SSLSocketFactory.
Set the appropriate protocol-specific property, e.g.,

    props.setProperty("mail.imap.ssl.enable", "true");
    props.setProperty("mail.imap.ssl.socketFactory.class",
					"DummySSLSocketFactory");
    props.setProperty("mail.imap.ssl.socketFactory.fallback", "false");
    Session session = Session.getInstance(props, null);

Similar properties would need to be set to use other protocols.
分享到:
评论

相关推荐

    javamail(带界面的javamail)

    JavaMail 是一个强大的开源库,用于在Java应用程序中实现电子邮件的发送和接收功能。它提供了丰富的API,使得开发者能够方便地处理SMTP、POP3、IMAP等邮件协议,支持多种邮件格式,包括文本、HTML以及带有附件的邮件...

    javamail 所有jar包

    JavaMail 是一个开源的 Java API,它为开发者提供了在 Java 应用程序中发送和接收电子邮件的功能。这个API包括了多种协议的支持,如SMTP(简单邮件传输协议)、POP3(邮局协议)和IMAP(因特网消息访问协议)。在...

    javamail 1.4.3 mail.jar 1.4.3 jar 包+源代码下载 天涯浪子

    JavaMail 是一个开源的 Java API,它为Java程序员提供了邮件收发功能,使得开发者能够方便地集成电子邮件功能到他们的应用程序中。这个标题提到的是JavaMail的1.4.3版本,其中包含了mail.jar库文件以及对应的源代码...

    JavaMail所需最新版(1.5.4)jar包

    JavaMail 是一个开源的 Java API,它允许开发者在 Java 应用程序中发送和接收电子邮件。这个【标题】"JavaMail所需最新版(1.5.4)jar包"指的是包含JavaMail 1.5.4版本所需的所有库文件的集合,确保开发者能够使用这...

    JavaMail API 1.4.7 官网下载 最新版

    JavaMail API 1.4.7 是一个广泛使用的开源库,专为Java开发者设计,用于处理电子邮件相关的任务。这个版本的API提供了丰富的功能,包括发送、接收邮件,管理邮箱,处理附件,以及支持多种邮件协议如SMTP(简单邮件...

    使用JAVAMail代发邮件

    使用 JavaMail 代发邮件 使用 JavaMail 库可以轻松地在 Java 应用程序中发送电子邮件。JavaMail 是一个 Java API,用于在 Java 应用程序中发送和接收电子邮件。它提供了一个抽象层,允许开发者使用不同的电子邮件...

    JavaMail-1.5 API

    JavaMail API是Java平台上用于发送和接收电子邮件的一个标准扩展库。它并不是Java标准库的一部分,而是Java企业版(J2EE)中的一部分。JavaMail提供了一系列用于处理电子邮件的类和接口,允许开发者在Java应用程序中...

    JavaMail实现源代码和jar包

    JavaMail 是一个开源的 Java API,它允许 Java 程序员通过 SMTP、POP3 或 IMAP 协议发送、接收和管理电子邮件。这个压缩包包含了一组与 JavaMail 相关的源代码和必要的库文件,使得开发者可以快速地理解和实现邮件...

    javamail的jar包:javamail-1.6

    JavaMail 是一个开源库,用于在Java应用程序中处理电子邮件。这个库提供了丰富的API,使得开发者可以方便地执行发送、接收、读取和管理邮件的任务。`javamail-1.6`是JavaMail的一个版本,它包含了从早期版本中继承的...

    JavaMail+JAF+JavaMail_API+JavaMail_API详解

    JavaMail 是一个开源的Java库,它为Java程序员提供了处理电子邮件的能力。这个库不仅支持SMTP、POP3和IMAP协议,还支持其他的邮件系统协议,如NNTP和EWS。JavaMail API是JavaMail库的核心部分,它提供了一系列的接口...

    javamail收发邮件加密和不加密

    JavaMail 是一个强大的开源库,用于在Java应用程序中处理电子邮件。它支持多种协议,包括POP3、IMAP和SMTP,这些协议分别用于接收、检索和发送邮件。在使用JavaMail时,了解如何进行加密和非加密通信对于确保数据...

    基于Javamail的邮件收发系统.zip

    基于Javamail的邮件收发系统.zip基于Javamail的邮件收发系统.zip基于Javamail的邮件收发系统.zip基于Javamail的邮件收发系统.zip基于Javamail的邮件收发系统.zip基于Javamail的邮件收发系统.zip基于Javamail的邮件...

    javaMail API 跟 javaMail.jar包

    JavaMail API是Java平台上用于处理电子邮件的一套标准API,它为开发者提供了丰富的接口和类,以便于发送、接收和管理电子邮件。JavaMail API是Java EE的一部分,但也可以在Java SE环境中使用。`javaMail.jar`是...

    javamail 回执邮件资料

    JavaMail 是一个开源的 Java 库,用于处理电子邮件的发送和接收。它提供了与 SMTP、POP3 和 IMAP 协议交互的接口,是开发基于 Java 的电子邮件应用的基础。回执邮件是邮件服务中的一项功能,它允许发件人在发送邮件...

    javamail.jar,javamail资源包

    JavaMail 是一个开源的 Java API,它为Java开发者提供了处理电子邮件的能力。这个API允许开发者发送、接收、管理邮件,包括附件、HTML 内容、MIME 多部分消息等。`javamail.jar` 文件是JavaMail的核心库,包含了所有...

    JavaMail配置SSL服务器及安装证书

    JavaMail是一种广泛使用的Java库,用于在应用程序中发送和接收电子邮件。配置JavaMail与SSL(Secure Socket Layer)服务器配合使用,是为了确保邮件通信的安全性,防止数据在传输过程中被窃取或篡改。SSL提供了加密...

    JavaMail(JAVA邮件服务) API详解

    JavaMail API是Java平台上用于处理电子邮件的一套强大的库,它为开发者提供了丰富的功能,使得在应用程序中发送和接收邮件变得简单。本文将深入探讨JavaMail API的各个方面,帮助你理解和掌握这一关键工具。 首先,...

    javamail API帮助文档

    JavaMail API是Java平台上用于处理电子邮件的一套强大的开源库。这个API允许开发人员在Java应用程序中创建、发送、接收和管理邮件。它提供了与多种邮件协议(如SMTP、POP3和IMAP)交互的能力,使得Java开发者无需...

    JavaMail相关jar包

    JavaMail是Java编程语言中用于处理电子邮件的API,它提供了丰富的功能,允许开发人员发送、接收和管理电子邮件。本篇文章将深入探讨JavaMail的核心概念、关键组件以及如何使用这两个特定的jar包——`jaf-1_1_1.zip`...

    javamail相关JAR包

    JavaMail是Java编程环境中用于处理电子邮件的一套API,它提供了丰富的功能,允许开发者发送、接收和操作邮件。在JavaMail的实现中,通常会用到三个关键的JAR包:`activation-1.1.jar`、`javax.mail-api-1.5.6.jar`和...

Global site tag (gtag.js) - Google Analytics