gdufo

           

          javamail與exchange 發(fā)送圖片與文字(圖片內(nèi)嵌)

          package com.ellington.test;

          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.util.Date;  
          import java.util.Properties;  
           
          import javax.mail.BodyPart;  
          import javax.mail.Message;  
          import javax.mail.MessagingException;  
          import javax.mail.Multipart;  
          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 sun.misc.BASE64Decoder;
          import sun.misc.BASE64Encoder;
           
          public class Mailer {  
              private String host;  
              private String auth;  
              private String username;  
              private String domainUser;  
              private String password;  
           
              public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content) throws MessagingException {  
                  Properties props = new Properties();  
                  props.put("mail.smtp.host", host);  
                  props.put("mail.smtp.auth", auth);  
                  Session s = Session.getInstance(props);  
                  //      s.setDebug(true);  
           
                  MimeMessage message = new MimeMessage(s);  
           
                  InternetAddress from = new InternetAddress(username);  
                  message.setFrom(from);  
                  InternetAddress[] Toaddress = new InternetAddress[to.length];  
                  for (int i = 0; i < to.length; i++)  
                      Toaddress[i] = new InternetAddress(to[i]);  
                  message.setRecipients(Message.RecipientType.TO, Toaddress);  
           
                  if (cc != null) {  
                      InternetAddress[] Ccaddress = new InternetAddress[cc.length];  
                      for (int i = 0; i < cc.length; i++)  
                          Ccaddress[i] = new InternetAddress(cc[i]);  
                      message.setRecipients(Message.RecipientType.CC, Ccaddress);  
                  }  
           
                  if (bcc != null) {  
                      InternetAddress[] Bccaddress = new InternetAddress[bcc.length];  
                      for (int i = 0; i < bcc.length; i++)  
                          Bccaddress[i] = new InternetAddress(bcc[i]);  
                      message.setRecipients(Message.RecipientType.BCC, Bccaddress);  
                  }  
                  message.setSubject(subject);  
                  message.setSentDate(new Date());  
           
                  BodyPart mdp = new MimeBodyPart();  
                  mdp.setContent(content, "text/html;charset=utf-8");  
                  Multipart mm = new MimeMultipart();  
                  mm.addBodyPart(mdp);  
                  message.setContent(mm);  
           
                  message.saveChanges();  
                  Transport transport = s.getTransport("smtp");  
                  transport.connect(host, (null == domainUser) ? username : domainUser, password);  
                  transport.sendMessage(message, message.getAllRecipients());  
                  transport.close();  
                  return true;  
              }  
           
              public Mailer(String host, String auth, String domainUser, String username, String password) {  
                  super();  
                  this.host = host;  
                  this.auth = auth;  
                  this.domainUser = domainUser;  
                  this.username = username;  
                  this.password = password;  
              }
              /**
               * @param args
               * @throws MessagingException
               */
              public static void main(String[] args) throws MessagingException {
                  // TODO Auto-generated method stub
                  System.out.println(GetImageStr());
                  new Mailer("10.110.4.4", "false", "abc\\sysadmin", "sysadmin@abc.com", "sysadmin").send(new String[] { "G@abc.com","F@abc.com" }, null, null, "郵件中發(fā)送文字與圖片", "<h3>hello html5</h3><img src=\"data:image/png;base64,"+GetImageStr()+"\">" );
              }        
                  
               public static String GetImageStr()
                  {//將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理
                      String imgFile = "d:\\111.jpg";//待處理的圖片
                      InputStream in = null;
                      byte[] data = null;
                      //讀取圖片字節(jié)數(shù)組
                      try
                      {
                          in = new FileInputStream(imgFile);        
                          data = new byte[in.available()];
                          in.read(data);
                          in.close();
                      }
                      catch (IOException e)
                      {
                          e.printStackTrace();
                      }
                      //對字節(jié)數(shù)組Base64編碼
                      BASE64Encoder encoder = new BASE64Encoder();
                      return encoder.encode(data);//返回Base64編碼過的字節(jié)數(shù)組字符串
                  }
                  public static boolean GenerateImage(String imgStr)
                  {//對字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片
                      if (imgStr == null) //圖像數(shù)據(jù)為空
                          return false;
                      BASE64Decoder decoder = new BASE64Decoder();
                      try
                      {
                          //Base64解碼
                          byte[] b = decoder.decodeBuffer(imgStr);
                          for(int i=0;i<b.length;++i)
                          {
                              if(b[i]<0)
                              {//調(diào)整異常數(shù)據(jù)
                                  b[i]+=256;
                              }
                          }
                          //生成jpeg圖片
                          String imgFilePath = "d:\\222.jpg";//新生成的圖片
                          OutputStream out = new FileOutputStream(imgFilePath);    
                          out.write(b);
                          out.flush();
                          out.close();
                          return true;
                      }
                      catch (Exception e)
                      {
                          return false;
                      }
                  }        
          }

          廣州白馬檔口出租

          番禺大石空調(diào)維修中心

          posted on 2013-04-19 10:30 gdufo 閱讀(1331) 評論(0)  編輯  收藏

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Hibernate

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 天门市| 盐边县| 酉阳| 炎陵县| 金堂县| 深水埗区| 湖州市| 高邮市| 榕江县| 浪卡子县| 噶尔县| 达孜县| 拉萨市| 门源| 出国| 那坡县| 鹤峰县| 古浪县| 渑池县| 原平市| 阿克苏市| 丹凤县| 灯塔市| 江北区| 嘉禾县| 喀什市| 尤溪县| 津市市| 康保县| 海盐县| 平阴县| 龙川县| 会泽县| 宁蒗| 海兴县| 双江| 雷州市| 东乌珠穆沁旗| 九台市| 平乐县| 苏尼特左旗|