我已经成功地学习了如何使用javamail将图像嵌入到HTML中的教程。但是我现在正试图从模板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 = "usernamehere@gmail.com";
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("from-email@gmail.com"));
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 :(得分:0)
电子邮件客户端解释HTML代码的方式与写入HTML模板文件不同。但有一件事你可以肯定的是,一旦你得到模板,将图像的字节数组复制到src属性。您可以尝试使用内嵌图像作为浏览器解释src属性并发出另一个请求来获取数据。
让您更深入地了解这个概念。Inline Images in HTML
答案 1 :(得分:0)
当然,您需要确保文件中的数据实际上是以UTF-8编码的,而不是计算机的默认编码。如果使用所有ASCII文本对其进行测试,则无关紧要。
假设您在上面的示例代码中的字符串中有相同的文本,您可以比较两种情况(字符串,文件),以查看JavaMail发送的消息如何使用message.writeTo( new FileOutputStream(“msg.txt”));就在Transport.send调用之前或代替它。