2011-03-21 40 views
2

我想使用Spring的郵件實現發送電子郵件,並使用velocity模板替換html文件的內容。到目前爲止,它的運行效果很好,但是現在當我試圖向郵件中添加第二個內聯圖像時,我會遇到麻煩。JavaMailSenderImpl發送內嵌照片

我Velocity模板是這個:

<html> 
<head> 
    <title>Ndeveloper publishing</title> 
</head> 
<body> 
    <div id="header" style="background-color: #eeeeee"> 
     <div align="center"> 
      <p><em>Header1</em></p> 
     </div> 
    </div> 
    <div id="content"> 
     <div id="paragraph1"> 
      <img src='cid:${photo1}' width="200px" height="200px" style="display: block;float: left; margin: 0em 1em 1em 0em "/> 
      <p>${paragraph1} 
      </p> 
     </div> 
     <div id="paragraph2> 
      <img src='cid:${photo2}' width="200px" height="200px" style="display: block;float: right; margin: 0em 0em 1em 1em "/> 
      <p>${paragraph2} 
      </p> 
     </div> 
    </div> 
    <div id="footer" style="background-color: #eeeeee"> 
     <div align="center"> 
      <p><em>Footer1</em></p> 
     </div> 
    </div> 
</body> 

現在,我使用發送郵件的代碼如下所示:

@SuppressWarnings("unchecked") 
public void sendTemplateMail(VelocityMailMessage message) { 
    Connection connection = null; 
    Session session = null; 


    try { 
     connection = connectionFactory.createConnection(); 
     session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 

     Velocity.init(initializeVelocityProperties()); 
     VelocityContext velocityContext = new VelocityContext(); 

     HashMap<String, Object> parameterMap=message.getReplaceableParameters(); 
     HashMap<String, Attachment> attachmentMap=message.getAttachList(); 

     //${paragraph1} and ${paragraph2} are replaced here 
     for (String key : parameterMap.keySet()) { 
      velocityContext.put(key, parameterMap.get(key)); 
     } 
     //Here the inline photos identifiers should be replaced ${photo1} and ${photo2} 
     int k=1; 
     for (String key: attachmentMap.keySet()) 
     { 
      //INLINE_PHOTO_PREFIX has a value of "photo"    
      velocityContext.put(Constants.INLINE_PHOTO_PREFIX+k, attachmentMap.get(key).getIdentifier()); 
      k++; 
     } 

     StringWriter text = new StringWriter(); 
     Velocity.mergeTemplate(message.getTemplateName(), "UTF-8", velocityContext, text); 

     List<String> emailList = message.getTo(); 

     ArrayList<String> emails = new ArrayList<String>(); 
     for (Iterator<String> iterator = emailList.iterator(); iterator 
       .hasNext();) { 
      String[] tmp = null; 
      String[] tmp1 = null; 
      int i = 0; 
      int j = 0; 
      String name = (String) iterator.next(); 
      tmp = name.split(";"); 
      while (i < tmp.length) { 
       tmp1 = tmp[i].split(","); 
       i++; 
       j = 0; 
       while (j < tmp1.length) { 
        emails.add(tmp1[j]); 
        j++; 
       } 
      } 

     } 
     if (!emails.isEmpty()) { 
      emailList = emails; 
     } 

     JavaMailSenderImpl sender = new JavaMailSenderImpl(); 
     MimeMessage mimeMessage = sender.createMimeMessage(); 
     String[] toArray = new String[emailList.size()]; 
     int i = 0; 
     for (String to : emailList) { 
      toArray[i] = to; 
      i++; 
     } 


      MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); 

      helper.setText(text.toString(), true); 
      helper.setTo(toArray); 
      helper.setFrom(message.getFrom(), "Portal"); 
      helper.setReplyTo(message.getFrom()); 
      helper.setSubject(message.getSubject()); 
      if (message.getAttachList() != null) { 
       if (!(message.getAttachList().isEmpty())) { 
        Set<String> keys = message.getAttachList().keySet(); 
        for (String string : keys) { 
          Attachment at=message.getAttachList().get(string); 
          if(at.isInline()){ 
           helper.addInline(at.getIdentifier(), at.getAttachFile()); 
          }else{ 
           helper.addAttachment(string, message.getAttachList() 
           .get(string).getAttachFile()); 
          } 
        } 
       } 
      } 
      sender.setHost(parameterServiceLocal.parameterByName("SMTP HOST") 
        .getValue()); 
      sender.setUsername(parameterServiceLocal.parameterByName("SMTP USER").getValue()); 
      sender.setPassword(parameterServiceLocal.parameterByName("SMTP PASSWORD").getValue()); 
      Properties p = new Properties(); 

      p.put("mail.smtp.starttls.enable","true"); 
      p.put("mail.smtp.auth", "true"); 
      sender.setJavaMailProperties(p); 
      sender.send(mimeMessage); 

    } catch (VelocityException e) { 
     e.printStackTrace(); 
    } catch (MessagingException e) { 
     e.getMessage(); 
    } 
    catch (JMSException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (session != null && session != null) { 
      try { 
       session.close(); 
       connection.close(); 
      } catch (JMSException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

哪裏Constants.INLINE_PHOTO_PREFIX是簡單的字符串「照片」用於替換vecloity模板中的值。

問題是,當您檢查發送到收件箱的郵件時,它只會顯示$ {photo1}符號所在的第一張照片。我已經檢查並且所有參數都達到

if(at.isInline()){ 
      helper.addInline(at.getIdentifier(), at.getAttachFile()); 
} 

是正確的,即使速度模板被正確修改。那麼這會失敗的原因是什麼?非常感謝。

+1

該代碼很難閱讀。你需要把它分解成更小的方法,以便我們理解它。先從郵件中分離速度。 – skaffman 2011-03-21 17:32:02

回答

1

是的,謝謝你的建議。後來發現問題....只是這部分

<div id="paragraph2> 
     <img src='cid:${photo2}' width="200px" height="200px" style="display: block;float: right; margin: 0em 0em 1em 1em "/> 
     <p>${paragraph2} 

因爲我從來沒有關閉報價圖像從未顯示。我的錯,非常抱歉,再次感謝您的回覆。