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)格式邮件的方法


基础教程推荐
猜你喜欢
- dubbo自定义异常的完整步骤与测试 2023-01-02
- springBoot项目集成quartz开发定时任务案例及注意事项 2023-01-13
- Spring5新功能@Nullable注解及函数式注册对象 2022-11-28
- Spring Boot实现功能的统一详解 2023-01-18
- jsp遍历文件夹下的文件的代码 2023-12-22
- 利用Java实现天气预报播报功能 2023-01-24
- java线程池中线程数量到底是几 2023-03-22
- java内部类的最详细详解 2023-01-09
- Springboot详解底层启动过程 2023-02-27
- swagger2和knife4j的详细使用教程(入门级) 2023-06-01