|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
专门做了这个例子;而java的这个例子好像就是为了教学而写的,很多教学目的的例子是不考虑优化、性能的。发邮件起首引进j2ee的包路径到ClassPath,已安装Jbuilder的能够在它的安装路径lib目次下找
到mail.jar,activation.jar,将这两个包到场体系的情况变量,那末即便不必JBuilder开辟的话也能够
利用了.
程序:
importjava.io.*;
importjava.util.*;
importjavax.mail.*;
importjavax.mail.internet.*;
/**
*<p>Title:浅易MailBean</p>
*<p>Description:供应烦琐的Mail发送功效(/p>
*<p>Copyright:Copyright(c)2003</p>
*@version1.0
*/
classExtendString{
publicExtendString(){
}
/**
往失落字符串两头的空缺字符,并将字符串转化为中国的尺度字符gb2312的字符串.
*/
publicStringCS(Stringstr){//往失落字符串2真个空缺字符
try{
if(str==null)
return"";
str=str.trim();
if(str==null)
return"";
str=newString(str.getBytes("8859_1"),"GBK");
}
catch(Exceptione){
System.out.println(e);
}
returnstr;
}
}
publicclassSendMail
{
privateStringerrMsg="";
privateExtendStringExStr=newExtendString();
privateStringsender="";//发件人地点
privateStringsmtpHost="";//邮件发送服务器(smtp)
privateStringuser="";//登任命户名
privateStringpassword="";//登录暗码
privateStringsubject="";//mail主题
publicSendMail()
{
this.setPropertiesAttri();
}
privatevoidsetPropertiesAttri()
{
try
{
InputStreamis=getClass().getResourceAsStream("MailServer.properties");
Propertiesprop=newProperties();
prop.load(is);
this.setSmtpHost(prop.get("SmtpHost").toString());
this.setUser(prop.get("User").toString());
this.setPassword(prop.get("Password").toString());
this.setSender(prop.get("Sender").toString());
this.setSubject(ExStr.CS(prop.get("Subject").toString()));
}
catch(Exceptionex)
{
System.err.println("ex1insendmail.java:"+ex.toString());
}
}
/**设置发件人地点*/
publicvoidsetSender(Stringsender)
{
this.sender=sender;
}
publicStringgetSender()
{
returnsender;
}
/**设置邮件发送服务器(smtp)*/
publicvoidsetSmtpHost(StringsmtpHost){this.smtpHost=smtpHost;}
publicStringgetSmtpHost(){returnsmtpHost;}
/**设置登任命户名*/
publicvoidsetUser(Stringuser)
{
this.user=user;
}
publicStringgetUser()
{
returnuser;
}
/**设置登录暗码*/
publicvoidsetPassword(Stringpassword)
{
this.password=password;
}
publicStringgetPassword()
{
returnpassword;
}
/**设置mail主题*/
publicvoidsetSubject(Stringsubject)
{
this.subject=subject;
}
publicStringgetSubject()
{
returnsubject;
}
/**
*利用smtp发送邮件主程序
*@throwsMessagingExceptionmail发送失利
*/
publicvoidsmtp(Stringreceiver,Stringcontent)throwsMessagingException
{
if(smtpHost==null)thrownewMessagingException("smtpHostnotfound");
if(user==null)thrownewMessagingException("usernotfound");
if(password==null)thrownewMessagingException("passwordnotfound");
Propertiesproperties=newProperties();
properties.put("mail.smtp.host",smtpHost);//设置smtp主机
properties.put("mail.smtp.auth","true");//利用smtp身份考证
Sessionsession=Session.getDefaultInstance(properties,
newAuthenticator(){
publicPasswordAuthenticationgetPasswordAuthentication(){
returnnewPasswordAuthentication(user,password);
}
});
//取得邮件会话对象
MimeMessagemimeMsg=newMimeMessage(session);//创立MIME邮件对象
if(sender!=null)//设置发件人地点
{
mimeMsg.setFrom(newInternetAddress(sender));
}
if(receiver!=null)//设置收件人地点
{
mimeMsg.setRecipients(Message.RecipientType.TO,parse(receiver));
}
if(subject!=null)//设置邮件主题
{
mimeMsg.setSubject(subject,"GBK");
}
MimeBodyPartpart=newMimeBodyPart();//mail内容部分
part.setText(content==null?"":content,"GBK");
//设置邮件格局为htmlcqc
part.setContent(content.toString(),"text/html;charset=GBK");
Multipartmultipart=newMimeMultipart();
multipart.addBodyPart(part);//在Multipart中增添mail内容部分
mimeMsg.setContent(multipart);//增添Multipart到信息体
mimeMsg.setSentDate(newDate());//设置发送日期
Transport.send(mimeMsg);//发送邮件
}
/**剖析地点汇合字符串*/
privateInternetAddress[]parse(StringaddressSet)throwsAddressException
{
ArrayListlist=newArrayList();
StringTokenizertokens=newStringTokenizer(addressSet,";");
while(tokens.hasMoreTokens())
{
list.add(newInternetAddress(tokens.nextToken().trim()));
}
InternetAddress[]addressArray=newInternetAddress[list.size()];
list.toArray(addressArray);
returnaddressArray;
}
/**
*供内部挪用的接口
*/
publicbooleansendMails(Stringmail,Stringcontent)
{
intmailLen=0;
intcontentLen=0;
if(mail==null||content==null)
{
returnfalse;
}
try
{
this.smtp(mail,content);
}
catch(Exceptionex)
{
System.err.println("ex2insendmail.java:"+ex.toString());
}
returntrue;
}
publicstaticvoidmain(String[]args)
{
SendMailmail=newSendMail();
Stringemail="feng_lei@ecfounder.com;ladofwind@163.com";
Stringcontent="账号:123暗码:123<br/>感激您注册!<br/><ahref=http://www.xxxx.com.cntarget=_blank>www.xxxx.com.cn</a><br/>此致<br/>xxxx<br/>克日";
try
{
mail.sendMails(email,content);
}
catch(Exceptionex)
{
System.err.println("ex33:"+ex.toString());
}
}
}
编译经由过程后,在class文件的目次下创建一个文本文件MailServer.properties,
格局:
SmtpHost=smtp.163.com
User=user
Password=pwd
Sender=csdn@csdn.com
Subject=Hello
运转程序便可完成邮件发送!
你通过从书的数量和开发周期及运行速度来证明:net和ruby要比java简单。 |
|