gpt4 book ai didi

java - 电子邮件从 SPRING JAVA Mail API 收到重复项

转载 作者:行者123 更新时间:2023-12-01 16:35:01 25 4
gpt4 key购买 nike

我使用 Spring Web 应用程序中的 Java 邮件 API 每周向 Outlook 邮件发送一封电子邮件。该功能在前几周表现正常,然后没有任何变化,Outlook 收到了两封电子邮件,下周收到了三封电子邮件,然后是四封,然后是五封电子邮件。Java 代码中设置的日志表明应用程序仅发送一封电子邮件。我无法通过将计划更改为每 15 分钟、每小时或任何更短的时间间隔发送一次来重现该问题。

电子邮件 Controller 类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class WeeklyReportScheduler {

@Autowired
private WeeklyReportService weeklyReportService;

@Scheduled(cron = "${cron.expression}")
public void sendWeeklyReport(){
weeklyReportService.sendWeeklyReport();
}
}

电子邮件服务类别:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.stereotype.Service;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WeeklyReportService {

@Value("${weekly.report.mail.subject}")
private String subject;

@Value("${weekly.report.mail.body}")
private String mailBody;

@Value("${mail.body.empty.report}")
private String emptyReportMailBody;

@Value("${receiver}")
private String receiver;

@Autowired
private MailService mailService;

@Autowired
private WeeklyReportLogDao weeklyReportLogDao;

@Autowired
private ProjectService projectService;

@Value("${export.path}")
private String exportDir;

protected final Log logger = LogFactory.getLog(getClass());

public void sendWeeklyReport(){
boolean emptyReport = true;
//retrieving attachment data 'projects'
if(projects.size() != 0){
emptyReport = false;
}
String body = "";
if(emptyReport){
body = emptyReportMailBody;
} else {
body = mailBody;
}
SimpleDateFormat format = new SimpleDateFormat("MM/dd/YYYY");
String dateString = format.format(new Date());
String mailSubject = subject + " " + dateString;
List recipients = new ArrayList<String>();
recipients.add(receiver);
String fileName = mailSubject.replace(" ", "_").replace("/", "_");
WeeklyReportExcelExport excelExport = new WeeklyReportExcelExport(exportDir, fileName);
excelExport.createReport(projects);
File excelFile = excelExport.saveToFile();
File[] attachments = new File[1];
attachments[0] = excelFile;

boolean sent = false;
String exceptionMessage = "";
for(int i=0; i<3; i++){
try {
logger.info("Sending Attempt: " + i+1);
Thread.sleep(10000);
mailService.mail(recipients, mailSubject, body, attachments);
sent = true;
break;
} catch (Exception ex) {
logger.info("sending failed because: " + ex.getMessage() + " \nRe-attempting in 10 seconds");
exceptionMessage = ex.getMessage();
sent = false;
}
}
if(!sent){
weeklyReportLogDao.logFailedReporting(dateString, exceptionMessage);
}
//re-try 3 times in case of mail sending failure
}

邮件服务类:

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailServiceImpl implements MailService {

/** The From address for the e-mail. read from ant build properties file */
private String fromAddress;

/** The mail sender. */
private JavaMailSender mailSender;

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

public void mail(List<String> emailAddresses, String subject, String text, File[] attachments) throws MailException {
//System.out.println("mail: "+subject);
MimeMessage message = null;

// Fill in the From, To, and Subject fields.
try {
message = mailSender.createMimeMessage();

MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(fromAddress);
for (String emailAddress : emailAddresses) {
messageHelper.addTo(emailAddress);
}
messageHelper.setSubject(subject);

// Fill in the body with the message text, sending it in HTML format.
messageHelper.setText(text, true);

// Add any attachments to the message.
if ((attachments != null) && (attachments.length != 0)) {
for (File attachment : attachments) {
messageHelper.addAttachment(attachment.getName(), attachment);
}
}
}
catch(MessagingException mse) {
String warnMessage = "Error creating message.";
logger.warn(warnMessage);
throw (new RuntimeException(warnMessage, mse));
}
try {
mailSender.send(message);
} catch (Exception ex){
logger.info("Exception sending message: " + ex.getMessage());
}
}

}

最佳答案

您的参数 emailAddresses 上可能有重复的条目,如果无法确保存储库层中的重复条目,请尝试将其移动到树集中

关于java - 电子邮件从 SPRING JAVA Mail API 收到重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61972488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com