I am sending large number of emails(around 1000) to the users using javax.mail from my SMTP server, which owns for my domain. And all the recipients are in the same domain email.
When sending mail, if there is only success then I am saving the token in userToken table. Else I am not saving the token in table. After completion of the process, userToken table inserted with 950 rows, which means there is success for all the emails.
I have written following code to sending bulk emails, But almost 300 emails were not received by the users. I din't get any errors while execution,
@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";
}
Sending emails
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", "noreply@mydomain.com", "Val", accountId, token, context, accountName, session);
if (!email.equals("failure")) {
return token;
}else {
return "failure";
}
} catch (Exception e) {
return "failure";
}
}
Getting session for the emails
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";
}