I am trying send an email notifier using java mail. I am able to send mails to multiple recipients but with multiple subjects. Say for eg.I am sending the count of unregistered devices to san jose users it should have the subject as 'Unregistered devices for San Jose' and if it is for Boulder users it should be 'Unregistered devices for Boulder'. Here is the main method. public class EmailNotifier implements Job {
private static Map<String,String>Site_names=new HashMap<String,String>();
private static Map<String,String> hostName_EmailAlias = new HashMap<String,String>();
private static Properties emailProperties;
private static Session mailSession;
private static MimeMessage emailMessage;
static {
hostName_EmailAlias.put("gigantic-6", "[email protected]");
hostName_EmailAlias.put("ccm-sjcvtg-091", "[email protected]");
Site_names.put("gigantic-6", "UnRegistered Devices for San Jose");
Site_names.put("ccm-sjcvtg-091", "UnRegistered Devices for Pre-Alpha");
public void createSendEmailMessage(QueryDataObject dataObj) throws AddressException,
MessagingException, JobExecutionException {
String emailPort = "25";
String emailHost = "outbound.xxxxx.com";
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.host", emailHost);
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "false");
String emailSubject = Site_names.get( "gigantic-6");
String emailSubject1=Site_names.get( "ccm-sjcvtg-091");
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(dataObj.getEmailAlias()));
emailMessage.setSubject(emailSubject,emailSubject1);
emailMessage.setContent(dataObj.getEmailBody(), "text/html");// for a html email
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost,emailSubject.emailSubject1);
//transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
}
I have created 2 Maps one for different hostnames for diff recipients and one for diff Subject(Sitenames).I have created 2 strings with emailSubject and emailSubject1.I do not know how to relate each hostname and mail with corresponding subject. I am not asking complete code help.Any pointers or thoughts would help.
Thanks
The subject is actually part of the body of the message. You cannot send a single message to multiple recipients and vary the content per recipient.
For each distinct content (including the subject) you must create a separate message. If there are multiple recipients for that distinct content you can then send that one message to those multiple recipients.