我向使用javax.mail的用戶發送大量電子郵件(大約1000個),這些用戶來自我的SMTP服務器,該郵件擁有我的域名。所有的收件人都在同一個域名的電子郵件。使用javax.mail發送大量電子郵件,某些電子郵件的發送失敗
發送郵件時,如果只有成功,那麼我將令牌保存在userToken表中。否則,我不會將該令牌保存在表中。該過程完成後,userToken表將插入950行,這意味着所有電子郵件都會成功。
我已經寫了下面的代碼發送批量電子郵件,但幾乎300個電子郵件沒有收到用戶。我din't得到執行時的錯誤,
@RequestMapping(value = "bulkMail")
public @ResponseBody String bulkMail(Model model, HttpServletRequest request,@RequestParam(value="myArray") Long[] myArray, HttpServletResponse response) throws IOException {
Session session = null;
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("roca");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
HttpSession httpSession = request.getSession(false);
session = getRocaSession();
Test test = (Test) httpSession.getAttribute("test");
for (int i = 0; i < myArray.length; i++) {
User user = entityManager.createQuery("SELECT u FROM User u where u.userId=" + myArray[i], User.class).getSingleResult();
UserToken userToken;
try {
userToken = entityManager.createQuery("SELECT ut FROM UserToken ut where ut.testId="+test.getId()+" and ut.userId="+myArray[i], UserToken.class).getSingleResult();
} catch (Exception e) {
userToken = new UserToken();
}
String mailStatus = sendMail(userToken,test,request, user.getUserId(), user.getUserName(), context, user.getEmailId(),session);
if (!mailStatus.equals("failure")) {
userToken.setToken(mailStatus);
userToken.setCreatedDate(new Date());
userToken.setUpdatedDate(new Date());
userToken.setTestId(test.getId());
userToken.setUserId(user.getUserId());
userToken.setUsed(0);
testDao.saveOrUpdate(userToken);
}
}
entityManager.getTransaction().commit();
entityManager.close();
return "Emails have been send to Users";
}
發送電子郵件
public String sendMail(UserTestToken userTestToken, Test test,HttpServletRequest request, Long accountId,String accountName,
String context,String accountEmail,Session session) {
String token = getToken();
try {
String email = mailService.sendAlert(test, accountEmail,"User Triger", "[email protected]", "Val", accountId, token, context, accountName, session);
if (!email.equals("failure")) {
return token;
}else {
return "failure";
}
} catch (Exception e) {
return "failure";
}
}
獲取會話的電子郵件
public Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", "gatesmtp.moc.domain.com");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props);
session.setDebug(false);
return session;
}
MailService的Implementaion
public String sendTestMailAlert(Test test, String accountEmail, String receiverName, String senderEmail, String url, Long accountId, String token, String context, String accountName, Session session)
{
String subject = "Subject";
String content = "Content";
return sendMail(accountEmail, content, subject, receiverName, senderEmail, session);
}
private String sendMail(String receiverMailIds, String content, String subject, String employeeName, String employeeEmail, Session session) {
try {
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(employeeEmail, employeeName));
} catch (UnsupportedEncodingException e) {
this.logger.error("Mail service address catch UnsupportedEncodingException");
}
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverMailIds));
message.setSubject(subject);
message.setContent(content, "text/html");
Transport.send(message);
return "success";
} catch (Exception e) {
this.logger.error("Mail Exception - " + receiverMailIds);
}
return "failure";
}
https://stackoverflow.com/questions/13287515/how-to-send-bulk-mails-using-javax-mail-api-efficiently-can-we-use-reuse -auth –