2012-04-20 86 views
0

我已经成功地遵循了如何使用javamail将图像嵌入到HTML的教程。不过,我现在正尝试从模板html文件中读取,然后在发送之前将图像嵌入到此文件中。嵌入的图像不显示在来自Javamail的电子邮件html模板

我确信代码是正确的嵌入图像,当我使用:

bodyPart.setContent("<html><body><h2>A title</h2>Some text in here<br/>" + 
       "<img src=\"cid:the-img-1\"/><br/> some more text<img src=\"cid:the-img-1\"/></body></html>", "text/html"); 

的图像显示效果细腻。然而,当我使用一个文件中读取:

readHTMLToString reader = new readHTMLToString(); 
String str = reader.readHTML(); 
bodyPart.setContent(str, "text/html"); 

的图像不显示当电子邮件发送。

我读的HTML字符串代码如下:

public class readHTMLToString { 
static String finalFile; 

public static String readHTML() throws IOException{ 

//intilize an InputStream 
    File htmlfile = new File("C:/temp/basictest.html"); 
    System.out.println(htmlfile.exists()); 
try { 
    FileInputStream fin = new FileInputStream(htmlfile); 

    byte[] buffer= new byte[(int)htmlfile.length()]; 
new DataInputStream(fin).readFully(buffer); 
    fin.close(); 
    String s = new String(buffer, "UTF-8"); 
    finalFile = s; 
} 
catch(FileNotFoundException e) 
{ 
    System.out.println("File not found" + e); 
} 
catch(IOException ioe) 
{ 
    System.out.println("Exception while reading the file " + ioe); 
} 
return finalFile; 
    } 
} 

我发送电子邮件完整的类如下:

package com.bcs.test; 

import java.io.IOException; 
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.Multipart; 
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; 

public class SendEmail { 

public static void main(String[] args) throws IOException { 

    final String username = "[email protected]"; 
    final String password = "passwordhere"; 

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


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

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("recepientemailhere")); 
     message.setSubject("Testing Subject"); 

     //SET MESSAGE AS HTML 
     MimeMultipart multipart = new MimeMultipart("related"); 

     // Create bodypart. 
     BodyPart bodyPart = new MimeBodyPart(); 

     // Create the HTML with link to image CID. 
     // Prefix the link with "cid:". 

     //bodyPart.setContent("<html><body><h2>A title</h2>Some text in here<br/>" + 
       // "<img src=\"cid:the-img-1\"/><br/> some more text<img src=\"cid:the-img-1\"/></body></html>", "text/html"); 
     readHTMLToString reader = new readHTMLToString(); 
     String str = reader.readHTML(); 

     // Set the MIME-type to HTML. 
     bodyPart.setContent(str, "text/html"); 

     // Add the HTML bodypart to the multipart. 
     multipart.addBodyPart(bodyPart); 

     // Create another bodypart to include the image attachment. 
     BodyPart imgPart = new MimeBodyPart(); 

     // Read image from file system. 
     DataSource ds = new FileDataSource("C:\\temp\\dice.png"); 
     imgPart.setDataHandler(new DataHandler(ds)); 

     // Set the content-ID of the image attachment. 
     // Enclose the image CID with the lesser and greater signs. 
     imgPart.setDisposition(MimeBodyPart.INLINE); 
     imgPart.setHeader("Content-ID","the-img-1"); 
     //bodyPart.setHeader("Content-ID", "<image_cid>"); 

     // Add image attachment to multipart. 
     multipart.addBodyPart(imgPart); 

     // Add multipart content to message. 
     message.setContent(multipart); 



     //message.setText("Dear Mail Crawler," 
     // + "\n\n No spam to my email, please!"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 
} 
} 

伊夫通过关于这一点,但确实很多答案阅读不知道为什么会发生这种情况。我以为这是因为我的HTML文件的问题,但我创建了一个非常基本的使用相同的内容作为上面的初始setContent代码和图片不出现在这个基本的例子。

与读入字节数组有关吗?

任何帮助非常感谢。

感谢

回答

0

方式的电子邮件客户端解析HTML代码是从写HTML模板文件不同。但有一件事你可以肯定的是,一旦你得到模板,将图像的字节数组复制到src属性中。您可以尝试使用内嵌图像作为浏览器inteprets src属性,并发出另一个请求来获取数据。

给你更多的概念洞察力。 Inline Images in HTML

+0

谢谢你的快速回复,但我不完全确定我完全理解你的答案。为了澄清,如果我只是从html文件读取文本到一个字符串,为什么它会被解释不同?你可能举个例子,将图像的字节数组复制到src属性中。 Thankyou – EHarpham 2012-04-20 16:03:59

0

当然,您需要确保文件中的数据实际上是使用UTF-8编码的,而不是使用计算机的默认编码。如果使用全部ASCII文本进行测试,则无关紧要。

假设您在上面的示例代码中的字符串中包含文件中的相同文本,则可以比较两种情况(字符串,文件)以查看JavaMail将如何使用message.writeTo发送消息(新的FileOutputStream(“msg.txt”));在Transport.send调用之前或之前。

+0

你好比尔感谢您的建议。 html模板中的编码确实是UTF-8。当我将该消息写入.txt时,html看起来是完全相同的,但后面是图像的base64代码,之后是消息属性:'------ = _ Part _0_21357269.1334981436511 Content-Type:text/HTML; charset = us-ascii Content-Transfer-Encoding:7bit' – EHarpham 2012-04-20 21:17:56

+0

我不确定你在说什么。只是为了检查,你在两种情况下都使用相同的代码来构造消息,只有**的区别在于你如何设置主消息正文部分的内容,对吗?如果您可以发布这两个版本的msg.txt文件,可能会有所帮助。 – 2012-04-21 01:59:29

相关问题