gpt4 book ai didi

JavaMail - 如何发送带有图像的 html 内容

转载 作者:行者123 更新时间:2023-11-30 02:17:51 26 4
gpt4 key购买 nike

我见过很多例子,他们分别提供了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>

输出:

enter image description here

最佳答案

试试这个:

// 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);

关于JavaMail - 如何发送带有图像的 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47748626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com