,可以通过以下步骤实现:
以下是一个示例代码:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendEmail {
public static void main(String[] args) {
// 邮件服务器配置
String host = "smtp.example.com";
String username = "your_username";
String password = "your_password";
// 收件人列表
List<String> recipients = new ArrayList<>();
recipients.add("recipient1@example.com");
recipients.add("recipient2@example.com");
recipients.add("recipient3@example.com");
// 邮件内容
String subject = "Hello";
String content = "This is a test email.";
// 创建邮件会话
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// 创建邮件消息
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
for (String recipient : recipients) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
message.setSubject(subject);
message.setText(content);
// 发送邮件
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
在上述示例代码中,需要替换以下内容:
推荐的腾讯云相关产品:腾讯云邮件推送(https://cloud.tencent.com/product/ses)
请注意,以上示例代码仅为演示如何使用javax.mail发送给多个收件人,实际应用中可能需要根据具体情况进行适当的修改和调整。
云+社区沙龙online [云原生技术实践]
腾讯云数据湖专题直播
云+社区技术沙龙[第14期]
企业创新在线学堂
云+社区技术沙龙[第27期]
Techo Day 第三期
云原生正发声
新知·音视频技术公开课
Techo Day 第二期
领取专属 10元无门槛券
手把手带您无忧上云