- 浏览: 759512 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
corelengine:
好文,支持一下!
在 Spring Web Flow 项目中应用 Hessian 服务 -
corelengine:
感谢分享,不过你的工程太简单了,怎么可以导入eclipse
Spring Web Flow 2.0 入门 例子源码 -
chenrongtao2132:
melody404 写道请教博主一个问题
登录成功以后为什么老 ...
CAS 单点登录安装笔记4 -- asp.net client端的设置 -
chxiaowu:
从头到尾没发现 那里有 cxf bean配置啊。。。。
WebService开发笔记 3 -- 增强访问 WebService 的安全性 -
chxiaowu:
严重: StandardWrapper.Throwable
o ...
WebService开发笔记 3 -- 增强访问 WebService 的安全性
http://www.blogjava.net/shmily432685/archive/2005/12/30/26041.html
http://www.blogjava.net/Crying/archive/2007/09/22/142701.html
re: 使用Spring邮件抽象层发送简单邮件
java 代码
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.activation.*;
- import java.io.*;
- public class SendMail
- {
- static final String MAIL_HOST = "61.177.95.155";
- static final boolean MAIL_NEEDAUTH = true;
- static final String DEFAULT_MAIL_USER = "lioulb@126.com";
- static final String DEFAULT_MAIL_PASSWORD = ".......";
- static final String DEFAULT_FORMAT = "plain"; //纯文本
- private MimeMessage mimeMsg; //MIME邮件对象
- private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
- private Session session; //邮件会话对象
- private Properties props; //系统属性
- private boolean needAuth; //smtp是否需要认证
- private String userName; //smtp认证用户名和密码
- private String password; //smtp认证密码
- private String mailFormat = DEFAULT_FORMAT; //邮件文本格式
- public SendMail(String host,boolean needAuth,String user,String password)
- { //构造方法
- if(host==null||host.trim().equals(""))
- {
- host = MAIL_HOST;
- }
- setHost(host);
- createMimeMessage();
- setAuth(needAuth);
- if(user==null)
- {
- user = "";
- }
- if(password==null)
- {
- password = "";
- }
- setUser(user,password);
- setFrom(user);
- }
- public SendMail()
- {
- setHost(MAIL_HOST);
- createMimeMessage();
- setAuth(MAIL_NEEDAUTH);
- setUser(DEFAULT_MAIL_USER,DEFAULT_MAIL_PASSWORD);
- setFrom(DEFAULT_MAIL_USER);
- }
- private void setHost(String hostName)
- { //设置smtp的主机地址
- if(props==null)
- {
- props = System.getProperties(); //获得系统属性对象
- }
- props.put("mail.smtp.host",hostName); //设置SMTP主机
- }
- private void setAuth(boolean need)
- { //smtp认证
- if(props==null)
- {
- props = System.getProperties();
- }
- if(need)
- {
- props.put("mail.smtp.auth","true");
- }
- else
- {
- props.put("mail.smtp.auth","false");
- }
- }
- private void setUser(String userName,String password)
- { //设置smtp用户名和密码
- this.userName = userName;
- this.password = password;
- }
- private boolean createMimeMessage()
- { //生成邮件对象
- try
- {
- session = Session.getDefaultInstance(props,null); //获得邮件会话对象
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- try
- {
- mimeMsg = new MimeMessage(session); //创建MIME邮件对象
- mp = new MimeMultipart();
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- private void setMailFormat(String format)
- { //设置邮件的正文格式 plain:纯文本格式 html:html格式
- if(format==null)
- {
- format = "plain";
- }
- format = format.trim();
- if(format.equals("plain")||format.equals("html"))
- {
- this.mailFormat = "text/"+format;
- }
- else
- {
- this.mailFormat = "text/plain";
- }
- }
- public boolean sendMail(String to,String subject,String body,String format)
- { //发送不带附件,不转发的邮件
- boolean theReturn = true;
- setMailFormat(format);
- // String aLine = Time.getdate()+" "+Time.gettime()+" send: "+this.userName
- //+" "+to+" "+Common.convertToGb(subject);
- String aLine = " send: "+this.userName
- +" "+to+" "+subject;
- if(setSubject(subject)&&setBody(body)&&setTo(to))
- {
- theReturn = sendOut();
- aLine = aLine+" [Success]";
- }
- else
- {
- theReturn = false;
- aLine = aLine+" [Failed]";
- }
- return theReturn;
- }
- public boolean sendMail(String to,String subject,String body)
- {
- return sendMail(to,subject,body,DEFAULT_FORMAT);
- }
- private boolean setSubject(String mailSubject)
- { //设置邮件主题
- try
- {
- //mailSubject = Common.convertToGb(mailSubject);
- mimeMsg.setSubject(mailSubject);
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- private boolean setBody(String mailBody)
- { //设置邮件正文
- try
- {
- //mailBody = Common.convertToGb(mailBody);
- BodyPart bp = new MimeBodyPart();
- bp.setContent(mailBody,this.mailFormat+";charset=GB2312"); //"<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody
- mp.addBodyPart(bp);
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- private boolean setFrom(String from)
- { //设置发信人地址
- try
- {
- mimeMsg.setFrom(new InternetAddress(from));
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- private boolean setTo(String to)
- { //设置收信人地址
- if(to==null)
- {
- return false;
- }
- try
- {
- mimeMsg.addRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- private boolean addFileAffix(String filename)
- { //添加附件
- try
- {
- BodyPart bp = new MimeBodyPart();
- FileDataSource fileds = new FileDataSource(filename);
- bp.setDataHandler(new DataHandler(fileds));
- bp.setFileName(fileds.getName());
- mp.addBodyPart(bp);
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- private boolean setCopyTo(String copyto)
- { //设置转发人地址
- if(copyto==null)
- {
- return false;
- }
- try
- {
- mimeMsg.addRecipients(Message.RecipientType.CC,
- (Address[])InternetAddress.parse(copyto));
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- public int tryToConnect()
- { //连接邮箱 1:连接成功 0:连接失败 -1:已经连接或系统忙
- int theReturn = 0;
- // String aLine = Time.getdate()+" "+Time.gettime()+" Connect: "+this.userName
- //+" "+this.userName+" "+this.password;
- String aLine = " Connect: "+this.userName
- +" "+this.userName+" "+this.password;
- try
- {
- Session mailSession = Session.getInstance(props,null);
- Transport transport = mailSession.getTransport("smtp");
- transport.connect((String)props.get("mail.smtp.host"),this.userName,
- this.password);
- transport.close();
- theReturn = 1;
- aLine = aLine+" [Success]";
- }
- catch(MessagingException e)
- {
- e.printStackTrace();
- theReturn = 0;
- }
- catch(IllegalStateException e)
- {
- e.printStackTrace();
- theReturn = -1;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- theReturn = 0;
- aLine = aLine+" [Failed]";
- }
- return theReturn;
- }
- private boolean sendOut()
- { //发送邮件
- try
- {
- mimeMsg.setContent(mp);
- mimeMsg.saveChanges();
- Session mailSession = Session.getInstance(props,null);
- Transport transport = mailSession.getTransport("smtp");
- transport.connect((String)props.get("mail.smtp.host"),this.userName,
- this.password);
- transport.sendMessage(mimeMsg,mimeMsg.getAllRecipients());
- transport.close();
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- public boolean changePwd(String userName,String newPwd)
- { //修改邮箱密码
- boolean theReturn = false;
- try
- {
- String commond = "passwd "+userName;
- Process process = Runtime.getRuntime().exec(commond);
- BufferedReader br = new BufferedReader(new InputStreamReader(process.
- getInputStream()));
- PrintStream ps = new PrintStream(process.getOutputStream());
- BufferedReader br1 = new BufferedReader(new InputStreamReader(process.
- getErrorStream()));
- char ac[] = new char[1024];
- br1.read(ac);
- ps.println(newPwd);
- ps.flush();
- br1.read(ac);
- ps.println(newPwd);
- ps.flush();
- br1.read(ac);
- if(process.waitFor()==0)
- {
- theReturn = true;
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- //e.printStackTrace(System.out);
- System.out.println(e.toString());
- theReturn = false;
- }
- return theReturn;
- }
- public boolean addUser(String userName)
- { //添加邮件用户 (密码默认为空)
- boolean theReturn = false;
- try
- {
- String commond = "/usr/sbin/useradd "+userName+
- " -g mail -d /dev/null -s /bin/false";
- Process process = Runtime.getRuntime().exec(commond);
- BufferedReader br = new BufferedReader(new InputStreamReader(process.
- getInputStream()));
- PrintStream ps = new PrintStream(process.getOutputStream());
- BufferedReader br1 = new BufferedReader(new InputStreamReader(process.
- getErrorStream()));
- char ac[] = new char[1024];
- br1.read(ac);
- if(process.waitFor()==0)
- {
- theReturn = true;
- }
- }
- catch(Exception e)
- {
- e.printStackTrace(System.out);
- theReturn = false;
- }
- return theReturn;
- }
- public boolean addUser(String userName,String pwd)
- { //添加邮件用户
- boolean theReturn = addUser(userName);
- if(theReturn)
- {
- theReturn = changePwd(userName,pwd);
- if(!theReturn)
- { //修改密码失败
- deleUser(userName);
- }
- }
- return theReturn;
- }
- public boolean deleUser(String userName)
- { //删除邮件用户
- boolean theReturn = false;
- try
- {
- String commond = "/usr/sbin/userdel "+userName;
- Process process = Runtime.getRuntime().exec(commond);
- BufferedReader br = new BufferedReader(new InputStreamReader(process.
- getInputStream()));
- PrintStream ps = new PrintStream(process.getOutputStream());
- BufferedReader br1 = new BufferedReader(new InputStreamReader(process.
- getErrorStream()));
- char ac[] = new char[1024];
- br1.read(ac);
- if(process.waitFor()==0)
- {
- theReturn = true;
- }
- }
- catch(Exception exception)
- {
- exception.printStackTrace(System.out);
- theReturn = false;
- }
- return theReturn;
- }
- public static void main(String args[]){
- SendMail myMail=new SendMail();
- System.out.println(myMail.sendMail("oxservice@126.com","this is test","my \n test"));
- }
- }
发表评论
-
Android开发笔记
2009-10-19 09:11 16251.复制数据库文件: D:\Program Files\and ... -
天气预报的 WebService 服务网站
2009-06-29 10:53 0http://www.webxml.com.cn/WebSer ... -
在 Spring Web Flow 项目中应用 Hessian 服务
2009-05-21 11:19 2544原来作的一个项目因为页面跳转比较多,应用了S ... -
Spring Web Flow 2.0 入门 例子源码
2008-12-22 11:34 11747developerWorks 中有一篇教材讲解了 Spr ... -
JAD反编译工具
2008-05-29 11:33 5044This is README file for Jad - t ... -
压力测试与系统调优
2008-05-04 16:19 2298最近用loadrunne ... -
通过压力测试排查Bug(二)--排查Bug
2008-05-04 11:44 1804最近的一个项目 ... -
通过压力测试排查Bug(一)--测试过程
2008-05-04 11:05 1603最近的一个项目应用了Acegi作为安全框架,项目试运 ... -
WebService开发笔记 3 -- 增强访问 WebService 的安全性
2008-03-19 09:50 21803在WebService开发笔记 1中我们创建了一个WebSer ... -
WebService开发笔记 2 -- VS 2005 访问WebServcie更简单
2008-03-12 19:32 11138WebService开发笔记 2 -- VS 2005 访问W ... -
WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单
2008-03-12 18:37 27498WebService开发笔记 1 -- 利用cxf开发WebS ... -
Tomcat 配置 -- 打开中文文件名的附件
2008-03-04 10:23 1896设计了文件上传的工具,但在Tomcat服务器上访问中文文件名的 ... -
常用的System.getProperty()
2008-03-02 11:53 29常用的System.getProperty()System.g ... -
CAS 单点登录安装笔记4 -- asp.net client端的设置
2008-03-02 11:51 16449CAS 单点登录安装笔记4 --- asp.net clien ... -
CAS 单点登录安装笔记3 -- 与acegi集成
2008-02-28 23:38 8248CAS 单点登录安装笔记3 -- 与acegi集成 在我的项 ... -
CAS 单点登录安装笔记2 -- 配置CAS,访问自己的用户表
2008-02-27 17:45 6992CAS 单点登录安装笔记2 1.修改cas/webapp/ ... -
CAS 单点登录安装笔记1 -- 基本设置与数字证书的安装
2008-02-26 16:35 5699安装JA-SIG SSO系统笔记1 (关于配置访问数据库的用 ... -
JAVA文档
2007-12-19 16:05 1164JAVA相关文档 http://www.lybbs.net/n ... -
Tomcat性能调优(2)
2007-12-19 15:37 2892原文出处:http://www.lybbs.n ... -
Spring 通过 Tomcat 6.0 下的数据源连接池 访问Oracle数据库
2007-12-17 18:26 5945头疼的老问题,折腾了一天,tomcat6.0数据源配置 to ...
相关推荐
spring邮件抽象层详解,简单的方式发送email
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以...
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender(实现类为org.springframework.mail.javamail.JavaMailSenderImpl,下面会用到改实现类)和封装了简单邮件属性的值...
6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack ...19.2. 使用Spring JMS ...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
简单邮件 HTML 邮件 内嵌图片或附档 排程 Spring则对 java.util.Timer提供了抽象封装,让您可以善用Spring的容器管理功能,而Spring对Quartz进行了封装,让它在使用上更加方便。 使用 ...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java...附录A JavaMail发送邮件 附录B 在Spring中开发Web Service
Spring Email抽象核心接口MailSender,其实现类JavaMailSenderImpl,在其中配置邮件 服务器host,pssword,协议等 。。。。。 1.发送简单的消息 SimpleMailMessage:发送简单的消息 2.发送丰富的消息(比如带有附件,内...
5.3.3使用spring对jdbc的dao支持类 5.4在spring里集成hibernate 5.4.1选择hibernate的版本 5.4.2使用hibernate模板 5.4.3建立基于hibernate的dao 5.4.4使用hibernate3上下文会话 5.5spring和java持久api ...
你可以使用 IoC容器,在其上使用Struts,但是你也可以选择使用 Hibernate 整合代码或者 JDBC 抽象层。 我们将Spring设计为非侵入式的(并且以后也是如此),这意味着应用基本上不需要依赖框架本身 (或者肯定是最小...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java...附录A JavaMail发送邮件 附录B 在Spring中开发Web Service
5.3.3 使用Spring对JDBC的DAO支持类 5.4 在Spring里集成Hibernate 5.4.1 选择Hibernate的版本 5.4.2 使用Hibernate模板 5.4.3 建立基于Hibernate的DAO 5.4.4 使用Hibernate 3上下文会话 5.5 Spring和Java...
5.3.3 使用Spring对JDBC的DAO支持类 5.4 在Spring里集成Hibernate 5.4.1 选择Hibernate的版本 5.4.2 使用Hibernate模板 5.4.3 建立基于Hibernate的DAO 5.4.4 使用Hibernate 3上下文会话 5.5 Spring和Java...
你可以使用 IoC容器,在其上使用Struts,但是你也可以选择使用 Hibernate 整合代码或者 JDBC 抽象层。 我们将Spring设计为非侵入式的(并且以后也是如此),这意味着应用基本上不需要依赖框架本身 (或者肯定是最小...
☆ Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭...
着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...
00、尚硅谷_SpringBoot_源码、课件 1、尚硅谷-SpringBoot高级-缓存-JSR107简介 2、尚硅谷-SpringBoot高级-缓存-Spring缓存抽象简介 3、尚硅谷-SpringBoot高级-缓存-基本环境搭建 4、尚硅谷-SpringBoot高级-缓存-@...