java mail 发送html_JavaMail发送HTML邮件

package com.what21.network.mail;

import java.util.Date;

import java.util.Properties;

import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

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 {

/**

* @param host

* @param port

* @param userName

* @param password

* @param toAddress

* @param subject

* @param message

* @throws AddressException

* @throws MessagingException

*/

public void sendHtmlEmail(String host, String port,

final String userName, final String password, String toAddress,

String subject, String message) throws AddressException,

MessagingException {

// sets SMTP server properties

Properties properties = new Properties();

properties.put("mail.smtp.host", host);

properties.put("mail.smtp.port", port);

properties.put("mail.smtp.auth", "true");

properties.put("mail.smtp.starttls.enable", "true");

// creates a new session with an authenticator

Authenticator auth = new Authenticator() {

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName, password);

}

};

Session session = Session.getInstance(properties, auth);

// creates a new e-mail message

Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(userName));

InternetAddress[] toAddresses = { new InternetAddress(toAddress) };

msg.setRecipients(Message.RecipientType.TO, toAddresses);

msg.setSubject(subject);

msg.setSentDate(new Date());

// set plain text message

msg.setContent(message, "text/html");

// sends the e-mail

Transport.send(msg);

}

/**

* Test the send html e-mail method

*

*/

public static void main(String[] args) {

// SMTP server information

String host = "smtp.gmail.com";

String port = "587";

String mailFrom = "your-email-address";

String password = "your-email-password";

// outgoing message information

String mailTo = "your-friend-email-address";

String subject = "Hello my friend";

// message contains HTML markups

String message = "Greetings!
";

message += "Wish you a nice day!
";

message += "Duke";

HtmlEmailSender mailer = new HtmlEmailSender();

try {

mailer.sendHtmlEmail(host, port, mailFrom, password, mailTo,

subject, message);

System.out.println("Email sent.");

} catch (Exception ex) {

System.out.println("Failed to sent email.");

ex.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当需要在邮件正文中嵌入图片时,可以使用HTML格式的邮件,并在HTML中嵌入图片的URL。下面是使用JavaMail发送包含图片的HTML格式邮件的详细步骤: 1. 导入JavaMailjavax.activation库。 2. 创建一个MimeMessage对象。 3. 设置邮件的基本信息,包括发件人、收件人、主题等。 4. 创建一个Multipart对象,用于组合邮件正文和图片。 5. 创建一个HTML格式的邮件正文。 6. 创建一个MimeBodyPart对象,用于包装图片。 7. 将图片附件添加到MimeBodyPart对象中。 8. 将MimeBodyPart对象添加到Multipart对象中。 9. 将Multipart对象设置为邮件内容。 10. 发送邮件。 下面是一份示例代码,可以参考: ```java import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class SendMailWithImage { public static void main(String[] args) { // 发件人电子邮箱 String from = "from@example.com"; // 收件人电子邮箱 String to = "to@example.com"; // 指定发送邮件的主机为 smtp.qq.com String host = "smtp.qq.com"; //QQ 邮件服务器 // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); // 获取默认session对象 Session session = Session.getDefaultInstance(properties, new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("你的QQ账号", "你的QQ邮箱授权码"); //发件人邮件用户名、密码 } }); try{ // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("包含图片的邮件"); // 创建消息部分 BodyPart messageBodyPart = new MimeBodyPart(); // 消息 String messageText = "<h1>这是一封包含图片的HTML格式邮件!</h1><br/><img src=\"cid:image\">"; messageBodyPart.setContent(messageText, "text/html"); // 创建多重消息 Multipart multipart = new MimeMultipart(); // 设置文本消息部分 multipart.addBodyPart(messageBodyPart); // 附件部分 messageBodyPart = new MimeBodyPart(); String filename = "image.png"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setHeader("Content-ID", "<image>"); multipart.addBodyPart(messageBodyPart); // 发送合成消息 message.setContent(multipart); // 发送消息 Transport.send(message); System.out.println("邮件发送成功!"); }catch (MessagingException mex) { mex.printStackTrace(); } } } ``` 其中,需要替换的部分有: - `from@example.com` 为发件人邮箱地址。 - `to@example.com` 为收件人邮箱地址。 - `smtp.qq.com` 为发件人邮箱SMTP服务器地址,可以根据实际情况进行修改。 - `你的QQ账号` 和 `你的QQ邮箱授权码` 分别为发件人邮箱的账号和授权码,需要替换为实际的内容。 - `image.png` 为要添加的图片文件名,需要替换为实际的图片文件名。 运行程序后,将会发送一封包含图片的HTML格式邮件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值