备注信息如下:
- 获取邮箱授权码,QQ邮箱获取授权码获取方式
- 获取 javax.mail-1.6.2.jar方式如下
2.1、百度网盘下载
链接:https://pan.baidu.com/s/1Bfhvk67JWAXHow01dJAN9A 提取码:mmos
2.2、通过自身所拥有的Maven项目下载
| |
| <dependency> |
| <groupId>javax.mail</groupId> |
| <artifactId>javax.mail-api</artifactId> |
| <version>1.6.2</version> |
| </dependency> |
1、此方式参数不可以放在项目.properties配置文件中通过@PostConstruct方式获取,会导致邮件发送失败,若是有大佬知道怎么处理可以在下方留言告知,或者发送邮件至1181155871@qq.com,不胜感谢!!!
| |
| |
| |
| |
| |
| |
| private void sendEmail(String targetMailAddress, String title, String content) throws Exception { |
| |
| Properties props = new Properties(); |
| |
| props.setProperty("mail.host", srcMailHost); |
| |
| props.setProperty("mail.smtp.auth", "true"); |
| props.setProperty("mail.store.protocol", "imap"); |
| Authenticator authenticator = new Authenticator() { |
| @Override |
| public PasswordAuthentication getPasswordAuthentication() { |
| |
| return new PasswordAuthentication(srcUsername, srcPassword); |
| } |
| }; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Session session = Session.getDefaultInstance(props, authenticator); |
| Store store = session.getStore(); |
| store.connect(srcUsername, srcPassword); |
| |
| Message message = new MimeMessage(session); |
| |
| message.setFrom(new InternetAddress(srcMailAddress)); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| message.setRecipient(RecipientType.TO, new InternetAddress(targetMailAddress)); |
| |
| message.setSubject(title); |
| |
| message.setContent(content, "text/html;charset=UTF-8"); |
| |
| Transport.send(message); |
| } |
2、此方法Demo可行,未进行读取配置信息发送邮件测试
| |
| |
| |
| |
| |
| |
| |
| |
| public void sendMail(String receiveMail, String subject, String mailContent) throws Exception { |
| |
| String sendMail = "xxxxxx@qq.com"; |
| String authorizationCode = "授权码"; |
| |
| String host = "smtp.qq.com"; |
| |
| Properties properties = System.getProperties(); |
| |
| properties.setProperty("mail.smtp.host", host); |
| properties.put("mail.smtp.auth", "true"); |
| MailSSLSocketFactory sf = null; |
| try { |
| sf = new MailSSLSocketFactory(); |
| } catch (GeneralSecurityException e) { |
| throw new RuntimeException(e); |
| } |
| sf.setTrustAllHosts(true); |
| properties.put("mail.smtp.ssl.enable", "true"); |
| properties.put("mail.smtp.ssl.socketFactory", sf); |
| |
| Session session = Session.getDefaultInstance(properties, new Authenticator() { |
| public PasswordAuthentication getPasswordAuthentication() { |
| return new PasswordAuthentication(sendMail, authorizationCode); |
| } |
| }); |
| try { |
| |
| MimeMessage message = new MimeMessage(session); |
| |
| message.setFrom(new InternetAddress(sendMail)); |
| |
| message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveMail)); |
| |
| message.setSubject(subject); |
| |
| message.setText(mailContent); |
| |
| Transport.send(message); |
| System.out.println("==========通过授权码发送邮件成功=========="); |
| } catch (MessagingException mex) { |
| mex.printStackTrace(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了