![java如何发送html邮件-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码](https://zhuji.vsping.com/wp-content/uploads/2024/04/a5b783cee9bbb84274752371b4197261.jpg)
java如何发送html邮件
要使用Java发送HTML邮件,可以使用JavaMail API,以下是详细步骤:,1、添加JavaMail依赖,在项目的pom.xml文件中添加JavaMail的依赖:,2、编写Java代码,创建一个Java类,如 HtmlEmailSender.java,并编写以下代码:,注意替换 your_email@example.com、 your_password和 recipient_email@example.com为实际的发件人邮箱、密码和收件人邮箱。,3、运行Java程序,运行 HtmlEmailSender.java,如果一切正常,收件人将收到一封包含HTML内容的邮件。,,<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>,import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class HtmlEmailSender { public static void main(String[] args) { // 设置邮件服务器属性 Properties properties = new Properties(); properties.put(“mail.smtp.host”, “smtp.example.com”); properties.put(“mail.smtp.port”, “587”); properties.put(“mail.smtp.auth”, “true”); properties.put(“mail.smtp.starttls.enable”, “true”); // 创建邮件会话 Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(“your_email@example.com”, “your_password”); } }); try { // 创建邮件消息 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(“your_email@example.com”)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(“recipient_email@example.com”)); message.setSubject(“HTML邮件示例”); // 设置邮件内容为HTML格式 message.setContent(“<h1>欢迎使用Java发送HTML邮件!</h1><table border=’1′><tr><th>姓名</th><th>年龄</th></tr><tr><td>张三</td><td>30</td></tr></table>”, “text/html;charset=UTF8”); // 发送邮件 Transport.send(message); System.out.println(“邮件已成功发送!”); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } },