有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我见过很多例子,其中他们分别提供了img文件路径&;在html内容中已经提到了这一点。而我想阅读HTML(其中包含图像引用)内容&;作为邮件发送。当我传递html内容时,图像不会呈现

我引用了这个网站:http://www.rgagnon.com/javadetails/java-0504.html

Java类:SimpleEmail

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文件:测试。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


共 (2) 个答案

  1. # 1 楼答案

    试试这个:

    // 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);
    
  2. # 2 楼答案

    邮件中的图像URL是相对的,因此邮件客户端无法从复制HTML文件的网站加载图像URL。您需要使用绝对URL。正如您用作参考的页面已经解释过的,这也很可能不起作用,因为由于垃圾邮件发送者的滥用,大多数邮件客户端不再加载外部图像。因此,您应该按照该页面中描述的第三种方法,将图像作为附件添加到邮件中,并使用URLcid:unique-content-id在HTML页面中引用它。我跳过提供一个示例,因为您链接的页面已经包含一个应该可以工作的页面