使用JavaMail发送带有内联图像的HTML电子邮件-图像加载缓慢?

使用JavaMail发送带有内联图像的HTML电子邮件-图像加载缓慢?,java,email,jakarta-mail,Java,Email,Jakarta Mail,当我使用JavaMail发送一封HTML电子邮件,并在HTML中包含一个内联图像时,在Gmail或Yahoo中读取图像需要2-3秒才能加载 我使用的图像是一个小的.png,大小约为200字节 代码如下: import java.io.File; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.

当我使用JavaMail发送一封HTML电子邮件,并在HTML中包含一个内联图像时,在Gmail或Yahoo中读取图像需要2-3秒才能加载

我使用的图像是一个小的.png,大小约为200字节

代码如下:

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class EmailTest 
{   
    static String from = "Your_Gmail_Account_Name";  // (e.g., "name" if your account is "name@gmail.com"
    static String password = "Your_Gmail_Password";    
    static String to = "Send_Here@gmail.com";
    static String subject = "test";
    static String body = "<h1>The image in this e-mail is slow to load.</h1><img src=\"cid:my-image\">"; 
    static String host = "smtp.gmail.com";

    public static void main(String[] args)
    {
        Properties properties = System.getProperties();

        properties.put("mail.smtp.starttls.enable", "true");  
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.user", from);
        properties.put("mail.smtp.password", password);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(properties);

        MimeMessage message = new MimeMessage(session);

        try 
        {
            message.setFrom(new InternetAddress(from));    
            InternetAddress toAddress = new InternetAddress(to);
            message.addRecipient(Message.RecipientType.TO, toAddress);
            message.setSubject(subject);

            Multipart multipart = new MimeMultipart("related");

            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(body, "text/html");
            multipart.addBodyPart(htmlPart);

            MimeBodyPart imagePart = new MimeBodyPart();
            DataSource ds = new FileDataSource(new File("src\\icon.png"));
            imagePart.setDataHandler(new DataHandler(ds));
            imagePart.setHeader("Content-ID", "<my-image>");
            multipart.addBodyPart(imagePart);

            message.setContent(multipart);

            Transport transport = session.getTransport("smtp");

            try 
            {
                transport.connect(host, from, password);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();

                System.out.println("e-mail sent.");
            } 
            catch (MessagingException e) 
            {
                e.printStackTrace();
            }
        }
        catch (AddressException e) 
        {
            e.printStackTrace();
        }
        catch (MessagingException e) 
        {
            e.printStackTrace();
        }
    } 
}
导入java.io.File;
导入java.util.Properties;
导入javax.activation.DataHandler;
导入javax.activation.DataSource;
导入javax.activation.FileDataSource;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.AddressException;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.MimeBodyPart;
导入javax.mail.internet.mimessage;
导入javax.mail.internet.MimeMultipart;
公共类电子邮件测试
{   
静态字符串from=“Your_Gmail_Account_Name”;/(例如,如果您的帐户为”name@gmail.com"
静态字符串password=“您的邮箱密码”;
要发送的静态字符串_Here@gmail.com";
静态字符串subject=“test”;
static String body=“此电子邮件中的图像加载缓慢。”;
静态字符串host=“smtp.gmail.com”;
公共静态void main(字符串[]args)
{
Properties=System.getProperties();
properties.put(“mail.smtp.starttls.enable”、“true”);
properties.put(“mail.smtp.host”,host);
properties.put(“mail.smtp.user”,from);
properties.put(“mail.smtp.password”,password);
properties.put(“mail.smtp.port”,“587”);
properties.put(“mail.smtp.auth”、“true”);
Session Session=Session.getDefaultInstance(属性);
MimeMessage message=新MimeMessage(会话);
尝试
{
message.setFrom(新的InternetAddress(from));
InternetAddress toAddress=新的InternetAddress(to);
message.addRecipient(message.RecipientType.TO,toAddress);
message.setSubject(主题);
Multipart Multipart=新的mimemmultipart(“相关”);
MimeBodyPart htmlPart=新的MimeBodyPart();
setContent(body,“text/html”);
multipart.addBodyPart(htmlPart);
MimeBodyPart imagePart=新的MimeBodyPart();
DataSource ds=新文件DataSource(新文件(“src\\icon.png”);
setDataHandler(新的DataHandler(ds));
setHeader(“内容ID”,即“”);
multipart.addBodyPart(imagePart);
message.setContent(多部分);
传输=session.getTransport(“smtp”);
尝试
{
传输连接(主机、发件人、密码);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
System.out.println(“已发送电子邮件”);
} 
捕获(消息异常e)
{
e、 printStackTrace();
}
}
捕获(地址e)
{
e、 printStackTrace();
}
捕获(消息异常e)
{
e、 printStackTrace();
}
} 
}
有人知道为什么图像加载如此缓慢吗

更新:

在Thunderbird中阅读上述代码生成的电子邮件时,图像甚至不会加载,而只会显示为附件

但如果我删除这一行:

imagePart.setHeader("Content-ID", "<my-image>");
imagePart.setHeader(“内容ID”,即“”);
并将其替换为以下两行:

imagePart.addHeader("Content-ID", "<my-image>");
imagePart.addHeader("Content-Type", "image/png");
imagePart.addHeader(“内容ID”,即“”);
addHeader(“内容类型”、“图像/png”);
然后图像实际加载到Thunderbird中,并在瞬间加载


但是,在Gmail和Yahoo中加载该图像的速度仍然很慢。

也许他们正在运行病毒扫描程序?试试Thunderbird。

正如Bill和另一张海报所提到的,问题不是JavaMail的问题,而是Gmail和Yahoo扫描电子邮件中发送的嵌入式内联图像的问题

解决方案是不在HTML电子邮件中嵌入任何具有内容ID的内联图像,而是在HTML中的img标记中放置一个src属性,该属性引用位于远程主机上的图像

这将使图像在HTML电子邮件中发送时能够快速加载

换言之:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class EmailTest 
{   
    static String from = "Your_Gmail_Account_Name";  // Your Gmail account name (e.g., "name" if your account is "name@gmail.com"
    static String password = "Your_Gmail_Password";   // Your Gmail password
    static String to = "Send_Here@gmail.com";
    static String subject = "test";
    static String body = "<html><body><h1>The image in this e-mail loads very fast.</h1><img src=\"http://www.your_host.com/path/to/image/icon.png\"></body></html>"; 

    static String host = "smtp.gmail.com";

    public static void main(String[] args)
    {
        Properties properties = System.getProperties();

        properties.put("mail.smtp.starttls.enable", "true");  
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.user", from);
        properties.put("mail.smtp.password", password);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(properties);

        MimeMessage message = new MimeMessage(session);

        try 
        {
            message.setFrom(new InternetAddress(from));    
            InternetAddress toAddress = new InternetAddress(to);
            message.addRecipient(Message.RecipientType.TO, toAddress);
            message.setSubject(subject);    
            message.setContent(body, "text/html");

            Transport transport = session.getTransport("smtp");

            try 
            {
                transport.connect(host, from, password);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();

                System.out.println("e-mail sent.");
            } 
            catch (MessagingException e) 
            {
                e.printStackTrace();
            }
        }
        catch (AddressException e) 
        {
            e.printStackTrace();
        }
        catch (MessagingException e) 
        {
            e.printStackTrace();
        }
    } 
}
import java.util.Properties;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.AddressException;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
公共类电子邮件测试
{   
静态字符串from=“Your\u Gmail\u Account\u Name”;//您的Gmail帐户名(例如,如果您的帐户为”name@gmail.com"
静态字符串password=“Your\u Gmail\u password”//您的Gmail密码
要发送的静态字符串_Here@gmail.com";
静态字符串subject=“test”;
static String body=“此电子邮件中的图像加载速度非常快。”;
静态字符串host=“smtp.gmail.com”;
公共静态void main(字符串[]args)
{
Properties=System.getProperties();
properties.put(“mail.smtp.starttls.enable”、“true”);
properties.put(“mail.smtp.host”,host);
properties.put(“mail.smtp.user”,from);
properties.put(“mail.smtp.password”,password);
properties.put(“mail.smtp.port”,“587”);
properties.put(“mail.smtp.auth”、“true”);
Session Session=Session.getDefaultInstance(属性);
MimeMessage message=新MimeMessage(会话);
尝试
{
message.setFrom(新的InternetAddress(from));
InternetAddress toAddress=新的InternetAddress(to);
message.addRecipient(message.RecipientType.TO,toAddress);
message.setSubject(主题);
message.setC