Java Mail(二):JavaMail介绍及发送一封简单邮件
来源:互联网 发布:java 文件读入字符串 编辑:程序博客网 时间:2024/05/01 20:06
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,转载请注明。
JavaMail是SUN提供给开发人员在应用程序中实现邮件发送和接收功能而提供的一套标准开发类库,支持常用的邮件协议,如SMTP、POP3、IMAP,开发人员使用JavaMail编写邮件程序时,无需考虑底层的通信细节(Socket),JavaMail也提供了能够创建出各种复杂MIME格式的邮件内容的API。使用JavaMail,我们可以实现类似OutLook、FoxMail的软件。虽然JavaMail(仅支持JDK4及以上)也是Java的API之一,但是却没有直接加入到JDK中,所以我们需要另行下载。另外,JavaMail依赖JAF(JavaBeans Activation Framework),JAF在Java6之后已经合并到JDK中,而JDK5之前需要另外下载JAF的类库。下载地址如下:
JavaMail:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#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下载好后,我们来看一下其主要内容:
- 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示例,简单了解,有需要再看
- javax.mail.Session:上下文环境信息,如服务器的主机名、端口号、协议名称等
- javax.mail.Message:邮件模型,发送邮件和接收邮件的媒介,封装了邮件的信息,如发件人、收件人、邮件标题、邮件内容等
- javax.mail.Transport:连接邮件SMTP服务器,发送邮件
- javax.mail.Store:连接邮件POP3、IMAP服务器,收取邮件
- 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();
- }
- }
创建Session对象时可能需要的属性详细信息如下:
InternetAddress
methodgetLocalAddress
.mail.mime.address.strictbooleanThe 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.hostStringThe default host name of the mail server for both Stores and Transports. Used if themail.protocol.host
property isn't set.mail.store.protocolStringSpecifies 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.protocolStringSpecifies 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.userStringThe default user name to use when connecting to the mail server. Used if the mail.protocol.user
property isn't set.mail.protocol.classStringSpecifies 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.hostStringThe host name of the mail server for the specified protocol. Overrides themail.host
property.mail.protocol.portintThe port number of the mail server for the specified protocol. If not specified the protocol's default port number is used.mail.protocol.userStringThe 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,转载请注明。
- Java Mail(二):JavaMail介绍及发送一封简单邮件
- Java Mail(二):JavaMail介绍及发送一封简单邮件
- Java Mail(二):JavaMail介绍及发送一封简单邮件
- Java Mail(二):JavaMail介绍及发送一封简单邮件
- Java Mail(二):JavaMail介绍及发送一封简单邮件
- 【java邮件】JavaMail介绍及发送一封简单邮件
- JavaMail介绍及发送一封简单邮件
- JavaMail介绍及发送一封简单邮件
- 使用JavaMail发送一封简单的邮件
- JavaMail-发送一封简单邮件(附带附件)
- 用JavaMail发送一封邮件
- 简单java mail发送邮件
- Java-使用JavaMail发送一封邮件给用户邮箱功能实现
- 【javaMail】【3.多线程发邮件】java mail 多线程发送邮件
- 【javaMail】【2.发HTML邮件】java mail 发送HTML邮件
- JavaMail发送邮件API简单介绍
- JavaMail邮件发送-将邮件保存到本地和发送一封本地邮件
- Java邮件开发(一):使用JMail发送一封简单邮件
- 【产品经理】总结产品经理职责和素质
- ibatis 中 $与#的区别
- Android——Service 全面总结
- iTween动画库:关于Move移动篇
- log4cplus的安装与使用初步
- Java Mail(二):JavaMail介绍及发送一封简单邮件
- 2014-10-27
- Java Mail(三):Session、Message详解
- IOS征途之c语言-C语言关键字
- 正则表达式检查IP地址合法性
- 关于C++解压和压缩
- c++ DFT 二维傅里叶变换
- Java Mail(一):telnet实现发送收取邮件
- php中不同情况下整形变量可当成整形也可是字符串型来使用