欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

javamail 包含动态图片 src cid,javamailcid,[Java]代码pack

来源: javaer 分享于  点击 30885 次 点评:244

javamail 包含动态图片 src cid,javamailcid,[Java]代码pack


[Java]代码

package com.kevin.springmail;  import java.io.File;  import java.io.IOException;  import javax.mail.MessagingException;  import javax.mail.internet.MimeMessage;  import javax.mail.internet.MimeUtility;  import org.apache.commons.logging.Log;  import org.apache.commons.logging.LogFactory;  import org.apache.log4j.BasicConfigurator;  import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;  import org.springframework.core.io.FileSystemResource;  import org.springframework.core.io.UrlResource;  import org.springframework.mail.SimpleMailMessage;  import org.springframework.mail.javamail.JavaMailSenderImpl;  import org.springframework.mail.javamail.MimeMessageHelper;  /** *//**  * @author Administrator  * @date   2006-9-15  * @time   22:16:34  */  public class MailFactory ...{      private static final Log logger = LogFactory.getLog(MailFactory.class);      private static final ApplicationContext context;      /** *//**      * 初始化beans      */      static ...{          //加载log4j日志记录          BasicConfigurator.configure();          String springbean = "com/kevin/springmail/springmail.xml";          context = new ClassPathXmlApplicationContext(springbean);          logger.info("初始化beans springmail.xml 完成!");      }      /** *//**      * 发送简单的邮件信息,邮件的内容都是纯文本的内容      *      */      public static void sendSimpleMessageMail()...{          SimpleMailMessage mailmessage = (SimpleMailMessage)context.getBean("mailmessage");          JavaMailSenderImpl mailsender = (JavaMailSenderImpl)context.getBean("mailsender");          mailmessage.setText("你好,Jarry!");          mailsender.send(mailmessage);          logger.info("simple mail has bean sent !");      }      /** *//**      * 发送HTML格式的邮件,HTML格式中带有图片的,      * 图片的来源是Server端的文件系统。(图片就是文件系统的)      * @throws MessagingException      * @throws IOException      */      public static void sendMimeMessageMail() throws MessagingException, IOException...{          JavaMailSenderImpl mailsender = (JavaMailSenderImpl)context.getBean("mailsender");          MimeMessage mimeMessage = mailsender.createMimeMessage();          MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "GB2312");          StringBuffer html = new StringBuffer();          html.append("<html>");          html.append("<head>");          html.append("<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");          html.append("</head>");          html.append("<body bgcolor='#ccccff'>");          html.append("<center>");          html.append("<h1>你好,Jarry</h1>");          html.append("<img src='cid:img'>");          html.append("<p>logo:");          html.append("<img src='cid:logo'>");          html.append("</center>");          html.append("</body>");          html.append("</html>");          helper.setText(html.toString(), true);          FileSystemResource image = new FileSystemResource(new File("icon.gif"));          helper.addInline("img",image);          FileSystemResource logo = new FileSystemResource(new File("logo.gif"));          helper.addInline("logo",logo);          helper.setFrom("green006@163.com");          helper.setTo("green006@163.com");          helper.setSubject("spring javamail test");          logger.info(mimeMessage.getContentID());          logger.info(mimeMessage.getContent());          mailsender.send(mimeMessage);          logger.info("mime mail has bean sent !");      }      /** *//**      * 发送带动态图象的HTML邮件,所谓动态图象就是在发送邮件时      * 动态地生成一个图片,然后再随HTML格式的邮件发送出去。      * @throws MessagingException       * @throws IOException       *      */      public static void sendDynamicImageMail() throws MessagingException, IOException...{          JavaMailSenderImpl mailsender = (JavaMailSenderImpl)context.getBean("mailsender");          MimeMessage mimeMessage = mailsender.createMimeMessage();          MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "GB2312");          StringBuffer html = new StringBuffer();          html.append("<html>");          html.append("<head>");          html.append("<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");          html.append("</head>");          html.append("<body bgcolor='#ccccff'>");          html.append("<center>");          html.append("<h1>你好,Jarry</h1>");          html.append("<img src='cid:png'>");//cid:png中的png就是下面的helper.addInline("png",image);中的png          html.append("</body>");          html.append("</html>");          //设定邮件的正文内容          helper.setText(html.toString(), true);          //new一个UrlResource对象,注意http://localhost:8080/springtiles/makechart.png看起来好像一个png格式的          //图片,其实makechart.png本质上是一个Servlet,在这个Servlet中用JFreeChart构造了一个3D的图象。          UrlResource image = new UrlResource("http://localhost:8080/springtiles/makechart.png");          //把生成的image图象添加到邮件信息中          helper.addInline("png",image);          helper.setFrom("green006@163.com");          helper.setTo("green006@163.com");          helper.setSubject("spring javamail test");          logger.info(mimeMessage.getContentID());          logger.info(mimeMessage.getContent());          mailsender.send(mimeMessage);          logger.info("dynamic image mail has bean sent !");      }      /** *//**      *  发送带附件的电子邮件,包括文件名是中文的。      *  (中文比较特殊些,不然会出现乱码)      *       * @throws MessagingException      * @throws IOException       */      public static void sendMailWithAttachment() throws MessagingException, IOException...{          JavaMailSenderImpl mailsender = (JavaMailSenderImpl)context.getBean("mailsender");          MimeMessage mimeMessage = mailsender.createMimeMessage();          MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "GB2312");          StringBuffer html = new StringBuffer();          html.append("<html>");          html.append("<head>");          html.append("<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");          html.append("</head>");          html.append("<body bgcolor='#ccccff'>");          html.append("<center>");          html.append("<h1>你好,Jarry</h1>");          html.append("<p>logo:");          html.append("<img src='cid:logo'>");          html.append("</center>");          html.append("</body>");          html.append("</html>");          helper.setText(html.toString(), true);          FileSystemResource logo = new FileSystemResource(new File("logo.gif"));          helper.addInline("logo",logo);          //添加附件          File file = new File("build.xml");          helper.addAttachment("build.xml",file);          //添加中文名的,不做下面的处理会出现乱码的。          String filename = MimeUtility.encodeText(new String("个人助理-公元农历.mht".getBytes(),"GB2312"),"GB2312","B");          File f = new File("个人助理-公元农历.mht");          helper.addAttachment(filename,f);          helper.setFrom("green006@163.com");          helper.setTo("green006@163.com");          helper.setSubject("spring javamail test");          logger.info(mimeMessage.getContentID());          logger.info(mimeMessage.getContent());          mailsender.send(mimeMessage);          logger.info("a mime mail with an attachment has bean sent !");      }  }  
相关栏目:

用户点评