本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,#javamail-1.4.5-oth-JPR
JavaMail spec:http://www.Oracle.com/technetwork/java/javamail-1-149769.pdf
JAF:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jaf-1.1.1-fcs-oth-JPR
JavaMail下载好后,我们来看一下其主要内容:
[plain] view plain copy print?README.txt:整体介绍JavaMail,需要看一下 docs/javadocs:The JavaMail API javadocs,需要看一下 mail.jar:包括JavaMail API和所有service providers,大部分用户只需要该jar包 lib/mailapi.jar :只有JavaMail API lib/imap.jar:The IMAP service provider lib/smtp.jar:The SMTP service provider lib/pop3.jar:The POP3 service provider lib/dsn.jar:multipart/report DSN message support demo:demo示例,简单了解,有需要再看 %20 %20 %20 JavaMail包含两部分内容,一部分是JavaMail%20API,定义了一组平台无关、独立于通讯协议的邮件程序框架,该部分称为应用级接口,也就是供我们调用的部分,另一部分是service%20provider,该部分使用特定的协议语言来实现第一部分定义的抽象类和接口,这些协议包括:SMTP、NNTP、POP3、IMAP,如果让JavaMail与邮件服务器通信,就需要相应的协议支持,该部分称为服务提供者接口,也就是JavaMail自身需要的协议支持。在使用JavaMail时,通常我们只需将mail.jar放在classpath下使用,它包含了JavaMail%20API部分和SUN自己实现的service%20provider部分。可能也有特殊的时候,我们应用程序中需要自己实现service%20provider部分,那我们只需要mailapi.jar。下面通过几个类来简单认识下JavaMail%20API:[plain] view%20plain copy print?javax.mail.session:上下文环境信息,如服务器的主机名、端口号、协议名称等 javax.mail.Message:邮件模型,发送邮件和接收邮件的媒介,封装了邮件的信息,如发件人、收件人、邮件标题、邮件内容等 javax.mail.Transport:连接邮件SMTP服务器,发送邮件 javax.mail.Store:连接邮件POP3、IMAP服务器,收取邮件 %20 %20 %20 通过这些类,最终就可以实现收发邮件,一个发送邮件的简单示例:[java] view%20plain copy print?public class JavaMailTest1 { public static void main(String[] args) throws MessagingException { Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.163.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); // 设置环境信息 Session session = Session.getInstance(props); // 创建邮件对象 Message msg = new MimeMessage(session); msg.setSubject("JavaMail测试"); // 设置邮件内容 msg.setText("这是一封由JavaMail发送的邮件!"); // 设置发件人 msg.setFrom(new InternetAddress("java_mail_001@163.com")); Transport transport = session.getTransport(); // 连接邮件服务器 transport.connect("java_mail_001", "javamail"); // 发送邮件 transport.sendMessage(msg, new Address[] {new InternetAddress("java_mail_002@163.com")}); // 关闭连接 transport.close(); } } 最终运行后,邮件发送成功。由于我们开启了debug调试,在控制台可以看到JavaMail和服务器之间的交互信息记录,可以发现,和Java Mail(一):telnet实现发送收取邮件中telnet下的命令及服务器反馈信息基本一致。 创建Session对象时可能需要的属性详细信息如下:Name | Type | Description |
---|---|---|
mail.debug | boolean | The initial debug mode. Default is false. |
mail.from | String | The return email address of the current user, used by the InternetAddress methodgetLocalAddress . |
mail.mime.address.strict | boolean | The MimeMessage class uses the InternetAddress method parseHeader to parse headers in messages. This property controls the strict flag passed to theparseHeader method. The default is true. |
mail.host | String | The default host name of the mail server for both Stores and Transports. Used if themail.protocol.host property isn't set. |
mail.store.protocol | String | Specifies the default message access protocol. The Session methodgetStore() returns a Store object that implements this protocol. By default the first Store provider in the configuration files is returned. |
mail.transport.protocol | String | Specifies the default message transport protocol. The Session methodgetTransport() returns a Transport object that implements this protocol. By default the first Transport provider in the configuration files is returned. |
mail.user | String | The default user name to use when connecting to the mail server. Used if the mail.protocol.user property isn't set. |
mail.protocol.class | String | Specifies the fully qualified class name of the provider for the specified protocol. Used in cases where more than one provider for a given protocol exists; this property can be used to specify which provider to use by default. The provider must still be listed in a configuration file. |
mail.protocol.host | String | The host name of the mail server for the specified protocol. Overrides the mail.host property. |
mail.protocol.port | int | The port number of the mail server for the specified protocol. If not specified the protocol's default port number is used. |
mail.protocol.user | String | The user name to use when connecting to mail servers using the specified protocol. Overrides themail.user property. |
更新于2014.01.06 文中示例以及以后的示例中所用的邮箱账户均为在163申请的测试账户,分别为java_mail_001至java_mail_004,密码均为javamail。 本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,转载请注明。
新闻热点
疑难解答