HoRain云--Java发送邮件 HoRain云小助手个人主页 个人专栏: 《Linux 系列教程》《c语言教程》⛺️生活的理想就是为了理想的生活!⛳️ 推荐前些天发现了一个超棒的服务器购买网站性价比超高大内存超划算忍不住分享一下给大家。点击跳转到网站。专栏介绍专栏名称专栏介绍《C语言》本专栏主要撰写C干货内容和编程技巧让大家从底层了解C把更多的知识由抽象到简单通俗易懂。《网络协议》本专栏主要是注重从底层来给大家一步步剖析网络协议的奥秘一起解密网络协议在运行中协议的基本运行机制《docker容器精解篇》全面深入解析 docker 容器从基础到进阶涵盖原理、操作、实践案例助您精通 docker。《linux系列》本专栏主要撰写Linux干货内容从基础到进阶知识由抽象到简单通俗易懂帮你从新手小白到扫地僧。《python 系列》本专栏着重撰写Python相关的干货内容与编程技巧助力大家从底层去认识Python将更多复杂的知识由抽象转化为简单易懂的内容。《试题库》本专栏主要是发布一些考试和练习题库涵盖软考、HCIE、HRCE、CCNA等目录⛳️ 推荐专栏介绍 Java 发送邮件从 SMTP 到企业级 依赖先就位纯 Jakarta MailJava 17 推荐Spring Boot生产首选 最小可跑纯 JavaMailSMTP 25 或 465/587 常见变体HTML / 附件 / 多收件人HTML 邮件带附件多收件人 / 抄送 / 密送 SSL / TLS 端口速查 Spring Boot 写法生产真用这个application.ymlService 层⚠️ 高频踩坑清单️ 生产进阶姿势 Java 发送邮件从 SMTP 到企业级Java 发邮件主线就两条纯 JavaMailJakarta Mail​ 和Spring BootJavaMailSender。前者是底层 API后者是生产里 90% 场景的写法。下面从「能跑」到「不踩坑」铺开。 依赖先就位纯 Jakarta MailJava 17 推荐!-- pom.xml -- dependency groupIdcom.sun.mail/groupId artifactIdjakarta.mail/artifactId version2.0.1/version /dependencyJDK 11 之前叫javax.mailJava 11 后 Jakarta EE 改名jakarta.mail包名从javax.mail.*→jakarta.mail.*别引错。Spring Boot生产首选dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-mail/artifactId /dependency 最小可跑纯 JavaMailSMTP 25 或 465/587import jakarta.mail.*; import jakarta.mail.internet.*; import java.util.Properties; public class MailDemo { public static void main(String[] args) throws Exception { Properties props new Properties(); props.put(mail.smtp.host, smtp.qq.com); props.put(mail.smtp.port, 587); props.put(mail.smtp.auth, true); props.put(mail.smtp.starttls.enable, true); // 587 走 STARTTLS // 465 用 props.put(mail.smtp.socketFactory.class, javax.net.ssl.SSLSocketFactory); 老写法 Session session Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( 123456qq.com, // 发件人 xxxxxx // ⚠️ QQ/163 这里是【授权码】不是邮箱密码 ); } }); Message msg new MimeMessage(session); msg.setFrom(new InternetAddress(123456qq.com)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverexample.com)); msg.setSubject(测试邮件, UTF-8); msg.setText(你好这是一封 Java 发的邮件, UTF-8); Transport.send(msg); System.out.println(发送成功); } }⚠️ 国内邮箱QQ/163/126密码栏填的不是登录密码是「授权码」——要去邮箱设置 → 开启 POP3/SMTP → 生成授权码卡在这的人 80%。 常见变体HTML / 附件 / 多收件人HTML 邮件msg.setContent(h1Hello/h1p stylecolor:red红色字/p, text/html; charsetUTF-8);带附件MimeMessage msg new MimeMessage(session); msg.setFrom(…); msg.setSubject(带附件, UTF-8); // 正文 MimeBodyPart textPart new MimeBodyPart(); textPart.setText(见附件, UTF-8); // 附件 MimeBodyPart attachPart new MimeBodyPart(); attachPart.attachFile(report.pdf); attachPart.setFileName(MimeUtility.encodeText(报表.pdf, UTF-8, B)); // 中文文件名防乱码 Multipart mp new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(attachPart); msg.setContent(mp);多收件人 / 抄送 / 密送msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(ax.com,bx.com)); msg.setRecipient(Message.RecipientType.CC, new InternetAddress(ccx.com)); msg.setRecipient(Message.RecipientType.BCC, new InternetAddress(bccx.com)); SSL / TLS 端口速查端口协议备注25SMTP明文很多云厂商封老默认现基本不用587STARTTLS推荐明文→协商加密465SMTPSSSL/TLS 直连老写法mail.smtp.ssl.enabletrueQQ 邮箱两路都通587(STARTTLS) / 465(SSL)163 同理。 Spring Boot 写法生产真用这个application.ymlspring: mail: host: smtp.qq.com port: 587 username: 123456qq.com password: 授权码 # 不是邮箱密码 properties: mail: smtp: auth: true starttls: enable: trueService 层Service public class MailService { Autowired private JavaMailSender mailSender; public void send(String to, String subject, String body) { SimpleMailMessage msg new SimpleMailMessage(); msg.setFrom(123456qq.com); msg.setTo(to); msg.setSubject(subject); msg.setText(body); mailSender.send(msg); } // 带附件 public void sendWithAttach(String to, String pdfPath) throws MessagingException { MimeMessage msg mailSender.createMimeMessage(); MimeMessageHelper helper new MimeMessageHelper(msg, true, UTF-8); helper.setFrom(123456qq.com); helper.setTo(to); helper.setSubject(报表); helper.setText(请查收附件); helper.addAttachment(报表.pdf, new File(pdfPath)); mailSender.send(msg); } }Spring 把Authenticator、Session 构建全包了配置里username/password一把梭比纯 JavaMail 短一半。⚠️ 高频踩坑清单坑现象解法QQ/163 密码用登录密码535 Login Fail换SMTP 授权码​阿里云/腾讯云服务器 25 端口连不通 / 超时换 587 或申请解封 25中文附件名乱码收到???.pdfMimeUtility.encodeText(fileName, UTF-8, B)Gmail要用 App Password 允许低安全应用已废OAuth2 或 App Password企业邮箱钉钉/企业微信要走企业 SMTP 或 API看企业文档常要 IP 白名单生产群发被当成垃圾邮件DKIM/SPF 配好别一秒狂发️ 生产进阶姿势模板邮件FreeMarker / Thymeleaf 渲染 HTML再塞进helper.setText(html, true)异步发Async或 MQ别让主线程卡 SMTP云厂商 SMTP 替代AWS SES / 阿里云 DM / SendGrid → HTTP API不走 SMTP量大稳失败重试MQ 死信比 while-retry 稳要不要顺着往下看Spring Boot Thymeleaf 模板邮件​ 或者AWS SES / 阿里云 DM 的 Java SDK 发信不走 SMTPHTTP 那侧跟前面 FastAPI / PHP 那条工具线也能串上。❤️❤️❤️本人水平有限如有纰漏欢迎各位大佬评论批评指正如果觉得这篇文对你有帮助的话也请给个点赞、收藏下吧非常感谢! Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧