java发送邮件

java Mail 邮件群发 带多附件2009年07月21日 星期二 09:26public void sendMail(MailBean mail) throws AddressException,
    MessagingException, UnsupportedEncodingException {
   //stmp主机地址
   String stmp_host = mail.getStmp_host();
   //群发 mail数组 接收者
   String[] tomails = mail.getToMails();
   //附件名数组
   String[] uploadFileName = mail.getUploadFileName();
   //附件数组
   File[] upload = mail.getUpload();
   //显示发件人的名字
   String show_name = mail.getShow_name();
   //回复邮箱地址也就是显示发送者地址
   String recipient_email = mail.getRecipient_email();
   //正确邮件列表
   LinkedList<String> right_mailList = new LinkedList<String>();
   //错误邮件列表
   LinkedList<String> error_mailList = new LinkedList<String>();
   //验证邮箱的正则表达式
   java.util.regex.Pattern par = Pattern
     .compile("^//w+([-+.]//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*$");
   if (par.matcher(recipient_email).matches()) {
    //验证邮件列表
    for (int i = 0; i < tomails.length; i++) {
     String mailtmp = tomails[i];
     if (par.matcher(mailtmp).matches()) {
      right_mailList.add(mailtmp);
     } else {
      error_mailList.add(mailtmp);
     }
    }
    if (right_mailList.size()>0) {
     LinkedList<File> attachlist = new LinkedList<File>();
     if (upload != null) {
      for (int i = 0; i < upload.length; i++) {
       File file = upload[i];
       if (file != null) {
        attachlist.add(file);
       }
      }
     }
     MimeMessage message;
     InternetAddress[] address = new InternetAddress[right_mailList
       .size()];
     for (int i = 0; i < address.length; i++) {
      address[i] = new InternetAddress(right_mailList.get(i));
     }
     Properties props = new Properties();
     props.put("mail.smtp.host", stmp_host);
     props.put("mail.smtp.auth", "true");
     Session session = Session.getDefaultInstance(props, null);
     message = new MimeMessage(session);
     if (show_name == null || show_name.equals("")) {
      message.setFrom(new InternetAddress(recipient_email));
     } else {
      message.setFrom(new InternetAddress(recipient_email,
        show_name));
     }
     message.setRecipients(Message.RecipientType.TO, address);
     message.setSubject(mail.getSubject());
     message.setSentDate(mail.getSendDate());
     // 新建一个mimemultipart对象用来存放bodypart对象(事实上可以存放多个)
     MimeMultipart mm = new MimeMultipart();
     // 新建一个存放信件内容的bodypart对象
     BodyPart mdp = new MimeBodyPart();
     // 给bodypart对象设置内容和格式/编码方式
     mdp.setContent(mail.getContent(), mail.getContentType());
     mm.addBodyPart(mdp);
     // add the attachments
     for (int i = 0; i < attachlist.size(); i++) {
      // 新建一个存放附件的bodypart
      BodyPart bodyPart = new MimeBodyPart();
      FileDataSource fileDS = new FileDataSource(attachlist
        .get(i));
      DataHandler dh = new DataHandler(fileDS);
      bodyPart.setDataHandler(dh);
      // 加上这句将作为附件发送,否则将作为信件的文本内容
      bodyPart.setFileName(uploadFileName[i].toString());
      bodyPart.setDisposition(Part.ATTACHMENT);
      // mdp.setHeader("name", uploadFileName[i].toString());
      bodyPart.setHeader("Content-Type",
        "application/octet-stream;name="
          + uploadFileName[i].toString());
      bodyPart.setHeader("X-Attachment-Id", uploadFileName[i]
        .toString());
      // 将含有附件的bodypart加入到mimemultipart对象中
      mm.addBodyPart(bodyPart);
     }
     // 把mm作为消息对象的内容
     message.setContent(mm);
     message.saveChanges();
     try {
      javax.mail.Transport transport = session
        .getTransport("smtp");
      transport.connect(stmp_host, mail.getStmp_username(), mail
        .getStmp_password());
      transport.sendMessage(message, message.getAllRecipients());
      transport.close();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值