`
azure2a
  • 浏览: 31986 次
  • 性别: Icon_minigender_1
  • 来自: 承德
社区版块
存档分类
最新评论

用javamail发邮件

阅读更多
利用javamail发邮件,可以发送普通邮件,带附件和图片的邮件:
package util.mail;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
/**
* @author Lee
*
* 需要jaf-1_1_1及javamail-1_4_1的支持
*
*/
public class MailUtil {
private static Session session;
private static Transport sender;

private MailUtil(){  
}

public static void createSession(){
   if(session==null){
  String protocol=ResourceBundle.getBundle("mail").getString("mail.transport.protocol");
  String auth=ResourceBundle.getBundle("mail").getString("mail.smtp.auth");
  boolean debug=Boolean.parseBoolean(ResourceBundle.getBundle("mail").getString("debug"));
  
  createSession(protocol,auth,debug);
   }
}

/**
  *
  * @param protocol
  * @param auth
  * @param Debug
  *  用于创建发送邮件的配置类需要协议,是否认证,是否显示信息
  */
public static void createSession(String protocol,String auth,boolean Debug){
  Properties props=new Properties();
  props.setProperty("mail.transport.protocol",protocol);
  props.setProperty("mail.smtp.auth",auth);
  
  session=Session.getInstance(props);
  
  session.setDebug(Debug);
}

/**
  *
  * @param from 发送人的地址
  * @param to 接受人的地址
  * @param title 标题
  * @param body 邮件内容
  * @param date 发送日期
  * @return 返回邮件信息
  * @throws AddressException
  * @throws MessagingException
  * 用于发送文本信息
  */
public static MimeMessage createTextMail(String from,String to,String title,String body,Date date) throws AddressException, MessagingException{
  MimeMessage mail=new MimeMessage(session);
  
  mail.setFrom(new InternetAddress(from));
  mail.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
  mail.setSentDate(date);
  mail.setSubject(title);
  mail.setText(body);
  mail.saveChanges();
  
  return mail;
}

/**
  *
  * @param from
  * @param to
  * @param title
  * @param body
  * @param date
  * @return 返回邮件信息
  * @throws AddressException
  * @throws MessagingException
  * 用于发送html邮件
  */
public static MimeMessage createHtmlMail(String from,String to,String title,String body,Date date) throws AddressException, MessagingException{
  MimeMessage mail=createTextMail(from,to,title,body,date);
  mail.setContent(body,"text/html;charset=gb2312");
  mail.saveChanges();
  return mail;
}

/**
  *
  * @param from
  * @param to
  * @param title
  * @param body
  * @param date
  * @param filenames
  * @param imgnames
  * @return 返回邮件信息
  * @throws AddressException
  * @throws MessagingException
  * 创建带图片和附件的邮件
  */
public static MimeMessage createMultiMail(String from,String to,String title,String body,Date date,String[] filenames,String[] imgnames) throws AddressException, MessagingException{
  MimeMessage mail=createHtmlMail(from,to,title,body,date);
  
  MimeMultipart allMultipart=new MimeMultipart("mixed");  
  
  MimeMultipart contentMulti=new MimeMultipart("related");  
  MimeBodyPart contentPart=new MimeBodyPart();
  
  MimeBodyPart htmlBody=new MimeBodyPart();
  
  htmlBody.setContent(body,"text/html;charset=gb2312");
  
  contentMulti.addBodyPart(htmlBody);
  
  if(imgnames!=null){
  for(MimeBodyPart part:createImgAttachment(imgnames)){
   contentMulti.addBodyPart(part);
    }
  }
  
  contentPart.setContent(contentMulti);
  allMultipart.addBodyPart(contentPart);  
  
  if(filenames!=null){  
  for(MimeBodyPart part:createAttachment(filenames)){
   allMultipart.addBodyPart(part);
    }  
  }
  
  mail.setContent(allMultipart);
  
  mail.saveChanges();
  return mail;
}

/**
  *
  * @param server 服务器
  * @param user 用户
  * @param pass 密码
  * @return
  * @throws MessagingException
  * 建立邮件发送类
  */
public static Transport createSender(String server,String user,String pass) throws MessagingException{
  sender=session.getTransport();
  sender.connect(server,user,pass);
  
  return sender;
}

public static Transport createSender() throws MessagingException{
   if(sender==null){
  String server=ResourceBundle.getBundle("mail").getString("server");
  String user=ResourceBundle.getBundle("mail").getString("user");
  String pass=ResourceBundle.getBundle("mail").getString("pass");
  
  return createSender(server,user,pass);
   }
  return sender;
}

public static void send(MimeMessage mail) throws MessagingException{
  createSession();
  createSender();
  sender.sendMessage(mail,mail.getRecipients(Message.RecipientType.TO));  
}

public static void closeSender() throws MessagingException{
  if(sender.isConnected())sender.close();
  sender=null;
  session=null;  
}

public static Set<MimeBodyPart> createAttachment(String[] filenames) throws MessagingException{
  Set<MimeBodyPart> attachments=new HashSet<MimeBodyPart>();
  for(int i=0;i<filenames.length;i++){
    MimeBodyPart attachment=new MimeBodyPart();
    FileDataSource fds=new FileDataSource(filenames);
    attachment.setDataHandler(new DataHandler(fds));
    attachment.setFileName(filenames.substring(filenames.lastIndexOf("\\")+1));
    attachments.add(attachment);
  }
  return attachments;
}

public static Set<MimeBodyPart> createImgAttachment(String[] imgnames) throws MessagingException{
  Set<MimeBodyPart> attachments=new HashSet<MimeBodyPart>();
  for(int i=0;i<imgnames.length;i++){
    MimeBodyPart attachment=new MimeBodyPart();
    FileDataSource fds=new FileDataSource(imgnames);
    attachment.setDataHandler(new DataHandler(fds));
    attachment.setFileName(imgnames.substring(imgnames.lastIndexOf("\\")+1));
    attachment.setContentID(imgnames.substring(imgnames.lastIndexOf("\\")+1));
    attachments.add(attachment);
  }
  return attachments;
}

public static void main(String[] args) throws AddressException, MessagingException{  
  MailUtil.send(MailUtil.createMultiMail("azure2a@163.com","azure2a@163.com","测试","<b>测试邮件<img src=\"cid:a.jpg\"></b>",new Date(),new String[]{"D:\\Java\\a.txt","D:\\Java\\a.jpg"},new String[]{"D:\\Java\\a.jpg"}));
  MailUtil.send(MailUtil.createMultiMail("azure2a@163.com","azure2aa@sina.com","测试","<b>测试邮件<img src=\"cid:a.jpg\"></b>",new Date(),new String[]{"D:\\Java\\a.txt","D:\\Java\\a.jpg"},new String[]{"D:\\Java\\a.jpg"}));
  MailUtil.closeSender();
}
}

配置文件
mail.properties
server=smtp.163.com
user=azure2a
pass=
mail.transport.protocol=smtp
mail.smtp.auth=true
debug=true
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics