- 浏览: 935529 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (423)
- mysql (37)
- hibernate (3)
- struts (9)
- spring (33)
- dom4j (2)
- junit (0)
- exception (1)
- 随笔杂谈 (12)
- google app engine (1)
- XMPP (1)
- OAuth安全授权 (1)
- 版本控制 (8)
- 心情感悟 (0)
- core java (19)
- log4j (7)
- jquery (12)
- javascript (10)
- 网站性能优化及工具 (11)
- 服务器架设配置等 (38)
- EXT (4)
- 正则表达式 (5)
- 日志统计分析 (2)
- htmlparse (4)
- httpclient (5)
- java随笔 (5)
- dhtmlxtree (1)
- freemarke (5)
- memcached (6)
- javamail (5)
- Linux命令 (10)
- 应用监控cpu web jdbc等 (4)
- jmagick (9)
- 第三方缓存策略 (9)
- ORM (2)
- hadoop (2)
- 大数据量处理 (8)
- 经典 (1)
- 权限设计 (1)
- andriod (1)
- mybatis (12)
- redis (24)
- 数据结构_算法 (5)
- 分布式系统 (1)
- php (1)
- 网络编程 (3)
- 服务器部署 (3)
- ios (2)
- IM (23)
- mina (1)
- 视讯和语音 (1)
- 代码生成 (1)
- 架构 (4)
- 建模工具 (1)
- oracle (4)
- solr (10)
- 构建工具 (7)
- html5 (1)
- nginx (5)
- css (1)
- 大数据-分布式 (2)
- 设计模式 (2)
- mq (2)
- jvm调优 (8)
- 并发编程 (2)
- 搜索引擎 (1)
- UML (2)
最新评论
-
天使建站:
jquery获取网页里多选框checkbox选中项的值的方法及 ...
JS jQuery分别获取选中的复选框值 -
abao1:
发现一个小问题 sortAndSave方法中的for循环 第二 ...
完整java实现外部排序 -
西巴拉古呀那:
Kafka分布式消息系统实战(与JavaScalaHadoop ...
消息系统kafka介绍 -
kafodaote:
Kafka分布式消息系统实战(与JavaScalaHadoop ...
消息系统kafka介绍 -
成大大的:
Kafka分布式消息系统实 ...
消息系统kafka介绍
在学习spring之前先学习以下不用spring来实现javamail的邮件发送:
用到得jar包: package com.css.service.email; import java.security.Security; import javax.mail.Authenticator; public static void main(String[] args) throws AddressException, //邮箱的用户名和密码 验证用户名和密码 发送邮件地址 msg.setFrom(new InternetAddress(username + "@gmail.com")); System.out.println("Message sent."); spring实现javamail:(用红色表注的请注意:需要改动的) 这里的发送邮件的服务器地址:masterspring2@gmail.com是可以接受服务的smtp协议。你也可以自己申请了一个邮箱地址。但是好像126、163邮箱的2007年11份之后申请的邮箱都不能用smtp服务。这里用的邮箱地址masterspring2@gmail.com是可以发送的。 用到jar包: applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="propertyConfigurer" <property name="port"> <property name="javaMailProperties"> </props> </bean> host=smtp.gmail.com SpringMailImpl.java: package com.css.springmail.test; public class SpringMailImpl{ MimeBodyPart content = new MimeBodyPart(); //发送带附件的邮件 } test.java: package com.css.springmail.test; import org.springframework.context.ApplicationContext; public class test { //测试发送带附件的邮件
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaMailSslSender {
MessagingException {
// 加密邮件有提供的类Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");服务主机地址
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");端口
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");是否要验证身份
final String username = "masterspring2";
final String password = "spring";
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message msg = new MimeMessage(session);
接受邮件地址
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
"dengjianli@126.com", false));
msg.setSubject("Hello");
msg.setText("How are you");
msg.setSentDate(new Date());
URLName urln = new URLName("smtps", "smtp.gmail.com", 465, null,"masterspring2", "spring");
Transport t = session.getTransport(urln);
t.send(msg);
}
}
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:messages.properties</value>
</list>
</property>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>${host}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<value>${port}</value>
</property>
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
<prop key="mail.debug">${mail.debug}</prop>
<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
<prop key="mail.smtp.socketFactory.class">${mail.smtp.socketFactory.class}</prop>
<prop key="mail.smtp.socketFactory.fallback">${mail.smtp.socketFactory.fallback}</prop>
</property>
</beans>
messages.properties:
username=masterspring2
password=spring
port=465
mail.smtp.socketFactory.port=456
mail.debug=true
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=true
#from=masterspring2@gmail.com
#subject=\u6807\u9898
#text=\u5185\u5BB9\u554A
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
/** *//**
* 测试发送只有文本信息的简单测试
* @param sender 邮件发送器
* @throws Exception
*/
public void sendTextMail(JavaMailSender sender) throws Exception {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo("dengjianli@126.com");
mailMessage.setFrom("masterspring2@gmail.com");
mailMessage.setSubject("test by amigo");
mailMessage.setText("spring Mail的简单测试123");
mailMessage.setSentDate(new Date());
sender.send(mailMessage);
System.out.println("成功发送文本文件!");
}
/** *//**
* 发送带附件的邮件
* @param sender 邮件发送器
* @throws Exception
*/
public void sendMimeMessage(final JavaMailSender sender) throws Exception {
//附件文件集合
final List files = new ArrayList();
MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress("dengjianli@126.com"));
mimeMessage.setFrom(new InternetAddress("masterspring2@gmail.com"));
mimeMessage.setSubject("Spring发送带附件的邮件", "gb2312");
Multipart mp = new MimeMultipart();
//向Multipart添加正文
content.setText("内含spring邮件发送的例子,请查收!");
//向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(content);
// files.add("com/css/springmail/test/SpringMail.java");
files.add("e:/s.xml");
//向Multipart添加附件
Iterator it = files.iterator();
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
while(it.hasNext()) {
MimeBodyPart attachFile = new MimeBodyPart();
String filename = it.next().toString();
FileDataSource fds = new FileDataSource(filename);
attachFile.setDataHandler(new DataHandler(fds));
attachFile.setFileName("=?GBK?B?"+enc.encode(fds.getName().getBytes())+"?=");
mp.addBodyPart(attachFile);
}
files.clear();
//向Multipart添加MimeMessage
mimeMessage.setContent(mp);
mimeMessage.setSentDate(new Date());
}
};
sender.send(mimeMail);
System.out.println("成功发送带附件邮件!");
}
/** *//**
* 发送带附件的html邮件
* @param 邮件发送器
* @throws Exception
*/
public void sendMimehtmlandmultiple() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime,true,"utf-8");
helper.setFrom("masterspring2@gmail.com");
helper.setTo("dengjianli@126.com");
helper.setSubject(" 测试spring Mail的附件功能");
//需要将附件显示在html中
//在标签中用cid:xx 标记,使用helper.addInline()方法添加
helper.setText("<html><body>javaeye是个好网站:<br>"+
"<a href='http://www.iteye.com'>" +
"<img src='cid:logo'></a></body></html>",true);
helper.addInline("logo",new FileSystemResource("e:/s.xml"));
//helper.addAttachment("javaeye.gif", new ClassPathResource("javaeye.gif"));
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mailSender.send(mime);
System.out.println("成功发送带附件邮件!");
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
SpringMailImpl springMail = new SpringMailImpl();
//测试发送只有文本信息的简单测试
springMail.sendTextMail(sender);
//springMail.sendMimeMessage(sender);
//发送html和附件
// springMail.sendMimehtmlandmultiple();
}
}
发表评论
-
spring3.0发送电子邮件(velocity模板,带附件,群发,解决乱码
2010-12-09 11:07 3190在这个例子中,将与发送方相关的配置信息放在了一个email ... -
spring发送javamail配置
2010-10-30 17:13 1567<!-- our Authenticator impl ... -
JavaMail读发邮件
2010-10-30 17:08 1928SMTP(Simple Mail Transfer Proto ... -
JavaMail 深入浅出
2010-10-30 16:54 1099SMTP:简单邮件传输协议(Simple Mail Trans ...
相关推荐
它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层...
Spring提供了非常好用的JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件。
包含了spring发送邮件的说明文档和代码案例以及图解。
用spring发送电子邮件 在此示例中,我们使用Gmail服务发送电子邮件 应用界面: 如果电子邮件发送成功: 主要依赖 <groupId>org.springframework <artifactId>spring-context-support <version>5.2.5.RELEASE ...
#电子邮件地址 spring.mail.host=smtp.126.com #Application spring.application.name=SEND-MAIL #授权密码 spring.mail.password=password #邮箱服务器默认端口 spring.mail.port=25 #协议 spring.mail.protocol=...
Spring邮件发送系统:基于Java构建,包含67个文件,包括60个XML配置文件、3个Java类文件、2个.gitignore文件、1个Idea项目文件...该项目是一个Spring email的最简单入门案例,用于演示如何使用Spring框架发送电子邮件。
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Spring的任务执行与调度、Spring Web应用的国际化支持、AJAX和Spring结合的访问模式、利用Spring发 送电子邮件、Spring JMS消息应用编程、教学资源全文检索设计、Java应用的报表打印、网络考试系统设 计、Spring应用...
JEmail发送电子邮件的例子,本Demo对jEmail的API做了包装,开发人员开发应用和学习。支持发送多个附件。另外还采用Struts2+Spring等技术。是一个不错的学习例子
Spring启动与AWS-SES发送电子邮件
第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...
使用Thymeleaf模板发送电子邮件并创建它们 使用MockMvcTest,RepositoryTest,EmailTest和用于服务层的常规JUnit4测试进行单元测试的设置 通过Fluentlenium和HtmlUnit与FeatureTest进行快速UI测试 包含SCSS + ...
主要内容包括Spring框架开发环境的搭建及使用Spring时必须掌握的基础知识,Spring的数据持久化、事务管理以及企业应用中的远程调用,JNDI命名服务,JMail发送电子邮件等企业级服务,小型网站或应用程序的开发思路、...
Spring Boot Quartz Scheduler示例:... 该项目正在使用Gmail的SMTP服务器发送电子邮件。 无论您使用Gmail还是其他任何SMTP服务器,都需要相应地配置以下邮件属性- spring.mail.host =smtp.gmail.com spring.mail.p
第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...
基于spring的项目管理应用程序。 我的主要目标是为每个人创建一个免费的开源隐私项目管理应用程序。...可选择发送电子邮件通知 仪表板 获取为当前用户分配的任务 用户: 用户名 完成 个性化通知 阻止添加到新项目/团队
该项目依赖于无模板模块it.ozimov:spring-boot-email-core ,该模块提供了核心功能(例如,发送电子邮件,调度和确定优先级,持久性)。 由于它是无模板的,因此它不提供用于通过模板引擎生成电子邮件正文的服务的...
7.2 发送电子邮件 7.3 调度任务 7.3.1 使用Java Timer调度任务 7.3.2 使用Quartz调度器 7.3.3 按调度计划调用方法 7.4 使用JMS发送消息 7.4.1 使用JMS模板发送消息 7.4.2 消费消息 ...
常用的电子邮件协议有SMTP、POP3、IMAP4,它们都隶属于TCP/IP协议簇,默认状态下,分别通过TCP端口25、110和143建立连接。 SMTP协议 SMTP的全称是 “Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组...
12.2发送电子邮件 12.2.1配置邮件发送器 12.2.2构建电子邮件 12.3调度任务 12.3.1使用javatimer调度任务 12.3.2使用quartz调度器 12.3.3按调度计划调用方法 12.4使用jmx管理springbean 12.4.1将springbean...