发送邮件的主程序
importjava.util.Properties;
importcommon.util.Email_Autherticatorbean;
importjavax.mail.Authenticator;
importjavax.mail.internet.InternetAddress;
importorg.apache.commons.lang.StringUtils;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.Multipart;
importjavax.activation.FileDataSource;
importjavax.mail.internet.MimeMultipart;
importjavax.activation.DataHandler;
importjavax.mail.internet.MimeUtility;
importjava.util.Date;
/**
*利用java.mail的邮件发送程序
*/
publicclassSendMailTest
{
publicstaticvoidmain(String[]args)
{
Stringtitle="titleTest";//所发送邮件的标题
Stringfrom="sir_znp@163.com";//从那里发送
StringsendTo[]={"znp@163.net","sir_znp@163.com"};//发送到那里
//邮件的文本内容,可以包含html标记则显示为html页面
Stringcontent="mailtest!!!!!!<br><ahref=#>aaa</a>";
//所包含的附件,及附件的重新命名
StringfileNames[]={"F:musictext1.txt,text1.txt","F:musictext2.txt,text2.txt"};
try{
//MailSendermailsender=newMailSender();
sendmail(title,from,sendTo,content,fileNames,"text/html;charset=gb2312");
}catch(Exceptionex){ex.printStackTrace();}
}
publicstaticvoidsendmail(Stringsubject,Stringfrom,String[]to,Stringtext,String[]filenames,StringmimeType)throwsException
{
//ResourceBundlemailProps=ResourceBundle.getBundle("mail");可以从配置文件读取相应的参数
Propertiesprops=newProperties();
Stringsmtp="smtp.163.com";//设置发送邮件所用到的smtp
Stringservername="sir_znp";
Stringserverpaswd="123";
javax.mail.SessionmailSession;//邮件会话对象
javax.mail.internet.MimeMessagemimeMsg;//MIME邮件对象
props=java.lang.System.getProperties();//获得系统属性对象
props.put("mail.smtp.host",smtp);//设置SMTP主机
props.put("mail.smtp.auth","true");//是否到服务器用户名和密码验证
//到服务器验证发送的用户名和密码是否正确
Email_AutherticatorbeanmyEmailAuther=newEmail_Autherticatorbean(servername,serverpaswd);
//设置邮件会话
mailSession=javax.mail.Session.getInstance(props,(Authenticator)myEmailAuther);
//设置传输协议
javax.mail.Transporttransport=mailSession.getTransport("smtp");
//设置from、to等信息
mimeMsg=newjavax.mail.internet.MimeMessage(mailSession);
if(!StringUtils.isEmpty(from))
{
InternetAddresssentFrom=newInternetAddress(from);
mimeMsg.setFrom(sentFrom);//设置发送人地址
}
InternetAddress[]sendTo=newInternetAddress[to.length];
for(inti=0;i<to.length;i++)
{
System.out.println("发送到:"+to[i]);
sendTo[i]=newInternetAddress(to[i]);
}
mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO,sendTo);
mimeMsg.setSubject(subject,"gb2312");
MimeBodyPartmessageBodyPart1=newMimeBodyPart();
//messageBodyPart.setText(UnicodeToChinese(text));
messageBodyPart1.setContent(text,mimeType);
Multipartmultipart=newMimeMultipart();//附件传输格式
multipart.addBodyPart(messageBodyPart1);
for(inti=0;i<filenames.length;i++){
MimeBodyPartmessageBodyPart2=newMimeBodyPart();
//选择出每一个附件名
Stringfilename=filenames[i].split(",")[0];
System.out.println("附件名:"+filename);
Stringdisplayname=filenames[i].split(",")[1];
//得到数据源
FileDataSourcefds=newFileDataSource(filename);
//得到附件本身并至入BodyPart
messageBodyPart2.setDataHandler(newDataHandler(fds));
//得到文件名同样至入BodyPart
//messageBodyPart2.setFileName(displayname);
//messageBodyPart2.setFileName(fds.getName());
messageBodyPart2.setFileName(MimeUtility.encodeText(displayname));
multipart.addBodyPart(messageBodyPart2);
}
mimeMsg.setContent(multipart);
//设置信件头的发送日期
mimeMsg.setSentDate(newDate());
mimeMsg.saveChanges();
//发送邮件
transport.send(mimeMsg);
transport.close();
}
}
验证类
packagecommon.util;
importjavax.mail.Authenticator;
importjavax.mail.PasswordAuthentication;
publicclassEmail_AutherticatorbeanextendsAuthenticator
{
privateStringm_username=null;
privateStringm_userpass=null;
publicvoidsetUsername(Stringusername)
{
m_username=username;
}
publicvoidsetUserpass(Stringuserpass)
{
m_userpass=userpass;
}
publicEmail_Autherticatorbean(Stringusername,Stringuserpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
publicPasswordAuthenticationgetPasswordAuthentication()
{
returnnewPasswordAuthentication(m_username,m_userpass);
}
}