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)格式邮件的方法
基础教程推荐
猜你喜欢
- Java并发程序刺客之假共享的原理及复现 2023-03-30
- Java作为与MySQL交互的cron脚本与使用PHP 2023-10-29
- SpringBoot 转发请求至指定页面的操作方法 2023-07-01
- java实现后台返回base64图形编码 2023-01-08
- 编写线程安全的JSP程序 2023-12-16
- 基于SSM+Shiro+Bootstrap实现用户权限管理系统 2023-08-07
- Spring Security安全框架之记住我功能 2022-10-31
- window.location和document.location的区别分析 2023-12-07
- JVM系列第6讲:Java 虚拟机内存结构 2023-09-01
- Spring IOC 常用注解与使用实例详解 2022-12-02
