gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:12 25 4
gpt4 key购买 nike

当我使用 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();
}
}
}

有人知道为什么图片加载这么慢吗?

更新:

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

但是如果我删除这一行:

imagePart.setHeader("Content-ID", "<my-image>");

并将其替换为以下两行:

imagePart.addHeader("Content-ID", "<my-image>");
imagePart.addHeader("Content-Type", "image/png");

然后图像实际上在 Thunderbird 中加载,并且立即加载。

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

最佳答案

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

关于java - 使用 JavaMail 发送带有内联图像的 HTML 电子邮件 - 图像加载速度慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446483/

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