Java 发送邮件
JavaMail 依赖、发送纯文本与 HTML 邮件、添加附件。
添加依赖
Since Java 11 JavaMail is not part of the JDK; add jakarta.mail (new coordinates) or javax.mail (legacy). One dependency line in Maven is enough.
<!-- Maven -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
<!-- Gradle -->
implementation 'com.sun.mail:jakarta.mail:2.0.1'发送一封简单的 E-mail
The steps: configure the SMTP host and auth, build a Session, compose a MimeMessage, then Transport.send. Put the provider's app password where the password goes.
import java.util.Properties;
import jakarta.mail.Message;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
public class SendMail {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("from@example.com"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@example.com"));
msg.setSubject("Hello from Java");
msg.setText("This is a plain text mail.");
Transport.send(msg, "from@example.com", "your-app-password");
System.out.println("sent");
}
}sent发送 HTML 邮件
Replace setText with setContent(html, "text/html; charset=UTF-8") to send HTML. For both plain text and HTML, use a MimeMultipart of subtype alternative.
String html = "<h1>Hello</h1><p>This is <b>HTML</b> mail.</p>";
MimeMessage msg = new MimeMessage(session);
msg.setContent(html, "text/html; charset=UTF-8");
System.out.println(html.contains("<b>"));true发送带附件的邮件
Attachments use a mixed MimeMultipart: add the body part first, then a MimeBodyPart per file via attachFile. The container holds the body plus the attachments.
MimeMultipart multipart = new MimeMultipart("mixed");
MimeBodyPart text = new MimeBodyPart();
text.setText("see attachment");
MimeBodyPart file = new MimeBodyPart();
file.attachFile("report.pdf");
multipart.addBodyPart(text);
multipart.addBodyPart(file);
msg.setContent(multipart);
System.out.println(multipart.getCount());2添加依赖
Java 11 起 JavaMail 不再包含在 JDK 里,需要单独引入 jakarta.mail(新坐标)或 javax.mail(旧坐标)。Maven 项目加一行依赖即可。
<!-- Maven -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
<!-- Gradle -->
implementation 'com.sun.mail:jakarta.mail:2.0.1'发送一封简单的 E-mail
核心步骤:配置 SMTP 主机与认证、Session.getInstance 建会话、MimeMessage 组装内容、Transport.send 发送。密码位置填邮箱的「授权码」。
import java.util.Properties;
import jakarta.mail.Message;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
public class SendMail {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("from@example.com"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@example.com"));
msg.setSubject("Hello from Java");
msg.setText("This is a plain text mail.");
Transport.send(msg, "from@example.com", "your-app-password");
System.out.println("sent");
}
}sent发送 HTML 邮件
把 setText 换成 setContent(html, "text/html; charset=UTF-8") 就能发 HTML。要同时支持纯文本和 HTML,用 MimeMultipart 的 alternative 子类型。
String html = "<h1>Hello</h1><p>This is <b>HTML</b> mail.</p>";
MimeMessage msg = new MimeMessage(session);
msg.setContent(html, "text/html; charset=UTF-8");
System.out.println(html.contains("<b>"));true发送带附件的邮件
附件用 MimeMultipart 的 mixed 类型:先加正文部分,再加 MimeBodyPart 并 attachFile。整体结构是「混合容器里装正文 + 若干附件」。
MimeMultipart multipart = new MimeMultipart("mixed");
MimeBodyPart text = new MimeBodyPart();
text.setText("see attachment");
MimeBodyPart file = new MimeBodyPart();
file.attachFile("report.pdf");
multipart.addBodyPart(text);
multipart.addBodyPart(file);
msg.setContent(multipart);
System.out.println(multipart.getCount());2