- 浏览: 141336 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (129)
- javaee struts2 (3)
- java mail (2)
- spring mail (4)
- 下拉列表 (1)
- MySQL (3)
- MyEclipse (1)
- java script (5)
- js html (4)
- oracle (7)
- eclipse ide (1)
- 正则表达式 (1)
- oracle 安装 (0)
- ExtJS 4.0 (1)
- java (5)
- linux (13)
- asterisk (25)
- asterisk AMI (1)
- java socket (1)
- sip (6)
- jsonp (1)
- mongodb (12)
- http (3)
- jQuery (2)
- java timer (2)
- OSGi (2)
- Firefox (1)
- tomcat (1)
- win7 (0)
- Highcharts (2)
- SVN (2)
- webstorm (1)
- AngularJS (1)
- 搜索引擎 (1)
- wordpress (1)
- 语音编码 (1)
- idea (1)
- dom4j (1)
- QQ (1)
- cti 呼叫中心 (3)
- 敏感词检测 (1)
- asterisk sip tls (1)
- nginx (1)
- FreeSWITCH (1)
最新评论
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。
包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息,请参考Javadocs。
为了使用JavaMail中的一些特色, 比如MIME类型的信件, Spring提供了MailSender的一个子接口, 即org.springframework.mail.javamail.JavaMailSender。
Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator, 用于准备JavaMail的MIME信件。
1.发送简单的文本邮件
001 |
package net.xftzr.mail; |
002 |
import java.util.Properties; |
003 |
|
004 |
|
005 |
import org.springframework.mail.SimpleMailMessage; |
006 |
import org.springframework.mail.javamail.JavaMailSenderImpl; |
007 |
|
008 |
|
009 |
/** |
010 |
* 本类测试简单邮件
|
011 |
* 直接用邮件发送
|
012 |
* @author Administrator
|
013 |
*
|
014 |
*/
|
015 |
public class SingleMailSend { |
016 |
public static void main(String args[]){
|
017 |
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
|
018 |
// 设定mail server
|
019 |
senderImpl.setHost( " smtp.163.com " );
|
020 |
|
021 |
// 建立邮件消息
|
022 |
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
023 |
// 设置收件人,寄件人 用数组发送多个邮件
|
024 |
// String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
|
025 |
// mailMessage.setTo(array);
|
026 |
mailMessage.setTo( " toEmail@sina.com " );
|
027 |
mailMessage.setFrom( " userName@163.com " );
|
028 |
mailMessage.setSubject( " 测试简单文本邮件发送! " );
|
029 |
mailMessage.setText( " 测试我的简单邮件发送机制!! " );
|
030 |
|
031 |
senderImpl.setUsername( " userName " ) ; // 根据自己的情况,设置username
|
032 |
senderImpl.setPassword( " password " ) ; // 根据自己的情况, 设置password
|
033 |
|
034 |
Properties prop = new Properties() ;
|
035 |
prop.put( " mail.smtp.auth " , " true " ) ; // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
|
036 |
prop.put( " mail.smtp.timeout " , " 25000 " ) ;
|
037 |
senderImpl.setJavaMailProperties(prop);
|
038 |
// 发送邮件
|
039 |
senderImpl.send(mailMessage);
|
040 |
|
041 |
System.out.println( " 邮件发送成功.. " );
|
042 |
}
|
043 |
}
|
044 |
|
045 |
|
046 |
|
047 |
|
048 |
2.发送简单的html邮件 |
049 |
org.springframework.mail.javamail.MimeMessageHelper是处理JavaMail邮件常用的顺手组件之一。它可以让你摆脱繁复的javax.mail.internetAPI类 |
050 |
package net.xftzr.mail; |
051 |
|
052 |
|
053 |
|
054 |
|
055 |
import java.util.Properties; |
056 |
|
057 |
|
058 |
import javax.mail.internet.MimeMessage; |
059 |
import org.springframework.mail.javamail.JavaMailSenderImpl; |
060 |
import org.springframework.mail.javamail.MimeMessageHelper; |
061 |
/** |
062 |
* 本类测试html邮件
|
063 |
* @author wangjianme
|
064 |
*
|
065 |
*/ |
066 |
public class HTMLMailDemo { |
067 |
/** |
068 |
* @param args |
069 |
*/ |
070 |
public static void main(String[] args) throws Exception{ |
071 |
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
|
072 |
|
073 |
//设定mail server
|
074 |
senderImpl.setHost( "smtp.163.com" );
|
075 |
|
076 |
//建立邮件消息,发送简单邮件和html邮件的区别
|
077 |
MimeMessage mailMessage = senderImpl.createMimeMessage();
|
078 |
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
|
079 |
|
080 |
//设置收件人,寄件人
|
081 |
messageHelper.setTo( "Mailto@sina.com" );
|
082 |
messageHelper.setFrom( "username@163.com" );
|
083 |
messageHelper.setSubject( "测试HTML邮件!" );
|
084 |
//true 表示启动HTML格式的邮件
|
085 |
messageHelper.setText( "<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>" , true );
|
086 |
|
087 |
senderImpl.setUsername( "username" ) ; // 根据自己的情况,设置username
|
088 |
senderImpl.setPassword( "password" ) ; // 根据自己的情况, 设置password
|
089 |
Properties prop = new Properties() ;
|
090 |
prop.put( "mail.smtp.auth" , "true" ) ; // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
|
091 |
prop.put( "mail.smtp.timeout" , "25000" ) ;
|
092 |
senderImpl.setJavaMailProperties(prop);
|
093 |
//发送邮件
|
094 |
senderImpl.send(mailMessage);
|
095 |
|
096 |
System.out.println( "邮件发送成功.." );
|
097 |
} |
098 |
} |
099 |
|
100 |
|
101 |
|
102 |
|
103 |
3.发送嵌套图片的邮件 |
104 |
|
105 |
|
106 |
Email允许添加附件,也允许在multipart信件中内嵌资源。内嵌资源可能是你在信件中希望使用的图像,或者样式表,但是又不想把它们作为附件。 |
107 |
package net.xftzr.mail; |
108 |
|
109 |
|
110 |
import java.io.File; |
111 |
import java.util.Properties; |
112 |
|
113 |
|
114 |
import javax.mail.internet.MimeMessage; |
115 |
import org.springframework.core.io.FileSystemResource; |
116 |
import org.springframework.mail.javamail.JavaMailSenderImpl; |
117 |
import org.springframework.mail.javamail.MimeMessageHelper; |
118 |
/** |
119 |
* 本类测试邮件中嵌套图片
|
120 |
* @author wangjianme
|
121 |
*
|
122 |
*/ |
123 |
public class AttachedImageMail { |
124 |
public static void main(String[] args) throws Exception{ |
125 |
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
|
126 |
|
127 |
//设定mail server
|
128 |
senderImpl.setHost( "smtp.163.com" );
|
129 |
|
130 |
|
131 |
//建立邮件消息,发送简单邮件和html邮件的区别
|
132 |
MimeMessage mailMessage = senderImpl.createMimeMessage();
|
133 |
//注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,
|
134 |
//multipart模式
|
135 |
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true );
|
136 |
|
137 |
//设置收件人,寄件人
|
138 |
messageHelper.setTo( "toMail@sina.com" );
|
139 |
messageHelper.setFrom( "username@163.com" );
|
140 |
messageHelper.setSubject( "测试邮件中嵌套图片!!" );
|
141 |
//true 表示启动HTML格式的邮件
|
142 |
messageHelper.setText( "<html><head></head><body><h1>hello!!spring image html mail</h1>" +
|
143 |
"<img src=\"cid:aaa\"/></body></html>" , true );
|
144 |
|
145 |
FileSystemResource img = new FileSystemResource( new File( "g:/123.jpg" ));
|
146 |
|
147 |
messageHelper.addInline( "aaa" ,img);
|
148 |
|
149 |
senderImpl.setUsername( "username" ) ; // 根据自己的情况,设置username
|
150 |
senderImpl.setPassword( "password" ) ; // 根据自己的情况, 设置password
|
151 |
Properties prop = new Properties() ;
|
152 |
prop.put( "mail.smtp.auth" , "true" ) ; // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
|
153 |
prop.put( "mail.smtp.timeout" , "25000" ) ;
|
154 |
senderImpl.setJavaMailProperties(prop);
|
155 |
|
156 |
//发送邮件
|
157 |
senderImpl.send(mailMessage);
|
158 |
|
159 |
System.out.println( "邮件发送成功.." );
|
160 |
} |
161 |
} |
162 |
|
163 |
|
164 |
|
165 |
|
166 |
4.发送包含附件的邮件 |
167 |
package net.xftzr.mail; |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
import java.io.File; |
175 |
import java.util.Properties; |
176 |
|
177 |
|
178 |
import javax.mail.internet.MimeMessage; |
179 |
import org.springframework.core.io.FileSystemResource; |
180 |
import org.springframework.mail.javamail.JavaMailSenderImpl; |
181 |
import org.springframework.mail .javamail.MimeMessageHelper; |
182 |
public class AttachedFileMail { |
183 |
/** |
184 |
* 本类测试的是关于邮件中带有附件的例子 |
185 |
* @param args |
186 |
*/ |
187 |
public static void main(String[] args) throws Exception{ |
188 |
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
|
189 |
|
190 |
//设定mail server
|
191 |
senderImpl.setHost( "smtp.163.com" );
|
192 |
//建立邮件消息,发送简单邮件和html邮件的区别
|
193 |
MimeMessage mailMessage = senderImpl.createMimeMessage();
|
194 |
//注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,
|
195 |
//multipart模式 为true时发送附件 可以设置html格式
|
196 |
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true , "utf-8" );
|
197 |
|
198 |
//设置收件人,寄件人
|
199 |
messageHelper.setTo( "toMail@sina.com" );
|
200 |
messageHelper.setFrom( "username@163.com" );
|
201 |
messageHelper.setSubject( "测试邮件中上传附件!!" );
|
202 |
//true 表示启动HTML格式的邮件
|
203 |
messageHelper.setText( "<html><head></head><body><h1>你好:附件中有学习资料!</h1></body></html>" , true );
|
204 |
|
205 |
FileSystemResource file = new FileSystemResource( new File( "g:/test.rar" ));
|
206 |
//这里的方法调用和插入图片是不同的。
|
207 |
messageHelper.addAttachment( "test.rar" ,file);
|
208 |
|
209 |
senderImpl.setUsername( "username" ) ; // 根据自己的情况,设置username
|
210 |
senderImpl.setPassword( "password" ) ; // 根据自己的情况, 设置password
|
211 |
Properties prop = new Properties() ;
|
212 |
prop.put( "mail.smtp.auth" , "true" ) ; // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
|
213 |
prop.put( "mail.smtp.timeout" , "25000" ) ;
|
214 |
senderImpl.setJavaMailProperties(prop);
|
215 |
//发送邮件
|
216 |
senderImpl.send(mailMessage);
|
217 |
|
218 |
System.out.println( "邮件发送成功.." );
|
219 |
} |
220 |
} /**
|
221 |
* 本类测试的是关于邮件中带有附件的例子 |
222 |
* @param args |
223 |
*/ |
224 |
public static void main(String[] args) throws Exception{ |
225 |
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
|
226 |
|
227 |
//设定mail server
|
228 |
senderImpl.setHost( "smtp.163.com" );
|
229 |
//建立邮件消息,发送简单邮件和html邮件的区别
|
230 |
MimeMessage mailMessage = senderImpl.createMimeMessage();
|
231 |
//注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,
|
232 |
//multipart模式 为true时发送附件 可以设置html格式
|
233 |
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true , "utf-8" );
|
234 |
|
235 |
//设置收件人,寄件人
|
236 |
messageHelper.setTo( "toMail@sina.com" );
|
237 |
messageHelper.setFrom( "username@163.com" );
|
238 |
messageHelper.setSubject( "测试邮件中上传附件!!" );
|
239 |
//true 表示启动HTML格式的邮件
|
240 |
messageHelper.setText( "<html><head></head><body><h1>你好:附件中有学习资料!</h1></body></html>" , true );
|
241 |
|
242 |
FileSystemResource file = new FileSystemResource( new File( "g:/test.rar" ));
|
243 |
//这里的方法调用和插入图片是不同的。
|
244 |
messageHelper.addAttachment( "test.rar" ,file);
|
245 |
|
246 |
senderImpl.setUsername( "username" ) ; // 根据自己的情况,设置username
|
247 |
senderImpl.setPassword( "password" ) ; // 根据自己的情况, 设置password
|
248 |
Properties prop = new Properties() ;
|
249 |
prop.put( "mail.smtp.auth" , "true" ) ; // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
|
250 |
prop.put( "mail.smtp.timeout" , "25000" ) ;
|
251 |
senderImpl.setJavaMailProperties(prop);
|
252 |
//发送邮件
|
253 |
senderImpl.send(mailMessage);
|
254 |
|
255 |
System.out.println( "邮件发送成功.." );
|
256 |
} |
257 |
} |
相关推荐
邮件发送支持以 HTML 的形式去构建我们喜欢的文本格式,Spring 对 HTML 格式的邮件也做出了支持,非常方便使用。 我们在 MailService 中添加支持 HTML 发送的方法. Spring Boot整合邮件发送并保存历史发送邮箱 和...
使用Spring Boot 开发支持多附件邮件发送微服务平台代码
Spring邮件发送服务(java邮件发送),支持多附件、多人发送/抄送/暗送
因为上传大小的限制,分为两部分上传,这是第二部分,第一部分会在评论中给出链接 绝对是spring攻略中文版第二版 ... 第18章 Spring对JMX、电子邮件和调度的支持 第19章 Spring中的脚本编程
因为上传大小的限制,分为两部分上传,这是第一部分,第二部分会在评论中给出链接 绝对是spring攻略中文版第二版 ... 第18章 Spring对JMX、电子邮件和调度的支持 第19章 Spring中的脚本编程
本资源提供了一套基于Spring ...这些文件详细展示了如何使用Spring Boot构建一个邮件发送服务,该服务支持文本、附件、模板等多种实现,并具有队列和线程定时任务功能。这些文件非常适合用于学习和参考Java项目的开发。
Spring专家力作 理论与实践完美结合 问题描述→解决方案→实现方法 第一部分 核心概念 第1章 控制反转和容器 ... 第18章 Spring对JMX、电子邮件和调度的支持 第19章 Spring中的脚本编程
使用邮件请在gateway模块配置文件中配置自己的邮箱信息 跟着课件学习的,启动服务可能需要关联启动:nacos sentinel seata skywalking其中某些中间件 配合博客...
基于Spring Boot Security 2.5.8,扩展了核心功能,支持用户名,手机号,邮箱登录,以及记住密码,JWT等功能。有完整的数据库脚本及功能演示。
(10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar 这个jar文件包含Web应用...
在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。这个jar 文件为Spring 核心提供了大量扩展。可以找到使用...
Velocity模板邮件发送例子(Spring框架)(可通用,支持多扩展)Velocity模板邮件发送例子(Spring框架)(可通用,支持多扩展)
核心容器。...通过配置管理特性,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块直接将面向方面编程的功能集成到Spring框架中。它为基于Spring 应用程序的对象提供了事务管理服务。
spring-context.jar(必须):这个jar 文件在基础IOC功能上为Spring 核心提供了大量扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持...
java发送邮件示例源码,支持html格式邮件,接收人,邮件主题,邮件内容,附件路径 #邮件 spring.mail.default-encoding=utf-8 # 配置SMTP服务器地址 spring.mail.host=smtp.qq.com #发送方邮件名 spring.mail....
这个jar文件包含Spring对DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,因为Hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖 spring-dao.jar里的类,用这个包时你...
Mailspring,一个漂亮的电子邮件客户端。类似Nylas,但比Nylas速度更快。支持多种操作系统。
org.springframework.context-3.0.0.M4.jar: 提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等 org.spring...
13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form...
主要为大家详细介绍了Spring Boot实现邮件发送功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下