前言
相同功能实现,python版本 跳转Blog:https://gryffinbit.top/2022/08/15/ubuntu%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%AD%E6%90%AD%E5%BB%BAsmtp%E9%82%AE%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8/
调用钉钉api接口,实现机器人自动发送消息并艾特个人,blog跳转:
https://gryffinbit.top/2022/08/17/%E8%B0%83%E7%94%A8%E9%92%89%E9%92%89api%E6%8E%A5%E5%8F%A3%EF%BC%8C%E5%AE%9E%E7%8E%B0%E6%9C%BA%E5%99%A8%E4%BA%BA%E8%87%AA%E5%8A%A8%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF%E5%B9%B6%E8%89%BE%E7%89%B9%E4%B8%AA%E4%BA%BA/
smtp服务器授权
我使用的是126的smtp服务。之前有自己搭建过smtp服务器,但会有两个问题,第一是用腾讯云服务器搭建的话,25端口需要申请,而且不能发邮件,可能封禁。第二就是自己搭建的smtp服务器,发送邮件大概率会被扔进垃圾箱。
126邮箱授权
开启smtp服务
开启成功后,会获得一个授权码,后面会用到。
代码
mailUsername
是开启smtp服务,并获取到授权码的126邮箱。
mailAuthPassword
是开启smtp服务后,获取到的授权码
sender
填写发件人的邮箱地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| package main.controller;
import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties;
public class Notice{ public static String mailUsername = "[email protected]"; public static String mailAuthPassword = "xxxx";
public static String mailHost = "smtp.126.com";
public static String receiveMailAccount = "[email protected]"; public static String username = "test"; public static String passwd = "test123"; public static String usergroup = "admin";
public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", mailHost); props.setProperty("mail.smtp.auth", "true");
final String smtpPort = "25"; props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", smtpPort);
Session session = Session.getDefaultInstance(props); session.setDebug(true);
MimeMessage message = createMimeMessage(session, mailUsername, receiveMailAccount);
Transport transport = session.getTransport();
transport.connect(mailUsername, mailAuthPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close(); }
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception { MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sendMail, "第二组账号创建结果通知", "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用户", "UTF-8"));
message.setSubject("账号创建结果告知", "UTF-8");
String context = "你的账号创建结果如下:</br>"; context += "用户名:" + username + "</br>"; context += "密码:" + passwd + "</br>"; context += "属组:" + usergroup + "</br>"; message.setContent(context, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
return message; } }
|
结果
备注
整个项目打包(包括调用钉钉api接口,实现机器人自动发送消息并艾特个人):
链接: https://pan.baidu.com/s/1yjzsDVV4LlbOqx7VvpVuKQ?pwd=1kn8 提取码: 1kn8
–来自百度网盘超级会员v5的分享