给你一个实例: ExtendString是中文编码转换import java.io.*; import java.util.*; import javax.activation.*; import javax.mail.*; import javax.mail.internet.*; import com.xindeco.common.inc.ExtendString;/** * <p>Title: 简易MailBean</p> * <p>Description: 提供简便的Mail发送功能(/p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: 厦门信达网络科技有限公司</p> * @author 陈庆传 OICQ:507240 * @version 1.0 */public class SendMail { private String errMsg = ""; private ExtendString ExStr = new ExtendString(); private String sender = "";//发件人地址 private String smtpHost = "";//邮件发送服务器(smtp) private String user = ""; //登录用户名 private String password = "";//登录密码 private String subject = "";//mail主题 public SendMail() { this.setPropertiesAttri(); } private void setPropertiesAttri() { try { InputStream is = getClass().getResourceAsStream("MailServer.properties"); Properties prop = new Properties(); prop.load(is); this.setSmtpHost(prop.get("SmtpHost").toString()); //System.err.println("getSmtpHost:"+this.getSmtpHost()); this.setUser(prop.get("User").toString()); //System.err.println("getUser:"+this.getUser()); this.setPassword(prop.get("Password").toString()); //System.err.println("getPassword:"+this.getPassword()); this.setSender(prop.get("Sender").toString()); //System.err.println("getSender:"+this.getSender()); this.setSubject(ExStr.CS(prop.get("Subject").toString())); //System.err.println("getSubject:"+this.getSubject()); } catch(Exception ex) { System.err.println("ex1 in sendmail.java:"+ex.toString()); } } /** 设置发件人地址 */ public void setSender(String sender) { this.sender = sender; } public String getSender() { return sender; } /** 设置邮件发送服务器(smtp) */ public void setSmtpHost(String smtpHost) {this.smtpHost = smtpHost;} public String getSmtpHost() {return smtpHost;} /** 设置登录用户名 */ public void setUser(String user) { this.user = user; } public String getUser() { return user; } /** 设置登录密码 */ public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } /** 设置mail主题 */ public void setSubject(String subject) { this.subject = subject; } public String getSubject() { return subject; } /** * 使用smtp发送邮件 主程序 * @throws MessagingException mail发送失败 */ public void smtp(String receiver,String content) throws MessagingException { if (smtpHost == null) throw new MessagingException("smtpHost not found"); if (user == null) throw new MessagingException("user not found"); if (password == null) throw new MessagingException("password not found"); Properties properties = new Properties(); properties.put("mail.smtp.host", smtpHost);//设置smtp主机 properties.put("mail.smtp.auth", "true");//使用smtp身份验证 Session session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } } ); //获得邮件会话对象 MimeMessage mimeMsg = new MimeMessage(session);//创建MIME邮件对象 if (sender != null)//设置发件人地址 { mimeMsg.setFrom(new InternetAddress(sender)); } if (receiver != null)//设置收件人地址 { mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver)); } if (subject != null)//设置邮件主题 { mimeMsg.setSubject(subject, "GBK"); } MimeBodyPart part = new MimeBodyPart();//mail内容部分 part.setText(content == null ? "" : content, "GBK"); //设置邮件格式为html cqc part.setContent(content.toString(),"text/html;charset=GBK"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(part);//在 Multipart 中增加mail内容部分 mimeMsg.setContent(multipart);//增加 Multipart 到信息体 mimeMsg.setSentDate(new Date());//设置发送日期 Transport.send(mimeMsg);//发送邮件 //System.err.println("邮件发送成功!"); } /** 解析地址集合字符串 */ private InternetAddress[] parse(String addressSet) throws AddressException { ArrayList list = new ArrayList(); StringTokenizer tokens = new StringTokenizer(addressSet, ";"); while (tokens.hasMoreTokens()) { list.add(new InternetAddress(tokens.nextToken().trim())); } InternetAddress[] addressArray = new InternetAddress[list.size()]; list.toArray(addressArray); return addressArray; } //供外部调用的接口 public boolean sendMails(String mail,String content) { int mailLen = 0 ; int contentLen= 0; if (mail == null||content==null) { return false; } try { this.smtp(mail,content); } catch(Exception ex) { System.err.println("ex2 in sendmail.java:"+ex.toString()); } return true; } // for test /* public static void main (String[] args) { SendMail mail = new SendMail(); String email = "[email protected]"; String content = "账号:123 密码:123 <br>感谢您注册使用福建省大中专毕业生就业创业公共服务网!<br><a href='http://www.xindeco.com.cn' target='_blank'>www.xindeco.com.cn</a><br>此致<br>陈庆传 <br>即日"; try { mail.sendMails(email,content); } catch (Exception ex) { System.err.println("ex33:"+ex.toString()); } } */ } ExtendString.java /** 去掉字符串两端的空白字符,并将字符串转化为中国的标准字符gb2312的字符串. */ public String CS(String str) { //去掉字符串2端的空白字符 try { if (str == null) return ""; str = str.trim(); if (str == null) return ""; str = new String(str.getBytes("8859_1"), "GBK"); } catch (Exception e) { System.out.println(e); } return str; }
to crow_crown(黑色死神) servlet里面你可以直接调用它 jsp里面啊,和一般的javabean声明,和servlet一样使用。
楼上"bibiye(布什的老哥)"的还可以,可以用来学习啦
给你一份代码:/* * Created on 2004/07/23 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */
package com.mail;/** * @author geliang * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */import java.util.* ; import java.io.* ; import javax.mail.* ; import javax.mail.internet.* ; import javax.activation.* ;public class SendEMailBean { private String to=""; private String bcc = ""; private String cc = ""; private String from=""; //The Sender private String serverHost=""; private Vector file = null; private String subject=""; private String body = "";
public void setTo(String to){ this.to = to; } public String getTo(){ return to; }
public void setBcc(String bcc){ this.bcc = bcc; } public String getBcc(){ return bcc; }
public void setCc(String cc){ this.cc = cc; } public String getCc(){ return cc; }
public void setFrom(String from){ this.from = from; } public String getFrom(){ return from; }
public void setServerHost(String serverHost){ this.serverHost = serverHost; } public String getServerHost(){ return serverHost; }
public void setFile(Vector file){ this.file = file; } public Vector getFile(){ return file; }
public void setSubject(String subject){ this.subject = subject; } public String getSubject(){ return subject; }
public void setBody(String body){ this.body = body; } public String getBody(){ return body; }
public boolean sendMail(){ Properties props = System.getProperties(); props.put("mail.smtp.host", getServerHost()); //props.put("mail.smtp.auth","true"); Session session=Session.getDefaultInstance(props, null); //session.setDebug(true); try{ MimeMessage msg = new MimeMessage(session); if(from != null && !from.equals("")) msg.setFrom(new InternetAddress(from)); StringTokenizer str = new StringTokenizer(to,","); InternetAddress[] address=new InternetAddress[str.countTokens()]; for(int i=0;i<address.length;i++){ address[i] = new InternetAddress(str.nextToken()); } msg.setRecipients(Message.RecipientType.TO,address); if (getBcc() != null && !getBcc().equals("")){ str = new StringTokenizer(bcc,","); address=new InternetAddress[str.countTokens()]; for(int i=0;i<address.length;i++){ address[i] = new InternetAddress(str.nextToken()); } msg.setRecipients(Message.RecipientType.BCC,address); } if (getCc() != null && !getCc().equals("")){ str = new StringTokenizer(cc,","); address=new InternetAddress[str.countTokens()]; for(int i=0;i<address.length;i++){ address[i] = new InternetAddress(str.nextToken()); } msg.setRecipients(Message.RecipientType.CC,address); }
ExtendString是中文编码转换import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.xindeco.common.inc.ExtendString;/**
* <p>Title: 简易MailBean</p>
* <p>Description: 提供简便的Mail发送功能(/p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: 厦门信达网络科技有限公司</p>
* @author 陈庆传 OICQ:507240
* @version 1.0
*/public class SendMail
{
private String errMsg = "";
private ExtendString ExStr = new ExtendString(); private String sender = "";//发件人地址
private String smtpHost = "";//邮件发送服务器(smtp)
private String user = ""; //登录用户名
private String password = "";//登录密码 private String subject = "";//mail主题 public SendMail()
{
this.setPropertiesAttri();
} private void setPropertiesAttri()
{
try
{ InputStream is = getClass().getResourceAsStream("MailServer.properties");
Properties prop = new Properties();
prop.load(is); this.setSmtpHost(prop.get("SmtpHost").toString());
//System.err.println("getSmtpHost:"+this.getSmtpHost());
this.setUser(prop.get("User").toString());
//System.err.println("getUser:"+this.getUser());
this.setPassword(prop.get("Password").toString());
//System.err.println("getPassword:"+this.getPassword());
this.setSender(prop.get("Sender").toString());
//System.err.println("getSender:"+this.getSender());
this.setSubject(ExStr.CS(prop.get("Subject").toString()));
//System.err.println("getSubject:"+this.getSubject());
}
catch(Exception ex)
{
System.err.println("ex1 in sendmail.java:"+ex.toString());
}
} /** 设置发件人地址 */ public void setSender(String sender)
{
this.sender = sender;
}
public String getSender()
{
return sender;
} /** 设置邮件发送服务器(smtp) */
public void setSmtpHost(String smtpHost) {this.smtpHost = smtpHost;}
public String getSmtpHost() {return smtpHost;} /** 设置登录用户名 */
public void setUser(String user)
{
this.user = user;
}
public String getUser()
{
return user;
} /** 设置登录密码 */
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return password;
} /** 设置mail主题 */
public void setSubject(String subject)
{
this.subject = subject;
}
public String getSubject()
{
return subject;
} /**
* 使用smtp发送邮件 主程序
* @throws MessagingException mail发送失败
*/
public void smtp(String receiver,String content) throws MessagingException
{
if (smtpHost == null) throw new MessagingException("smtpHost not found");
if (user == null) throw new MessagingException("user not found");
if (password == null) throw new MessagingException("password not found"); Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);//设置smtp主机
properties.put("mail.smtp.auth", "true");//使用smtp身份验证 Session session = Session.getDefaultInstance(properties, new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, password);
}
}
); //获得邮件会话对象
MimeMessage mimeMsg = new MimeMessage(session);//创建MIME邮件对象
if (sender != null)//设置发件人地址
{
mimeMsg.setFrom(new InternetAddress(sender));
}
if (receiver != null)//设置收件人地址
{
mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver));
}
if (subject != null)//设置邮件主题
{
mimeMsg.setSubject(subject, "GBK");
}
MimeBodyPart part = new MimeBodyPart();//mail内容部分
part.setText(content == null ? "" : content, "GBK"); //设置邮件格式为html cqc
part.setContent(content.toString(),"text/html;charset=GBK");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(part);//在 Multipart 中增加mail内容部分
mimeMsg.setContent(multipart);//增加 Multipart 到信息体
mimeMsg.setSentDate(new Date());//设置发送日期
Transport.send(mimeMsg);//发送邮件
//System.err.println("邮件发送成功!");
} /** 解析地址集合字符串 */
private InternetAddress[] parse(String addressSet) throws AddressException
{
ArrayList list = new ArrayList();
StringTokenizer tokens = new StringTokenizer(addressSet, ";");
while (tokens.hasMoreTokens())
{
list.add(new InternetAddress(tokens.nextToken().trim()));
}
InternetAddress[] addressArray = new InternetAddress[list.size()];
list.toArray(addressArray);
return addressArray;
} //供外部调用的接口
public boolean sendMails(String mail,String content)
{
int mailLen = 0 ;
int contentLen= 0;
if (mail == null||content==null)
{
return false;
} try
{
this.smtp(mail,content);
}
catch(Exception ex)
{
System.err.println("ex2 in sendmail.java:"+ex.toString());
} return true;
} // for test
/*
public static void main (String[] args)
{
SendMail mail = new SendMail();
String email = "[email protected]";
String content = "账号:123 密码:123 <br>感谢您注册使用福建省大中专毕业生就业创业公共服务网!<br><a href='http://www.xindeco.com.cn' target='_blank'>www.xindeco.com.cn</a><br>此致<br>陈庆传 <br>即日";
try
{
mail.sendMails(email,content);
}
catch (Exception ex)
{
System.err.println("ex33:"+ex.toString());
}
}
*/ }
ExtendString.java /**
去掉字符串两端的空白字符,并将字符串转化为中国的标准字符gb2312的字符串.
*/
public String CS(String str) { //去掉字符串2端的空白字符
try {
if (str == null)
return "";
str = str.trim();
if (str == null)
return "";
str = new String(str.getBytes("8859_1"), "GBK");
}
catch (Exception e) {
System.out.println(e);
}
return str;
}
servlet里面你可以直接调用它
jsp里面啊,和一般的javabean声明,和servlet一样使用。
* Created on 2004/07/23
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.mail;/**
* @author geliang
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;public class SendEMailBean {
private String to="";
private String bcc = "";
private String cc = "";
private String from=""; //The Sender
private String serverHost="";
private Vector file = null;
private String subject="";
private String body = "";
public void setTo(String to){
this.to = to;
}
public String getTo(){
return to;
}
public void setBcc(String bcc){
this.bcc = bcc;
}
public String getBcc(){
return bcc;
}
public void setCc(String cc){
this.cc = cc;
}
public String getCc(){
return cc;
}
public void setFrom(String from){
this.from = from;
}
public String getFrom(){
return from;
}
public void setServerHost(String serverHost){
this.serverHost = serverHost;
}
public String getServerHost(){
return serverHost;
}
public void setFile(Vector file){
this.file = file;
}
public Vector getFile(){
return file;
}
public void setSubject(String subject){
this.subject = subject;
}
public String getSubject(){
return subject;
}
public void setBody(String body){
this.body = body;
}
public String getBody(){
return body;
}
public boolean sendMail(){
Properties props = System.getProperties();
props.put("mail.smtp.host", getServerHost());
//props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props, null);
//session.setDebug(true);
try{
MimeMessage msg = new MimeMessage(session);
if(from != null && !from.equals(""))
msg.setFrom(new InternetAddress(from));
StringTokenizer str = new StringTokenizer(to,",");
InternetAddress[] address=new InternetAddress[str.countTokens()];
for(int i=0;i<address.length;i++){
address[i] = new InternetAddress(str.nextToken());
}
msg.setRecipients(Message.RecipientType.TO,address);
if (getBcc() != null && !getBcc().equals("")){
str = new StringTokenizer(bcc,",");
address=new InternetAddress[str.countTokens()];
for(int i=0;i<address.length;i++){
address[i] = new InternetAddress(str.nextToken());
}
msg.setRecipients(Message.RecipientType.BCC,address);
}
if (getCc() != null && !getCc().equals("")){
str = new StringTokenizer(cc,",");
address=new InternetAddress[str.countTokens()];
for(int i=0;i<address.length;i++){
address[i] = new InternetAddress(str.nextToken());
}
msg.setRecipients(Message.RecipientType.CC,address);
}
msg.setSubject(subject);
msg.setText(getBody());
if(getFile() != null){
String filename;
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(getBody());
mp.addBodyPart(mbp);
Enumeration efile=file.elements();
while(efile.hasMoreElements()){
mbp=new MimeBodyPart();
filename=efile.nextElement().toString();
FileDataSource fds=new FileDataSource(filename);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
file.removeAllElements();
msg.setContent(mp);
}
msg.setSentDate(new Date());
Transport.send(msg);
return true;
}
catch(MessagingException mex){
mex.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
return false;
}
}