2012-03-23 3 views
1

로고와 사용자의 서명 이미지가 포함 된 html 이메일을 보내려고합니다. 아파치 커먼즈 메일을 사용하고 있습니다. Apache의 사이트 자습서를 따라 웹상에서 다양한 접근 방식을 시도했지만 이미지를 포함 할 수는 없습니다. 인트라넷 응용 프로그램이고 외부에서 액세스를 차단하는 단일 사인온 뒤에 있으므로 임베드 된 이미지를 가져 오기 위해 URL을 사용할 수 없다는 점을 말씀드립니다. 게다가 진정한 HTML이 아니라 애플리케이션이 템플릿으로 사용하는 XML입니다. 아래 xml-html을 추가했습니다. (노트 텍스트가 제대로 표시되고, 이미지에 문제가 있습니다.) 코드를 사용하여 이미지를 삽입합니다. 내가하는 모든 실수를 지적하거나 내 솔루션을 제안 할 수 있습니다. 제발 제발?이미지가 포함 된 이메일을 보내려면

결과 HTML/XML : 메일 보낼

<?xml version="1.0" encoding="UTF-8"?><div style="margin-top: 20px; font-size: small;"> 
<br/> 
    <div class="auto-style1"> 
     <div style="text-align: left;"> 
... 
     <div class="MsoNormal" style="text-align: right; padding-right: 100px; font-family: arial black,sans-serif;"> 
     <img id="signature" src="cid:jrvoirylpp"/> 
     </div> 
... 

내 코드 :

  HtmlEmail htmlMail = new HtmlEmail(); 
      initMail(htmlMail);//set commons parameters (host,port,... 
      htmlMail.setContent(htmlCorpoMessaggio, "text/html"); 
      //i'm trying to retrieve the raw byte array from my app resources 
      InputStream is = this.getClass().getResourceAsStream(
        String.format("%s%s", 
          Configurator.getString("Template.resources"), 
          Configurator.getString("Template.firma"))); 
      byte[] image = IOUtils.toByteArray(is); 
      //can't send an url i'm trying to truly embed the image inside the mail message 
      DataSource ds = new ByteArrayDataSource(image, "image/png"); 
      String cid = htmlMail.embed(ds, "signature"); 
      //i need to replace the src="an app path" to cid 
      Document doc = XmlHelper.loadXMLFromString(htmlCorpoMessaggio); 
      NodeList nodeList = doc.getElementsByTagName("img"); 
      Node currentNode = null; 
      for(int i = 0; i < nodeList.getLength(); i++) 
      { 
       currentNode = nodeList.item(i); 
      } 
      NamedNodeMap nodiAttributo = currentNode.getAttributes(); 
      for(int i= 0 ; i < nodiAttributo.getLength() ; i++) 
      { 
       Node n = nodiAttributo.item(i); 
       if(n.getNodeName().equals("src")) 
        n.setNodeValue("cid:" + cid); 
      } 
      htmlCorpoMessaggio = XmlHelper.getStringFromDocument(doc);   
      for(MailAttachment allegato : allegati) 
      { 
       //la stringa vuota rappresenta la descrizione dell'allegato 
       htmlMail.attach(allegato.getDataSource(), 
         allegato.getFilename(),"",EmailAttachment.ATTACHMENT); 
      } 
      htmlMail.send(); 

답변

1
내 대답 밤은 정말 자바 관련이 있기 때문에 대답 할 것 않네

하지만를 ...

Base64 인코더를 사용하여 이메일에 이미지를 포함시킬 수 있습니다. 대부분의 클라이언트는 쇼 인코딩 된 이미지 http://www.campaignmonitor.com/blog/post/1761/embedding-images-in-email/

해달라고 때문에 나는이 생각의 againt 제안 http://www.motobit.com/util/base64-decoder-encoder.asp

나는 당신의 최선의 방법은 서버에 호스팅 된 이미지에 일반 HTML 링크를 게시 할 것입니다 생각합니다.

죄송합니다.이 답변을 듣고 싶지 않다면 죄송합니다.

+0

@ 샘 워렌 음, 어쨌든, 적어도 누군가가 대답했습니다 :) 그리고 나는 새로운 것을 배웠습니다. 웹 앱은 웹에서 인트라넷 내부에서 접근 할 수 없으며 어쨌든 모든 리소스가 인증되지 않은 요청을 차단하는 sso 시스템 뒤에 있습니다. –

0

XmlHelper의 번거 로움을 겪지 마십시오. 아마 그것이 작동하지 않는 원인 일 것입니다.
그냥이 src="CIDSIGNATURE" 같은 이메일에있는 img 태그를 변경 한 후 다음을 수행

HtmlEmail htmlMail = new HtmlEmail(); 
initMail(htmlMail);//set commons parameters (host,port,... 
//i'm trying to retrieve the raw byte array from my app resources 
InputStream is = this.getClass().getResourceAsStream(
     String.format("%s%s", 
       Configurator.getString("Template.resources"), 
       Configurator.getString("Template.firma"))); 
byte[] image = IOUtils.toByteArray(is); 
//can't send an url i'm trying to truly embed the image inside the mail message 
DataSource ds = new ByteArrayDataSource(image, "image/png"); 
String cid = htmlMail.embed(ds, "signature"); 
htmlCorpoMessaggio = htmlCorpoMessaggio.replace("CIDSIGNATURE", "cid:" + cid); 
htmlMail.setHtmlMsg(htmlCorpoMessaggio); 
htmlMail.send(); 

공지 사항 나는 처음에 htmlMail.setContent를 제거했습니다.
잘 작동해야합니다. 그것은 나를 위해 않습니다 :

관련 문제