Java mail添加图片附件

package com.heartjack.db.batch.mail;


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


import com.heartjack.mvc.base.HjBaseException;
import com.heartjack.mvc.helper.HtmlReadHelper;
import com.heartjack.mvc.helper.ReadWriteFile;




public class SendInlineImagesInEmail {


   public static void main(String[] args) throws IOException, InterruptedException {
  
  SendInlineImagesInEmail client = new SendInlineImagesInEmail();
  
  List<String> emails = getEmails();
  List<String> fails = new ArrayList<String>();
  int count = 0;
  
  for (String to : emails) {
  client.SendEmail(getParentPathFile(), to, fails);
  
  count++;
 
  Thread.sleep(1000);
  }
  
  try {
ReadWriteFile.writeTxtFile(getParentPathFile().toString()+ "/hjMVCBatch/mail", "fail", fails.toString());
} catch (HjBaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     
   }
   
   public static File getParentPathFile() {
  File f = new File(".");
  File parentPathFile = f.getAbsoluteFile().getParentFile();

return parentPathFile;
   }
   
   
   public static List<String> getEmails() {
  
  List<String> emails = HtmlReadHelper.readMailList(
  new File( getParentPathFile(), "hjMVCBatch/mail"), "to");
  
  return emails;
   }
   public void SendEmail(File parentPathFile, String to, List<String> fails) {
  
 
  // Recipient's email ID needs to be mentioned.
//     String to = "xxx";



     // Sender's email ID needs to be mentioned
     String from = "xxx";
     final String username = "xxx";//change accordingly
     final String password = "xxx";//change accordingly



     // Assuming you are sending email through smtp.pigai.me
     String host = "smtp.shenqing.me";


     Properties props = new Properties();
     props.put("mail.smtp.auth", "true");
     props.put("mail.smtp.starttls.enable", "true");
     props.put("mail.smtp.host", host);
     props.put("mail.smtp.port", "25");


     Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
           protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(username, password);
           }
        });


     try {


        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);


        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));


        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO,
           InternetAddress.parse(to));


        // Set Subject: header field
        message.setSubject("完全免费的申请神器?高效又使用的写作帮手?你没有看错,这都不是梦!");


        // This mail has 2 part, the BODY and the embedded image
        MimeMultipart multipart = new MimeMultipart("related");


        // first part (the html)
        BodyPart messageBodyPart = new MimeBodyPart();
        //从文件读取编辑内容
       
String info = HtmlReadHelper.readhtml(new File(parentPathFile, "hjMVCBatch/html") ,"mail2");
        
        String htmlText = info + "<img src=\"cid:college\">"+ "<br/>" + "<img src=\"cid:writing\">";
        messageBodyPart.setContent(htmlText, "text/html;charset=UTF-8");
        // add it
        multipart.addBodyPart(messageBodyPart);


        // second part (the image)
        messageBodyPart = new MimeBodyPart();
        DataSource college = new FileDataSource("hjMVCBatch/images/college.png");
        messageBodyPart.setDataHandler(new DataHandler(college));
        messageBodyPart.setHeader("Content-ID", "<college>");
        // add image to the multipart
        multipart.addBodyPart(messageBodyPart);
        
        messageBodyPart = new MimeBodyPart();
        DataSource writing = new FileDataSource("hjMVCBatch/images/writing.jpg");
        messageBodyPart.setDataHandler(new DataHandler(writing));
        messageBodyPart.setHeader("Content-ID", "<writing>");
        
        // add image to the multipart
        multipart.addBodyPart(messageBodyPart);
        // put everything together
        message.setContent(multipart);
        // Send message
        message.saveChanges();
        Transport.send(message);
        session = null;
        message = null;
        college = null;
        writing = null;
        System.out.println(to + "   Sent");


     } catch (MessagingException e) {
    byte[] bytes = e.getMessage().getBytes();
    try {
String msg = new String(bytes,"ISO8859-1");
System.out.println(to + "   (" + msg + ")");
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
    fails.add(to + "   (" + e.getMessage() + ")\r\n");
     
//        throw new RuntimeException(e);
     }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值