JavaMail实现发送超文本(html)格式邮件的方法

JavaMail是一个用于处理电子邮件的Java API,可发送和接收邮件。要发送HTML格式的邮件,可以按照以下步骤进行:

JavaMail是一个用于处理电子邮件的Java API,可发送和接收邮件。要发送HTML格式的邮件,可以按照以下步骤进行:

步骤1: 导入包

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

步骤2: 设置连接属性

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

步骤3: 获取会话对象

Session session = Session.getInstance(props, new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("your_email@gmail.com", "your_password");
    }
});

步骤4: 创建邮件消息

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_email@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@gmail.com"));
message.setSubject("HTML格式邮件示例");

String htmlMessage = "<h1>这是一封HTML格式的邮件</h1><p>hello world!</p>";
message.setContent(htmlMessage, "text/html");

步骤5: 发送邮件

Transport.send(message);
System.out.println("邮件已发送");

示例1: 发送带图片的HTML邮件

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_email@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@gmail.com"));
message.setSubject("带图片的邮件");

String htmlMessage = "<h1>这是一封带图片的HTML邮件</h1>"
                   + "<p>hello <img src='https://example.com/image.jpg'> world!</p>";
message.setContent(htmlMessage, "text/html");

// 添加图片
MimeBodyPart imageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("/path/to/image.jpg");
imageBodyPart.setDataHandler(new DataHandler(fds));
imageBodyPart.setHeader("Content-ID","<image>");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(imageBodyPart);
multipart.addBodyPart(new MimeBodyPart().setContent(htmlMessage, "text/html"));
message.setContent(multipart);

示例2: 发送带附件的HTML邮件

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your_email@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@gmail.com"));
message.setSubject("带附件的邮件");

String htmlMessage = "<h1>这是一封带附件的HTML邮件</h1>"
                   + "<p>hello world!</p>";
message.setContent(htmlMessage, "text/html");

// 添加附件
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("/path/to/file.pdf");
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName("file.pdf");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(attachmentBodyPart);
multipart.addBodyPart(new MimeBodyPart().setContent(htmlMessage, "text/html"));
message.setContent(multipart);

本文标题为:JavaMail实现发送超文本(html)格式邮件的方法

基础教程推荐