利用java mail发送邮件(转)
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的类库。下载地址例如以下:
https://java.net/projects/javamail/pages/Home#Download_JavaMail_1.5.2_Release
欢迎訪问www.findspace.name来获取很多其它信息
利用这个api。之前写的那个抓取站点更新的工具就有了邮件通知管理员的功能啦~
事实上仅仅须要那个javax.mail.jar,把这个jar包导入到你的项目以下就可以。
JavaMail包括两部分内容。一部分是JavaMail API,定义了一组平台无关、独立于通讯协议的邮件程序框架,该部分称为应用级接口,也就是供我们调用的部分。还有一部分是service provider。该部分使用特定的协议语言来实现第一部分定义的抽象类和接口。这些协议包括:SMTP、NNTP、POP3、IMAP,假设让JavaMail与邮件server通信。就须要对应的协议支持,该部分称为服务提供者接口。也就是JavaMail自身须要的协议支持。
在使用JavaMail时,通常我们仅仅需将mail.jar放在classpath下使用。它包括了JavaMail
API部分和SUN自己实现的service provider部分。可能也有特殊的时候,我们应用程序中须要自己实现service provider部分,那我们仅仅须要mailapi.jar。以下通过几个类来简单认识下JavaMail API:
javax.mail.Session:上下文环境信息,如server的主机名、端口号、协议名称等
javax.mail.Message:邮件模型。发送邮件和接收邮件的媒介,封装了邮件的信息。如发件人、收件人、邮件标题、邮件内容等
javax.mail.Transport:连接邮件SMTPserver,发送邮件
javax.mail.Store:连接邮件POP3、IMAPserver,收取邮件
通过这些类。终于就能够实现收发邮件,一个发送邮件的简单演示样例:
public class JavaMailTest1 {
public static void main(String[] args) throws MessagingException {
Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送server须要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件server主机名
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();
// 连接邮件server
transport.connect("java_mail_001", "javamail");
// 发送邮件
transport.sendMessage(msg, new Address[] {new InternetAddress("java_mail_002@163.com")});
// 关闭连接
transport.close();
}
}
终于执行后。邮件发送成功。因为我们开启了debug调试。在控制台能够看到JavaMail和server之间的交互信息记录。
创建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 theInternetAddressmethodgetLocalAddress. |
| mail.mime.address.strict | boolean | The MimeMessage class uses the InternetAddress methodparseHeader toparse headers in messages. This property controls the strict flag passed to the parseHeader 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 propertyisn’t set. |
| mail.store.protocol | String | Specifies the default message access protocol. The SessionmethodgetStore()returnsa 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. TheSessionmethodgetTransport() returnsa 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 themail.protocol.user propertyisn’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 themail.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. |
本文来自:高爽|Coder。原文地址:http://blog.csdn.net/ghsau/article/details/17839983,转载请注明。
利用java mail发送邮件(转)的更多相关文章
- 利用java mail发送邮件
import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import java ...
- Java网络编程:利用Java mail包发送电子邮件
下面代码是利用Java mail包封装了一个发送邮件的类 import java.io.File; import java.util.ArrayList; import java.util.Date; ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件
文章目录 1. Spring Boot 集成 Java Mail 2. 单元测试 3. 源代码 Spring 对 Java Mail 有很好的支持.因此,Spring Boot 也提供了自动配置的支持 ...
- java mail(发送邮件--163邮箱)
package com.util.mail; /** * 发送邮件需要使用的基本信息 */ import java.util.Properties; public class MailSenderIn ...
- java mail发送邮件
最近做了自动发送邮件功能,带附件的:需要的jar包有
- 使用Java Mail发送邮件
本笔记参考自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983 JavaMail是SUN提供给开发人员在应用程序中实现 ...
- 简单的java mail发送邮件实例
mail.jar ,commons-email-X.X.jar ,activation.jar ,log4j.jar 这四个jar,放进项目里 下载地址 http://www.oracle.com/ ...
- Java Mail 发送邮件(SSL加密方式,TSL加密方式)
一.一般配置 发送邮件需要用到 mail包 maven 依赖如下: <!-- https://mvnrepository.com/artifact/javax.mail/mail --> ...
- 使用java mail 发送邮件
1.关联jar包: activation.jar mail.jar 2.调用 @Test public void test1() { List<String> imageUrlLi ...
随机推荐
- 如何运用GitHub来提高生产效率
这是一篇GitHub的入门级文章,主要针对git的初学者.我们将讨论初学者最关心的一些问题,如:为什么我们要使用GitHub,它的应用有哪些,如何运用它去帮助我们提高工作效率,以及它的基本用法有哪些. ...
- scrapy爬虫框架之理解篇(个人理解)
提问: 为什么使用scrapy框架来写爬虫 ? 在python爬虫中:requests + selenium 可以解决目前90%的爬虫需求,难道scrapy 是解决剩下的1 ...
- ldap数据库--ODSEE--卸载
针对ldap实例的卸载,即删除,可以通过管理界面进行操作也可以通过命令行进行操作.卸载顺序为ldap实例--agent--ads.这里主要介绍命令操作步骤 1,ldap实例卸载 从ads注销,即不在需 ...
- Python中的切片符
最近在学python,感觉切片符有点难以理解.在网上查了点资料,然后做个总结 理解切片符,首先得知道数组是从0开始的, 而且切片符最后一个是-1. 我们先定义个数组 a=[1,2,3,4,5] 切 ...
- Layui框架+PHP打造个人简易版网盘系统
网盘系统 大家应该都会注册过致命的一些网盘~如百度云.百科介绍:网盘,又称网络U盘.网络硬盘,是由互联网公司推出的在线存储服务,服务器机房为用户划分一定的磁盘空间,为用户免费或收费提供文件的存储. ...
- 通过正则表达式提取excel特定列中含有关键字的所有行数据
在 Excel 中打开需要提取数据excel文件,使用 Alt+F11 快捷键打开 VBA 项目窗口,在左侧的工作表名称上点右键,选择查看代码,即可出现右侧的编辑代码窗口(如下图) 在代码窗口中输入以 ...
- 微信小程序入门(一)
想必当你对官方文档了解地差不多的时候,一颗跃跃欲试的心就开始骚动了吧. 开发小程序之前的准备工作: 1).准备一个域名 2).准备一台云服务器 3).搭建小程序的后台,博主的小程序后台请求的的是自己写 ...
- spring boot跨域设置
定义 跨域是指从一个域名的网页去请求另一个域名的资源 跨域背景 限制原因 如果一个网页可以随意地访问另外一个网站的资源,那么就有可能在客户完全不知情的情况下出现安全问题 为什么要跨域 公司内部有多个不 ...
- [转载] HDFS简介
转载自http://www.csdn.net/article/2010-11-26/282582 http://subject.csdn.net/hadoop/ 一.HDFS的基本概念 1.1.数据块 ...
- shell全自动登录远程终端
先看效果 你需要做的事情,在配置文件中配置服务器信息,选择对应的服务器,进行连接. 传统手工连接 #密码方式 ssh user@ip # 然后输入服务器密码 #密钥登录 ssh -i identity ...