当我使用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中加载图像的速度仍然很慢。
答案 0 :(得分:1)
也许他们正在运行病毒扫描程序?试试Thunderbird。
答案 1 :(得分:0)
正如比尔和另一张海报所提到的,问题不是JavaMail的问题,但似乎是Gmail和雅虎扫描电子邮件中发送的嵌入式内嵌图像的问题。
解决方案是不在带有Content-ID的HTML电子邮件中嵌入任何内嵌图像,而是在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();
}
}
}