Merge branch 'mailMessageExceptions' into develop

* mailMessageExceptions:
  Changes to our mail sender to make partial sending possible, when some of the recipients is invalid.
This commit is contained in:
Emil Siverhall 2012-03-28 13:27:05 +02:00
commit a39b64a673
2 changed files with 35 additions and 9 deletions
src/main/java/se/su/dsv/scipro

@ -8,11 +8,12 @@ import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.sun.mail.smtp.SMTPMessage;
import se.su.dsv.scipro.data.dao.interfaces.GeneralSystemSettingsDao;
@Component
@ -31,8 +32,10 @@ public class Mail {
session.setDebug(debug);
// create a message
MimeMessage msg = new MimeMessage(session);
SMTPMessage msg = new SMTPMessage(session);
// Make sure we send the message to valid recipients even if some of the addresses are invalid
msg.setSendPartial(true);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(fromEmail);
addressFrom.setPersonal(fromName,"ISO-8859-1");

@ -1,13 +1,19 @@
package se.su.dsv.scipro.workerthreads;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.mail.Address;
import javax.mail.MessagingException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.sun.mail.smtp.SMTPSendFailedException;
import se.su.dsv.scipro.data.dao.interfaces.GeneralSystemSettingsDao;
import se.su.dsv.scipro.data.dao.interfaces.MailEventDao;
import se.su.dsv.scipro.data.dataobjects.MailEvent;
@ -59,22 +65,39 @@ public class MailEventWorker extends AbstractWorker {
replyToEmails[i] = u.getEmailAddress();
i++;
}
//TODO test-
//String[] tmp = new String[]{"mpeters@dsv.su.se"};
if( generalSystemSettingsDao.getGeneralSystemSettingsInstance().isMailNotifications() ){ //If false, mail-events "vanish"
logger.debug("Sending mail notification to: "+recipientsEmails);
mail.mail(fromName, fromEmail, recipientsEmails /* tmp */, replyToEmails, subject, messageBody, false);
logger.debug("Sending mail notification to: "+recipients);
mail.mail(fromName, fromEmail, recipientsEmails, replyToEmails, subject, messageBody, false);
}
mailEvent = mailEventDao.reLoad(mailEvent); //Re-attach to session so delete works
mailEventDao.delete(mailEvent);
this.commitTransaction();
} catch(Exception e){
} catch(SMTPSendFailedException e){
// We need to catch this exception for the use case when one or
// more of the recipients are invalid, to make sure we still
// send the mail to the valid recipients, and delete the mail
// event to prevent spamming.
for(Address a : e.getInvalidAddresses()) {
logger.info("Message could not be sent to the following invalid address: "+a.toString());
}
for(Address a : e.getValidSentAddresses()){
logger.info("Message have been sent to the following valid address: "+a.toString());
}
for(Address a : e.getValidUnsentAddresses()){
logger.info("Message have not been sent to the following valid address: "+a.toString());
}
mailEvent = mailEventDao.reLoad(mailEvent);
mailEventDao.delete(mailEvent);
this.commitTransaction();
} catch (UnsupportedEncodingException e) {
this.rollbackTransaction();
e.printStackTrace();
} catch (MessagingException e) {
this.rollbackTransaction();
e.printStackTrace();
//throw new RuntimeException(e);
}
}