使用spring 发送带附件的邮件

来源:互联网 发布:用python生成随机文件 编辑:程序博客网 时间:2024/04/29 21:42

转自:http://www.blogjava.net/amigoxie/archive/2007/04/28/114310.html

1.java代码:

 /**
     * 发送带附件的邮件
     * @param sender 邮件发送器
     * @param fileUrl 附件的绝对路径
     * @throws Exception
     */
 private void sendMimeMessage(final JavaMailSender sender,final String fileUrl) throws Exception {
         //附件文件集合
         final List files = new ArrayList();
         MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
             public void prepare(MimeMessage mimeMessage) throws MessagingException {
                 try {
          mimeMessage.setRecipient(Message.RecipientType.TO,
              new InternetAddress("123456789@qq.com"));//收件人邮箱
          mimeMessage.setFrom(new InternetAddress("chen123456789@126.com"));//发件人邮箱
                   mimeMessage.setSubject("修改后的文件", "gb2312"); //邮件主题
                   Multipart mp = new MimeMultipart(); //Multipart代表正文                 
                   MimeBodyPart content = new MimeBodyPart();
                   content.setText("spring文件,请查收!");//邮件内容
                   mp.addBodyPart(content);
                   //向Multipart添加附件
                   files.add(fileUrl);     //fileUrl文件的绝对路径             
                   Iterator it = files.iterator();
                   while(it.hasNext()) {
                    sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
                       MimeBodyPart attachFile = new MimeBodyPart();
                       String filename = it.next().toString();
                       FileDataSource fds = new FileDataSource(filename);
                       attachFile.setDataHandler(new DataHandler(fds));
                       attachFile.setFileName("=?GBK?B?"+enc.encode(fds.getName().getBytes())+"?=");
                       mp.addBodyPart(attachFile);
                   }                  
                   files.clear();                  
                   //向MimeMessage添加Multipart
                   mimeMessage.setContent(mp);
                   mimeMessage.setSentDate(new Date());
     } catch (AddressException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (javax.mail.MessagingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
             }
         };

         //发送带附件的邮件
         sender.send(mimeMail);        
         System.out.println("邮件发送成功!");
     }

2.配制文件

<!-- Spring发送邮件 -->
   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>smtp.126.com</value>
        </property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
            </props>
        </property>
        <property name="username">
            <!-- 发件人用户名(邮箱号) -->
            <value>chen123456789</value>
        </property>
        <property name="password">
         <!-- 发件人邮箱密码 -->
            <value>xxxxxxx</value>
        </property>
    </bean>

 

原创粉丝点击