importcom.sun.mail.util.MailSSLSocketFactory;import java.io.*;importjava.security.GeneralSecurityException;import java.util.*;import javax.activation.*;import javax.mail.*;import javax.mail.internet.*;public classSendMail {private String username = null;private String password = null;private Authenticator auth = null;private MimeMessage mimeMessage =null;private Properties pros = null;private Multipart multipart = null;private BodyPart bodypart= null;/*** 初始化账号密码并验证
* 创建MimeMessage对象
* 发送邮件必须的步骤:1
*@paramusername
*@parampassword*/
publicSendMail(String username,String password){this.username =username;this.password =password;
}/*** 设置email系统参数
* 接收一个map集合key为string类型,值为String
* 发送邮件必须的步骤:2
*@parammap*/
public void setPros(Mapmap){
pros= newProperties();for(Map.Entryentry:map.entrySet()){
pros.setProperty(entry.getKey(), entry.getValue());
}
}/*** 初始化MimeMessage对象
* 发送邮件必须的步骤:3*/
public voidinitMessage(){this.auth = newEmail_Autherticator();
Session session=Session.getDefaultInstance(pros,auth);
mimeMessage= newMimeMessage(session);
}/*** 验证账号密码
* 发送邮件必须的步骤
*@authorAdministrator
**/
public class Email_Autherticator extendsAuthenticator
{publicPasswordAuthentication getPasswordAuthentication()
{return newPasswordAuthentication(username, password);
}
}/*** 设置发送邮件的基本参数(去除繁琐的邮件设置)
*@paramsub 设置邮件主题
*@paramtext 设置邮件文本内容
*@paramrec 设置邮件接收人
*@throwsMessagingException
*@throwsUnsupportedEncodingException*/
public void setDefaultMessagePros(String sub,String text,String rec) throwsMessagingException, UnsupportedEncodingException{
mimeMessage.setSubject(sub);
mimeMessage.setText(text);
mimeMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(rec));
mimeMessage.setSentDate(newDate());
mimeMessage.setFrom(newInternetAddress(username,username));
}/*** 设置主题
*@paramsubject
*@throwsMessagingException*/
public void setSubject(String subject) throwsMessagingException{
mimeMessage.setSubject(subject);
}/*** 设置日期
*@paramdate
*@throwsMessagingException*/
public void setDate(Date date) throwsMessagingException{
mimeMessage.setSentDate(newDate());
}/*** 设置邮件文本内容
*@paramtext
*@throwsMessagingException*/
public void setText(String text) throwsMessagingException{
mimeMessage.setText(text);
}/*** 设置邮件头部
*@paramarg0
*@paramarg1
*@throwsMessagingException*/
public void setHeader(String arg0,String arg1) throwsMessagingException{
mimeMessage.setHeader(arg0, arg1);
}/*** 设置邮件接收人地址
*@paramrecipient
*@throwsMessagingException*/
public void setRecipient(String recipient) throwsMessagingException{
mimeMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(recipient));
}/*** 设置邮件接收人地址
*@paramlist
*@throwsMessagingException
*@throwsAddressException*/
public String setRecipients(List recs) throwsAddressException, MessagingException{if(recs.isEmpty()){return "接收人地址为空!";
}for(String str:recs){
mimeMessage.addRecipient(Message.RecipientType.TO,newInternetAddress(str));
}return "加入接收人地址成功!";
}/*** 设置邮件接收人地址
*@paramStringBuffer
*@throwsMessagingException
*@throwsAddressException*/@SuppressWarnings("static-access")public String setRecipients(StringBuffer sb) throwsAddressException, MessagingException{if(sb==null||"".equals(sb)){return "字符串数据为空!";
}
Address []address= newInternetAddress().parse(sb.toString());
mimeMessage.addRecipients(Message.RecipientType.TO, address);return "收件人加入成功";
}/*** 设置邮件发送人的名字
*@paramfrom
*@throwsUnsupportedEncodingException
*@throwsMessagingException*/
public void setFrom(String from) throwsUnsupportedEncodingException, MessagingException{
mimeMessage.setFrom(newInternetAddress(username,from));
}/*** 发送邮件
* return 是否发送成功
*@throwsMessagingException*/
public String sendMessage() throwsMessagingException{
Transport.send(mimeMessage);return "success";
}/*** 设置附件
*@paramfile 发送文件的路径*/
public void setMultipart(String file) throwsMessagingException, IOException{if(multipart==null){
multipart= newMimeMultipart();
}
multipart.addBodyPart(writeFiles(file));
mimeMessage.setContent(multipart);
}/*** 设置附件
*@paramfileList
*@throwsMessagingException
*@throwsIOException*/
public void setMultiparts(List fileList) throwsMessagingException, IOException{if(multipart==null){
multipart= newMimeMultipart();
}for(String s:fileList){
multipart.addBodyPart(writeFiles(s));
}
mimeMessage.setContent(multipart);
}/*** 发送文本内容,设置编码方式
*
*
*@params
*@paramtype
*@throwsMessagingException*/
public void setContent(String s,String type) throwsMessagingException{if(multipart==null){
multipart= newMimeMultipart();
}
bodypart= newMimeBodyPart();
bodypart.setContent(s, type);
multipart.addBodyPart(bodypart);
mimeMessage.setContent(multipart);
mimeMessage.saveChanges();
}/*** 读取附件
*@paramfilePath
*@return*@throwsIOException
*@throwsMessagingException*/
public BodyPart writeFiles(String filePath)throwsIOException, MessagingException{
File file= newFile(filePath);if(!file.exists()){throw new IOException("文件不存在!请确定文件路径是否正确");
}
bodypart= newMimeBodyPart();
DataSource dataSource= newFileDataSource(file);
bodypart.setDataHandler(newDataHandler(dataSource));//文件名要加入编码,不然出现乱码
bodypart.setFileName(MimeUtility.encodeText(file.getName()));returnbodypart;
}/*** 使用SSL登录
*@throwsGeneralSecurityException*/
public void openSSL() throwsGeneralSecurityException {
MailSSLSocketFactory sf=newMailSSLSocketFactory();
sf.setTrustAllHosts(true);
pros.put("mail.smtp.ssl.enable", "true");
pros.put("mail.smtp.ssl.socketFactory", sf);
}
}