[英]JavaMail - How to send html content with image
我看过很多例子,他们分别提供了img文件路径并在html内容中引用了它,而我想阅读HTML(带有图像引用)的内容并作为邮件发送。当我传递html内容时,图像不呈现。
我已推荐此网站: http : //www.rgagnon.com/javadetails/java-0504.html
Java类: SimpleMail
import javax.mail.*;
import javax.mail.internet.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Properties;
class SimpleMail {
public static void main(String[] args) throws Exception{
System.out.println("Sending mail...");
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.example.com");
props.setProperty("mail.user", "user@example.com");
props.setProperty("mail.password", "password");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("HTML mail with images");
message.setFrom(new InternetAddress("me@sender.com"));
//C:\Users\sv\Desktop\test.html
BufferedReader br = null;
FileReader fr = null;
//br = new BufferedReader(new FileReader(FILENAME));
fr = new FileReader("C:\\Desktop\\test.html");
br = new BufferedReader(fr);
String sCurrentLine;
String outCome="";
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
outCome+="\n"+sCurrentLine;
}
// message.setContent(outCome, "text/html:charset=utf-8");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("user1@example.com"));
transport.connect();
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(outCome, "text/html");
htmlPart.setHeader("Content-Type", "text/html" );
mp.addBodyPart(htmlPart);
message.setContent(mp);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
HTML文件: test.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<img src="tr.png" alt="Smiley face" height="42" width="42">
<div class="container">
<h2>Dropdowns</h2>
<p>The .dropdown class is used to indicate a dropdown menu.</p>
<p>Use the .dropdown-menu class to actually build the dropdown menu.</p>
<p>To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and data-toggle="dropdown".</p>
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="https://www.w3schools.com/html/default.asp">HTML</a></li>
<li><a href="https://www.w3schools.com/css/default.asp">CSS</a></li>
<li><a href="https://www.w3schools.com/js/default.asp">JavaScript</a></li>
</ul>
</div>
</div>
</body>
</html>
输出:
尝试这个:
// Your mail has 2 parts, the BODY(html) and the EMBEDDED image
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
// Add it
multipart.addBodyPart(messageBodyPart);
// Second part (EMBEDDED image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("/path/to/your/image/tr.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
// add the image to the multipart
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
// Send message
Transport.send(message);
邮件中的图片网址是相对的,因此邮件客户端无法从您复制HTML文件的网站上加载它。 您需要使用绝对URL。 正如您用作参考的页面已经解释的那样,这也很可能行不通,因为大多数邮件客户端由于垃圾邮件发送者的滥用而不再加载外部图像。 因此,您应该按照该页面中描述的第三种方法,将图像添加为邮件的附件,并使用URL cid:unique-content-id
在HTML页面内对其进行引用。 我跳过了一个示例,因为您链接的页面已经包含一个应该正常工作的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.