欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
示例
下面来看一个稍复杂点的示例:
public class JavaMailTest2 {
public static void main(String[] args) throws MessagingException {
Properties props = new Properties();
// 开启debug调试
props.setProperty(“mail.debug”, “true”);
// 发送服务器需要身份验证
props.setProperty(“mail.smtp.auth”, “true”);
// 设置邮件服务器主机名
props.setProperty(“mail.host”, “smtp.163.com”);
// 发送邮件协议名称
props.setProperty(“mail.transport.protocol”, “smtp”);
// 设置环境信息
Session session = Session.getInstance(props, new Authenticator() {
// 在session中设置账户信息,Transport发送邮件时会使用
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“java_mail_001”, “javamail”);
}
});
// 创建邮件对象
Message msg = new MimeMessage(session);
// 发件人
msg.setFrom(new InternetAddress(“java_mail_001@163.com”));
// 多个收件人
msg.setRecipients(RecipientType.TO, InternetAddress.parse(“java_mail_002@163.com,java_mail_003@163.com”));
// 抄送人
msg.setRecipient(RecipientType.CC, new InternetAddress(“java_mail_001@163.com”));
// 暗送人
msg.setRecipient(RecipientType.BCC, new InternetAddress(“java_mail_004@163.com”));
// 主题
msg.setSubject(“中文主题”);
// HTML内容
msg.setContent(”
你好啊