|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你总不能说你写框架吧,那无疑会加大工作量,现在大多企业采取的是折中的办法,就是改别人写好的框架,可要改框架,前提是你对这个框架足够的了解,这就更难了。smtp利用典范:
/*
*newsendmail()利用GBK的默许编码发送文本信息
*newsendmail(编码,文本仍是html);
*/
wuyu.javamail.sendmailmail=newwuyu.javamail.sendmail("GBK",true);
/*
*mail.connect(smtp服务器);
*main.connect(smtp服务器,用户名,暗码);合用于smtp认证的发佩服务器
*/
mail.connect(args[0],args[1],args[2]);
for(inti=0;i<Integer.parseInt(args[3]);i++){
System.out.print(i+" ");
/*
*main.send(from,to,cc,bcc,主题,注释,附件文件名)
*to,cc,bcc可填写多个mail地点
*附件文件名为null,暗示不发送附件
*/
mail.send("mmwy@ynwst.com",args[4],"","","这是主题"+i,"<b>这是注释</b>",null);
System.out.println(i);
}
mail.close();
=-=--------------------
sendmail.java
------------------------------
packagewuyu.javamail;
/**
*挪用javamail发送邮件的类
*/
publicclasssendmail{
privateStringencode;//文本编码
privatebooleanHTML;//文本仍是html内容
privatejavax.mail.Sessionsession;
privatejavax.mail.Transporttransport;
publicsendmail(){
this.encode="GBK";
this.HTML=false;
}
publicsendmail(Stringencode,booleanhtml){
this.encode=encode;
this.HTML=html;
}
publicbooleanconnect(StringsmtpHost){
returnconnect(smtpHost,null,null);
}
publicbooleanconnect(StringsmtpHost,Stringuid,Stringpwd){
try{
java.util.Propertiesproperties=newjava.util.Properties();
properties.put("mail.smtp.host",smtpHost);
if(uid==null||uid.equals("")){
session=javax.mail.Session.getInstance(properties,null);
}else{
properties.put("mail.smtp.auth","true");
Authauth=newAuth(uid,pwd);
session=javax.mail.Session.getInstance(properties,auth);
}
transport=session.getTransport("smtp");
transport.connect();
returntrue;
}
catch(Exceptione){
e.printStackTrace();
returnfalse;
}
}
publicvoidclose(){
if(transport!=null){
try{
transport.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
publicvoidsend(Stringfrom,Stringto,Stringcc,Stringbcc,Stringsubject,Stringtext,String[]filename){
try{
javax.mail.internet.MimeMessagemimeMessage=createMimeMessage(session,
from,to,cc,bcc,subject,text,filename);
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
}
catch(javax.mail.MessagingExceptione){
e.printStackTrace();//TochangebodyofcatchstatementuseOptions|FileTemplates.
}
}
privatejavax.mail.internet.MimeMessagecreateMimeMessage(
javax.mail.Sessionsession,
Stringfrom,Stringto,Stringcc,Stringbcc,
Stringsubject,Stringtext,
String[]filename)throwsjavax.mail.MessagingException{
javax.mail.internet.MimeMessagemessage=newjavax.mail.internet.MimeMessage(session);
javax.mail.internet.InternetAddressfromAddress=newjavax.mail.internet.InternetAddress(from);
message.setFrom(fromAddress);
javax.mail.internet.InternetAddress[]toAddress=javax.mail.internet.InternetAddress.parse(to);
javax.mail.internet.InternetAddress[]ccAddress=javax.mail.internet.InternetAddress.parse(cc);
javax.mail.internet.InternetAddress[]bccAddress=javax.mail.internet.InternetAddress.parse(bcc);
message.setRecipients(javax.mail.Message.RecipientType.TO,toAddress);
message.setRecipients(javax.mail.Message.RecipientType.CC,ccAddress);
message.setRecipients(javax.mail.Message.RecipientType.BCC,bccAddress);
message.setSubject(subject,this.encode);
//处置附件
if(filename!=null&&filename.length>0){
javax.mail.Multipartmultipart=newjavax.mail.internet.MimeMultipart();
javax.mail.internet.MimeBodyPartmimeBodyPart=newjavax.mail.internet.MimeBodyPart();
if(HTML){
mimeBodyPart.setContent(text,"text/html;charset="+this.encode);
}else{
mimeBodyPart.setText(text,this.encode);
}
multipart.addBodyPart(mimeBodyPart);
for(inti=0;i<filename.length;i++){
try{
javax.mail.internet.MimeBodyPartfileBodyPart=newjavax.mail.internet.MimeBodyPart();
javax.activation.DataSourcedatasource=newjavax.activation.FileDataSource(filename[i]);
fileBodyPart.setDataHandler(newjavax.activation.DataHandler(datasource));
fileBodyPart.setFileName(newjava.io.File(filename[i]).getName());
multipart.addBodyPart(fileBodyPart);
}
catch(Exceptione){
e.printStackTrace();
}
}
message.setContent(multipart);
}else{
if(HTML){
message.setContent(text,"text/html;charset="+this.encode);
}else{
message.setText(text,this.encode);
}
}
returnmessage;
}
}
Auth.java
------------------------
packagewuyu.javamail;
publicclassAuthextendsjavax.mail.Authenticator{
privateStringuser,pwd;
publicAuth(Stringuser,Stringpwd){
this.user=user;
this.pwd=pwd;
}
protectedjavax.mail.PasswordAuthenticationgetPasswordAuthentication(){
returnnewjavax.mail.PasswordAuthentication(this.user,this.pwd);
}
}
在1995年5月23日以“Java”的名称正式发布了。 |
|