自己写的一个mail发送组件,可以直接在程序中调用。
2007-08-22 13:51
567 查看
以下是自己用java mail包写的一个发送邮件的组件,写的比较乱,也没仔细加注释,有任何问题可以联系shanchao@neusoft.com。
mail.properties:配置文件,配置邮件服务器,发件人等信息
mail.smtp.host=smtp.neusoft.com
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.from=shanchao@neusoft.com
mail.from.name=/u4e4c/u9c81/u6728/u9f50/u52b3/u52a8/u548c/u793e/u4f1a/u4fdd/u969c/u5c40/u7f51/u7ad9/u7ba1/u7406/u5458
mail.username=shanchao
mail.password=**** //密码需要自己写
mail.copyto=guozl@neusoft.com
mail.subject=/u4e4c/u9c81/u6728/u9f50/u52b3/u52a8/u548c/u793e/u4f1a/u4fdd/u969c/u5c40/u7f51/u7ad9/u901a/u77e5
SendMail类:mail发送组件,只需要在程序中调用就可以发邮件了。
package com.neusoft.apps;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* 服务器端Mail发送组件,需要跟mail.properties配置文件配合使用
* @author 单超
* time:2007-8-14
*
*/
public class SendMail {
private Properties props = null;//配置文件props
private Session session = null; //邮件会话session
private MimeMessage mimeMsg = null;//Message对象 mimeMsg
private Multipart mp = null; //邮件内容容器mp
/**
* 重要方法:该构造方法在创建SendMail对象时调用。用于加载必要的参数
* @param toAddress
* @param toAddressName
* @param ccAddress
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,String toAddressName,Map ccAddress,String bodyContent) throws Exception{
//初始化成员变量
this.init();
//设置msg属性
//设置收发件人,抄送人
mimeMsg.setFrom(new InternetAddress(props.getProperty("mail.from"),props.getProperty("mail.from.name")));//设置发件人
mimeMsg.setRecipient(Message.RecipientType.TO, new InternetAddress (toAddress,toAddressName));//设置收件人
//设置抄送人
if(ccAddress != null && ccAddress.size() >0){
mimeMsg.setRecipients(Message.RecipientType.CC, createCcAddress(ccAddress));
}
//设置邮件标题和日期
//需要中文转码
mimeMsg.setSubject(encode(props.getProperty("mail.subject")));//设置邮件标题
mimeMsg.setSentDate(new java.util.Date());//设置发件日期
//设置正文
BodyPart bp = new MimeBodyPart();
bp.setContent("" + bodyContent, "text/html;charset=GBK");
mp.addBodyPart(bp);
}
/**
* 重要方法:重载的构造器
* @param toAddress
* @param ccAddress
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,Map ccAddress,String bodyContent) throws Exception{
this(toAddress,null,ccAddress,bodyContent);
}
/**
* 重要方法:重载的构造器
* @param toAddress
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,String bodyContent)throws Exception{
this(toAddress,null,null,bodyContent);
}
/**
* 重要方法:重载的构造器
* @param toAddress
* @param toAddressName
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,String toAddressName,String bodyContent)throws Exception{
this(toAddress,toAddressName,null,bodyContent);
}
/**
* 重要方法:该方法用于添加附件,fileRealPath为附件的本地绝对地址,newFileName为附件发送后的文件名
* @param fileRealPath
* @param newFileName
* @throws Exception
*/
public void addFile(String fileRealPath,String newFileName) throws Exception{
//设置邮件附件
BodyPart body = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(fileRealPath);
body.setDataHandler(new DataHandler(fileDataSource));
if(newFileName == null || "".equals(newFileName)){
newFileName = fileDataSource.getName();
}
//需要对附件文件名称进行转码,不然会出现乱码
body.setFileName(encode(newFileName));
mp.addBodyPart(body);
}
/**
* 重要方法:该方法用于添加附件,fileRealPath为附件的本地绝对地址,附件发送后的文件名默认于原文件名字相同
* @param fileRealPath
* @param newFileName
* @throws Exception
*/
public void addFile(String fileRealPath) throws Exception{
this.addFile(fileRealPath,null);
}
/**
* 重要方法:发送mail
* @throws Exception
*/
public void send() throws Exception{
//融合邮件内容
mimeMsg.setContent(mp);
//发送邮件
Transport transport = session.getTransport("smtp");//????????
transport.connect(props.getProperty("mail.smtp.host"),
props.getProperty("mail.username"),
props.getProperty("mail.password"));
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
transport.close();
}
/*
* 私有方法:用于对中文进行转码
* @param value
* @return
* @throws Exception
*/
private String encode(String value) throws Exception{
return MimeUtility.encodeText(
new String(value.getBytes(),"GB2312"),
"GB2312",
"B");
}
/*
* 私有方法:用于初始化props,session,mimeMsg,mp等成员变量
* @throws Exception
*/
private void init() throws Exception{
//初始化props,读取config/mail.properties文件 //需要完善,不能每次都读取配置文件
this.props = getProperties("config/mail.properties");//????????????
//初始化session,msg,mp
this.session = Session.getInstance(props, null);//获取共享session对象
this.mimeMsg = new MimeMessage(session);//创建Message
this.mp = new MimeMultipart();//创建Multipart
}
/*
* 私有方法:用于读取mail.properties配置文件
* @param fileName
* @return
* @throws Exception
*/
private Properties getProperties(String fileName) throws Exception{
URL url = ConfigUtil.class.getClassLoader().getResource(fileName);
File configFile = new File(url.getFile());
Properties props = new Properties();
props.load(new FileInputStream(configFile));
return props;
}
/*
* 私有方法:用于将以Map形式传入的抄送人地址,组织成Address[]
* @param ccAddress
* @return
* @throws Exception
*/
private static Address[] createCcAddress(Map ccAddress) throws Exception{
Object[] keys = ccAddress.keySet().toArray();
Address[] result = new InternetAddress[keys.length];
for(int i = 0;i<keys.length;i++){
String mailAddress = (String)keys[i];
String mailName = (String)ccAddress.get(keys[i]);
result[i] = new InternetAddress(mailAddress,mailName);
}
return result;
}
}
MailTest:测试程序,也同时说明了SendMail的调用方式。
package com.neusoft.apps;
import java.util.HashMap;
import java.util.Map;
public class MailTest {
public static void main(String[] args) throws Exception{
Map map = new HashMap();
SendMail mail = new SendMail("shanchao@neusoft.com","***",map,"三版测试");
mail.addFile("F://设计模式JAVA.pdf");
mail.send();
}
}
mail.properties:配置文件,配置邮件服务器,发件人等信息
mail.smtp.host=smtp.neusoft.com
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.from=shanchao@neusoft.com
mail.from.name=/u4e4c/u9c81/u6728/u9f50/u52b3/u52a8/u548c/u793e/u4f1a/u4fdd/u969c/u5c40/u7f51/u7ad9/u7ba1/u7406/u5458
mail.username=shanchao
mail.password=**** //密码需要自己写
mail.copyto=guozl@neusoft.com
mail.subject=/u4e4c/u9c81/u6728/u9f50/u52b3/u52a8/u548c/u793e/u4f1a/u4fdd/u969c/u5c40/u7f51/u7ad9/u901a/u77e5
SendMail类:mail发送组件,只需要在程序中调用就可以发邮件了。
package com.neusoft.apps;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* 服务器端Mail发送组件,需要跟mail.properties配置文件配合使用
* @author 单超
* time:2007-8-14
*
*/
public class SendMail {
private Properties props = null;//配置文件props
private Session session = null; //邮件会话session
private MimeMessage mimeMsg = null;//Message对象 mimeMsg
private Multipart mp = null; //邮件内容容器mp
/**
* 重要方法:该构造方法在创建SendMail对象时调用。用于加载必要的参数
* @param toAddress
* @param toAddressName
* @param ccAddress
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,String toAddressName,Map ccAddress,String bodyContent) throws Exception{
//初始化成员变量
this.init();
//设置msg属性
//设置收发件人,抄送人
mimeMsg.setFrom(new InternetAddress(props.getProperty("mail.from"),props.getProperty("mail.from.name")));//设置发件人
mimeMsg.setRecipient(Message.RecipientType.TO, new InternetAddress (toAddress,toAddressName));//设置收件人
//设置抄送人
if(ccAddress != null && ccAddress.size() >0){
mimeMsg.setRecipients(Message.RecipientType.CC, createCcAddress(ccAddress));
}
//设置邮件标题和日期
//需要中文转码
mimeMsg.setSubject(encode(props.getProperty("mail.subject")));//设置邮件标题
mimeMsg.setSentDate(new java.util.Date());//设置发件日期
//设置正文
BodyPart bp = new MimeBodyPart();
bp.setContent("" + bodyContent, "text/html;charset=GBK");
mp.addBodyPart(bp);
}
/**
* 重要方法:重载的构造器
* @param toAddress
* @param ccAddress
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,Map ccAddress,String bodyContent) throws Exception{
this(toAddress,null,ccAddress,bodyContent);
}
/**
* 重要方法:重载的构造器
* @param toAddress
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,String bodyContent)throws Exception{
this(toAddress,null,null,bodyContent);
}
/**
* 重要方法:重载的构造器
* @param toAddress
* @param toAddressName
* @param bodyContent
* @throws Exception
*/
public SendMail(String toAddress,String toAddressName,String bodyContent)throws Exception{
this(toAddress,toAddressName,null,bodyContent);
}
/**
* 重要方法:该方法用于添加附件,fileRealPath为附件的本地绝对地址,newFileName为附件发送后的文件名
* @param fileRealPath
* @param newFileName
* @throws Exception
*/
public void addFile(String fileRealPath,String newFileName) throws Exception{
//设置邮件附件
BodyPart body = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(fileRealPath);
body.setDataHandler(new DataHandler(fileDataSource));
if(newFileName == null || "".equals(newFileName)){
newFileName = fileDataSource.getName();
}
//需要对附件文件名称进行转码,不然会出现乱码
body.setFileName(encode(newFileName));
mp.addBodyPart(body);
}
/**
* 重要方法:该方法用于添加附件,fileRealPath为附件的本地绝对地址,附件发送后的文件名默认于原文件名字相同
* @param fileRealPath
* @param newFileName
* @throws Exception
*/
public void addFile(String fileRealPath) throws Exception{
this.addFile(fileRealPath,null);
}
/**
* 重要方法:发送mail
* @throws Exception
*/
public void send() throws Exception{
//融合邮件内容
mimeMsg.setContent(mp);
//发送邮件
Transport transport = session.getTransport("smtp");//????????
transport.connect(props.getProperty("mail.smtp.host"),
props.getProperty("mail.username"),
props.getProperty("mail.password"));
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
transport.close();
}
/*
* 私有方法:用于对中文进行转码
* @param value
* @return
* @throws Exception
*/
private String encode(String value) throws Exception{
return MimeUtility.encodeText(
new String(value.getBytes(),"GB2312"),
"GB2312",
"B");
}
/*
* 私有方法:用于初始化props,session,mimeMsg,mp等成员变量
* @throws Exception
*/
private void init() throws Exception{
//初始化props,读取config/mail.properties文件 //需要完善,不能每次都读取配置文件
this.props = getProperties("config/mail.properties");//????????????
//初始化session,msg,mp
this.session = Session.getInstance(props, null);//获取共享session对象
this.mimeMsg = new MimeMessage(session);//创建Message
this.mp = new MimeMultipart();//创建Multipart
}
/*
* 私有方法:用于读取mail.properties配置文件
* @param fileName
* @return
* @throws Exception
*/
private Properties getProperties(String fileName) throws Exception{
URL url = ConfigUtil.class.getClassLoader().getResource(fileName);
File configFile = new File(url.getFile());
Properties props = new Properties();
props.load(new FileInputStream(configFile));
return props;
}
/*
* 私有方法:用于将以Map形式传入的抄送人地址,组织成Address[]
* @param ccAddress
* @return
* @throws Exception
*/
private static Address[] createCcAddress(Map ccAddress) throws Exception{
Object[] keys = ccAddress.keySet().toArray();
Address[] result = new InternetAddress[keys.length];
for(int i = 0;i<keys.length;i++){
String mailAddress = (String)keys[i];
String mailName = (String)ccAddress.get(keys[i]);
result[i] = new InternetAddress(mailAddress,mailName);
}
return result;
}
}
MailTest:测试程序,也同时说明了SendMail的调用方式。
package com.neusoft.apps;
import java.util.HashMap;
import java.util.Map;
public class MailTest {
public static void main(String[] args) throws Exception{
Map map = new HashMap();
SendMail mail = new SendMail("shanchao@neusoft.com","***",map,"三版测试");
mail.addFile("F://设计模式JAVA.pdf");
mail.send();
}
}
相关文章推荐
- Android中Activity启动模式详解,可以控制程序按home键后进来还会调用一个自己不喜欢的界面
- .Net 邮件发送的类,在程序中直接调用就可以了
- 如题,既然直接对一个实例发送方法名就可以调用这个方法,那为什么还要使用performSelector:去调呢?两者u有啥区别
- Android中Activity启动模式详解,可以控制程序按home键后进来还会调用一个自己不喜欢的界面
- c# 控制台程序调用直接运行一个新程序和隐藏自己的窗体(转)
- JAVA程序调用javax.mail发送邮件
- [程序]自己做了一个测试数据生成器 可以下载
- 编写一个程序,可以通过次程序完成一个表的创建操作,输入表的名称,各个列的名称及类型,输入完成后直接通过JDBC创建指定的表。
- Kindeditor -单独调用组件,可以直接调用上传功能。
- NET简单的一个画图程序 使用简单 自己可以相关自己的内容进行配置就可以使用了
- 编写一个程序,可以通过次程序完成一个表的创建操作,输入表的名称,各个列的名称及类型,输入完成后直接通过JDBC创建指定的表。
- c语言:编写一个程序,可以直接接收键盘字符
- 简单完整的代码,通过这个代码你将对RSA加密算法在Java中的实现方法有一个初步的了解,这个类,你可以直接使用,水平高的,就自己修改完善下代码。
- 【郝斌数据结构自学笔记】53-56_一个函数为什么可以自己调用自己_递归必须满足三个条件_循环和递归的比较
- 一个 可以直接调用windows phone DatePicker choose date页面的 自定义类
- 一个有趣的程序——可以打印自己第N行代码的程序
- NET简单的一个画图程序 使用简单 自己可以相关自己的内容进行配置就可以使用了
- SAP ABAP/4学习---如何给写好的一个程序分配事务代码,用户可以直接通过事务码来访问程序.或者加入收藏夹(9)
- PHP的mail()函数可以实现直接用脚本发送邮件
- 程序有一个调用打印机的功能,在调试的时候可以用,用tomcat部署上去了之后就不能打印了····