我已经使用cron作业调度程序通过在mail.properties中配置邮件设置用户名和密码向多个收件人发送自动邮件,同时向多个客户端发送邮件我注意到异常显示gsmtp异常,即使邮件限制未超过当天
@Scheduled(cron = "0 0 1 * * *")
/ * Weekly runs @ 01:00AM
* Sends the DDR Pending count - email to Consortia Member and cc to CA[Configurable]
*/
public void sendDDRPendingAutoMailToMember() {
ArrayList<DDRAutoMailSchedulerVO> clientListForAutoMail = iDDRReportManager.getClientListForAutoMail();
iMailManager.sendMailToCM(clientListForAutoMail);
}
@Override
public void sendMailToCM(ArrayList<DDRAutoMailSchedulerVO> clientListForAutoMail) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
String temp_subject = " Pending DDR as of " + sdf.format(c.getTime());
try {
String jndiName = "java:comp/env/jdbc/JgateDatasource";
javax.sql.DataSource dataSrc;
InitialContext icxt = new InitialContext();
dataSrc = (javax.sql.DataSource)icxt.lookup(jndiName);
Connection connection = dataSrc.getConnection();
int mailSendCount = 0;
for(DDRAutoMailSchedulerVO clientDetail : clientListForAutoMail) {
if(mailSendCount == 10) {
mailSendCount = 0;
try {
Thread.sleep(300000);
}catch(InterruptedException e) {
System.out.println("Exception on Thread Sleep...");
}
}
int ddr_count = clientDetail.getDdrPendingCount();
MimeMessage message = generalMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(clientDetail.getMemberEmail().split(","));
if(clientDetail.getCcMailtoCA() == 1) {
helper.setCc(clientDetail.getCcEmail().split(","));
}
String subject = ddr_count + temp_subject + " : " + clientDetail.getWorkspaceName();
helper.setSubject(subject);
helper.setFrom(JGatePropertiesManager.getPropertyValue(PresentationContants.MAIL_USER_NAME));
Map<String, Object> model = new HashMap<String, Object>();
model.put("ddr_count", ddr_count > 1 ? "are " + ddr_count : "is " + ddr_count);
model.put("member_name", clientDetail.getMemberName());
model.put("from_date", sdf.format(clientDetail.getFromDate()));
model.put("to_date", sdf.format(c.getTime()));
String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "autoDDRPendingMailTemplate.vm", model);
helper.setText(body, true);
System.out.println("Sending DDRPending mail to ... " + clientDetail.getMemberEmail());
String fileName = "DDRPendingAutoMailScheduler.jasper";
InputStream file = getClass().getResourceAsStream("/com/informatics/jgate2/reports/" + fileName);
Map<String, Object> parameter_jasper = new HashMap<String, Object>();
parameter_jasper.put("workspace_id", clientDetail.getWorkspace_id());
parameter_jasper.put("created_date", sdf.format(new Date()));
parameter_jasper.put("from_date", sdf.format(clientDetail.getFromDate()));
parameter_jasper.put("to_date", sdf.format(c.getTime()));
DataSource ds = null;
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(file, parameter_jasper, connection);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRXlsExporter exporterXls = new JRXlsExporter();
exporterXls.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXls.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, false);
exporterXls.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, true);
exporterXls.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, false);
exporterXls.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, true);
exporterXls.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, true);
exporterXls.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, true);
exporterXls.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporterXls.exportReport();
ds = new ByteArrayDataSource(baos.toByteArray(), "application/excel");
} catch (JRException ex) {
Logger.getLogger(JGateMailManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
}
helper.addAttachment(clientDetail.getWorkspaceName() + "_" +sdf.format(new Date()) + ".xls", ds);
generalMailSender.send(message);
mailSendCount++;
System.out.println("Sent Successfully");
}
connection.close();
} catch (MessagingException ex) {
Logger.getLogger(JGateMailManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (NamingException ex) {
Logger.getLogger(JGateMailManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(JGateMailManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
发送一系列多封邮件时出现异常: