在电子邮件中发送内联图片(Send inline image in email)
优质
小牛编辑
137浏览
2023-12-01
以下是使用内嵌图像从您的计算机发送HTML电子邮件的示例。 在这里,我们使用了JangoSMPT服务器,通过该服务器将电子邮件发送到我们的目标电子邮件地址。 环境设置章节中介绍了该设置 。
要发送包含内嵌图像的电子邮件,请执行以下步骤:
参加一个会议
创建一个默认的MimeMessage对象,并在消息中设置From, To, Subject 。
创建一个MimeMultipart对象。
在我们的示例中,我们将在电子邮件中包含HTML部分和图像。 首先创建HTML内容并将其设置在multipart对象中:
// 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);
接下来,通过创建Datahandler来添加图像,如下所示:
// second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource( "/home/manisha/javamail-mini-logo.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID", "<image>");
接下来在消息中设置multipart,如下所示:
message.setContent(multipart);
使用Transport对象发送消息。
创建Java类 (Create Java Class)
创建一个java类文件SendInlineImagesInEmail ,其内容如下:
package cn.xnip;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendInlineImagesInEmail {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "destinationemail@gmail.com";
// Sender's email ID needs to be mentioned
String from = "fromemail@gmail.com";
final String username = "manishaspatil";//change accordingly
final String password = "******";//change accordingly
// Assuming you are sending email through relay.jangosmtp.net
String host = "relay.jangosmtp.net";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// This mail has 2 part, the BODY 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 (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(
"/home/manisha/javamail-mini-logo.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
由于我们使用主机提供商JangoSMTP提供的SMTP服务器,我们需要验证用户名和密码。 javax.mail.PasswordAuthentication类用于验证密码。
编译和运行 (Compile and Run)
现在我们的课已经准备好了,让我们编译上面的类。 我已将类SendInlineImagesInEmail.java保存到目录: /home/manisha/JavaMailAPIExercise 。 我们需要在类路径中使用jars javax.mail.jar和activation.jar 。 执行以下命令从命令提示符编译类(两个jar放在/ home/manisha /目录中):
javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendInlineImagesInEmail.java
现在编译了类,执行以下命令来运行:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendInlineImagesInEmail
验证输出 (Verify Output)
您应该在命令控制台上看到以下消息:
Sent message successfully....
当我通过JangoSMTP向我的Gmail地址发送电子邮件时,我的gmail帐户收件箱中会收到以下邮件: