importjava.io.ByteArrayOutputStream;importjava.io.FileInputStream;importjava.io.IOException;importjava.util.Arrays;importjava.util.Date;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjavax.mail.BodyPart;importjavax.mail.Multipart;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMultipart;importorg.apache.struts.util.MessageResources;importcom.qq.connect.javabeans.tenpay.Address;//import com.sun.istack.internal.ByteArrayDataSource;
public classSendMails {public int sendMail(String emailTitle, String content, String emailTo,MessageResources message) throwsException {
System.out.println(smtpServer1);
Properties props= newProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smpt");
props.put("mail.smtp.host", smtpServer1);//获得邮件会话对象
Session session = Session.getInstance(props,newSmtpAuthenticator1(fromMailAddress1, password1));/******************************************************/
//创建MIME邮件对象
MimeMessage mimeMessage = newMimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromMailAddress1));//发件人
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));//收件人
mimeMessage.setSubject(emailTitle);
mimeMessage.setSentDate(new Date());//发送日期
Multipart mp = new MimeMultipart("related");//related意味着可以发送html格式的邮件
/******************************************************/BodyPart bodyPart= new MimeBodyPart();//正文
StringBuffer content = newStringBuffer();
bodyPart.setDataHandler(new DataHandler(content.toString(),"text/html;charset=GBK"));//网页格式
/******************************************************/
//BodyPart attachBodyPart = new MimeBodyPart();//普通附件//FileDataSource fds = new FileDataSource("c:/boot.ini");//attachBodyPart.setDataHandler(new DataHandler(fds));//attachBodyPart.setFileName("=?GBK?B?"+ new sun.misc.BASE64Encoder().encode(fds.getName().getBytes()) + "?=");//解决附件名中文乱码//mp.addBodyPart(attachBodyPart);
/******************************************************/
//MimeBodyPart imgBodyPart = new MimeBodyPart();//附件图标//byte[] bytes = readFile("E:/webcms/webcms/WebContent/images/bjx.jpg");//ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,"application/octet-stream");//imgBodyPart.setDataHandler(new DataHandler(fileds));//imgBodyPart.setFileName("button.gif");//imgBodyPart.setHeader("Content-ID", "");//在html中使用该图片方法src="cid:IMG1"//mp.addBodyPart(imgBodyPart);
/******************************************************/mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);//设置邮件内容对象
try{
Transport.send(mimeMessage);//发送邮件//Transport transport=session.getTransport();//transport.send(mimeMessage);
} catch(Exception e) {//e.printStackTrace();
if(e.getMessage().contains("550 User not found")){return 2;
}else{return 3;
}
}//system.out.println("SendEmail OK!!");
return 0;
}public voidsetUsername1(String username1) {this.username1 =username1;
}public voidsetPassword1(String password1) {this.password1 =password1;
}public voidsetSmtpServer1(String smtpServer1) {this.smtpServer1 =smtpServer1;
}public voidsetFromMailAddress1(String fromMailAddress1) {this.fromMailAddress1 =fromMailAddress1;
}public static byte[] readFile(String file) {
FileInputStream fis= null;
ByteArrayOutputStream bos= null;try{
fis= newFileInputStream(file);
bos= newByteArrayOutputStream();intbytesRead;byte buffer[] = new byte[1024 * 1024];while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer,0, bytesRead);
Arrays.fill(buffer, (byte) 0);
}
}catch(IOException e1) {
e1.printStackTrace();
}finally{try{if (bos != null)
bos.close();
}catch(IOException e) {
e.printStackTrace();
}
}returnbos.toByteArray();
}
}/*** Smtp认证*/
class SmtpAuthenticator1 extendsAuthenticator {
String username= null;
String password= null;//SMTP身份验证
publicSmtpAuthenticator1(String username, String password) {this.username =username;this.password =password;
System.out.println(username+"::::::"+password);
}publicPasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(this.username, this.password);
}
}