赞
踩
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.7</version>
- </dependency>
- public static void main(String[] args) {
- try {
- Properties prop = new Properties();
- // 设置QQ邮件服务器
- prop.setProperty("mail.host", "mail.xxxxx.com");
- // 当使用公司内部邮箱,可以设置端口
- //prop.setProperty("mail.smtp.port", "25");
- // 当使用公司内部邮箱,可以设置服务器
- //prop.setProperty("mail.smtp.host", "smtp.qq.com");
- // 邮件发送协议
- prop.setProperty("mail.transport.protocol", "smtp");
- // 需要验证用户名密码
- prop.setProperty("mail.smtp.auth", "true");
-
- // QQ邮箱设置SSL加密(可以不改)
- MailSSLSocketFactory sf = new MailSSLSocketFactory();
- sf.setTrustAllHosts(true);
- prop.put("mail.smtp.ssl.enable", "true");
- prop.put("mail.smtp.ssl.socketFactory", sf);
-
- //1、创建定义整个应用程序所需的环境信息的 Session 对象
- Session session = Session.getDefaultInstance(prop, new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- //传入发件人的姓名和授权码
- return new PasswordAuthentication("xxxxx@xxxxx.com", "xxxxxx");
- }
- });
-
- //2、通过session获取transport对象
- Transport transport = session.getTransport();
-
- //3、通过transport对象邮箱用户名和授权码连接邮箱服务器
- transport.connect("mail.xxxx.com", "xxx@xxx.com", "xxxx");
-
- //4、创建邮件,传入session对象
- MimeMessage mimeMessage = complexEmail(session);
- //邮件发送太慢,有错误地址停止发送
- transport.addTransportListener(new CustomTransportListener(transport));
- //5、发送邮件
- transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
-
- //6、关闭连接
- transport.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- public static MimeMessage complexEmail(Session session) throws MessagingException {
- //消息的固定信息
- MimeMessage mimeMessage = new MimeMessage(session);
-
- //发件人
- mimeMessage.setFrom(new InternetAddress(username));
- List<ChatGPTMailAddress> mailList = statisticsDao.selectAllMail();
- List<String> toList = new ArrayList<>();
- List<String> ccList = new ArrayList<>();
- if (CollectionUtils.isNotEmpty(mailList)) {
- mailList.forEach(o -> {
- if ("1".equals(o.getMailType())) {
- toList.add(o.getMailAddress());
- } else if ("2".equals(o.getMailType())) {
- ccList.add(o.getMailAddress());
- }
-
- });
- }
- if (CollectionUtils.isNotEmpty(toList)) {
- Address[] tos = new InternetAddress[toList.size()];
- for (int i = 0; i < toList.size(); i++) {
- tos[i] = new InternetAddress(toList.get(i));
- }
- //收件人
- mimeMessage.setRecipients(Message.RecipientType.TO, tos);
- }
-
- if (CollectionUtils.isNotEmpty(ccList)) {
- Address[] ccs = new InternetAddress[ccList.size()];
- for (int i = 0; i < ccList.size(); i++) {
- ccs[i] = new InternetAddress(ccList.get(i));
- }
- //收件人
- mimeMessage.setRecipients(Message.RecipientType.CC, ccs);
- }
-
- //邮件标题
- mimeMessage.setSubject("儿童对话体验数据分析同步");
-
- //邮件内容
- //准备图片数据
- // MimeBodyPart image = new MimeBodyPart();
- // DataHandler handler = new DataHandler(new
- // FileDataSource("D:\\picture\\test1.jpg"));
- // image.setDataHandler(handler);
- // image.setContentID("test1.jpg"); //设置图片id
-
- String html = HtmlTableTemplate.generateHtml(list, charLength.toString(),questionRate.toString());
- //准备文本
- MimeBodyPart text = new MimeBodyPart();
- text.setContent(html, "text/html;charset=utf-8");
-
-
- //拼装邮件正文
- MimeMultipart mimeMultipart = new MimeMultipart();
- // mimeMultipart.addBodyPart(image);
- mimeMultipart.addBodyPart(text);
- mimeMultipart.setSubType("related");
-
- //将拼装好的正文内容设置为主体
- MimeBodyPart contentText = new MimeBodyPart();
- contentText.setContent(mimeMultipart);
-
- //拼接附件
- MimeMultipart allFile = new MimeMultipart();
- //正文
- allFile.addBodyPart(contentText);
- if (StringUtils.isNotBlank(txtPath)) {
- //附件
- MimeBodyPart appendix = new MimeBodyPart();
- appendix.setDataHandler(new DataHandler(new FileDataSource(txtPath)));
- appendix.setFileName("chatGPT统计.txt");
- //附件
- allFile.addBodyPart(appendix);
- }
- if (StringUtils.isNotBlank(path)) {
- //附件
- MimeBodyPart appendix = new MimeBodyPart();
- appendix.setDataHandler(new DataHandler(new FileDataSource(path)));
- appendix.setFileName("用户统计.xlsx");
- //附件
- allFile.addBodyPart(appendix);
- }
- //正文和附件都存在邮件中,所有类型设置为mixed
- allFile.setSubType("mixed");
-
-
- //放到Message消息中
- mimeMessage.setContent(allFile);
- mimeMessage.saveChanges();//保存修改
-
- return mimeMessage;
- }
- class CustomTransportListener extends TransportAdapter {
- Transport transport;
-
- CustomTransportListener(Transport transport) {
- this.transport = transport;
- }
-
- @Override
- public void messageNotDelivered(TransportEvent transportEvent) {
- Address[] addresses = transportEvent.getValidUnsentAddresses();
- Message message = transportEvent.getMessage();
- try {
- transport.sendMessage(message, addresses);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。