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)格式邮件的方法
基础教程推荐
猜你喜欢
- 关于protected修饰符详解-源于Cloneable接口 2023-08-10
- springboot+vue实现登录功能的最新方法整理 2023-01-02
- SpringBoot请求参数相关注解说明小结 2022-11-08
- SpringMVC深入讲解文件的上传下载实现 2023-01-23
- CompletableFuture 异步编排示例详解 2023-05-09
- JAVA学习进阶篇之时间与日期相关类 2023-05-08
- java – Hibernate,SQL和递归关联 2023-11-03
- Spring实现泛型注入的示例详解 2023-02-20
- springboot各种下载文件的方式汇总 2023-06-11
- ThreadLocal原理介绍及应用场景 2023-08-10