Monday, 17 November 2014

Send Mail with multiple TO address and with Rich HTML Text

//Send Mail with multiple TO address and with Rich HTML Text

                    String from = abc@abcc.com
                  logger.debug("From Address :" +from);
                 
                  String[] to = "abcd@gmail.com,bcda@yahoo.com, xyzaa@gmail.com,vbgjg@hotmail.com";
                  String[] temp_to;
                  String delimeter = ",";
                  temp_to = to.split(delimeter);
                  for(int i =0; i < temp_to.length ; i++){
                      System.out.println(temp_to[i]);
                      System.out.println("");
                      }
                  logger.debug("To Address :"+to);
                  String host = "localhost";
                  logger.debug("SMTP Server IP Address :"+host);

                 
                  try{
                  //Session
                  Properties properties = System.getProperties(); 
                  properties.setProperty("mail.smtp.host", host); 
                  Session session = Session.getDefaultInstance(properties);
                              
                  //logger.info("Session ="+session);
                  logger.debug("Session object is created :"+session);
                 
                 
                  //Mail Part
                  MimeMessage message = new MimeMessage(session);
                  Multipart multipart = new MimeMultipart();
                  message.setFrom(new InternetAddress(from));
                  InternetAddress[] recipientAddress = new InternetAddress[temp_to.length];
                  int counter = 0;
                  for (String recipient : temp_to) {
                      recipientAddress[counter] = new InternetAddress(recipient.trim());
                      counter++;
                  }
                 // message.setRecipients(message.RecipientType.TO, recipientAddress);
                  message.setRecipients(javax.mail.Message.RecipientType.TO, recipientAddress);
                  message.setSubject("Document Uploaded:Need Approval");
                  logger.debug("Subject is created");
                 
                  BodyPart messageBodyPart = new MimeBodyPart();
         
                  String body = "<i>Dear Sir/Madam,</i><br>";
                  body += "<br><br><b>**********************ABCD**********************</b><br>";
                  body += "<br><font color=black>Sample Mail from p8programmer.</font><br>";
                  body += "<br><br><b><font color=red>Details:</font></b><br>";
                  body += "<br><table border=1><tbody>";
                  body += "<tr><td width=150 valign=top class=emailcontent style=padding:8px;>From</td><td width=200 valign=top class=emailcontent style=padding:8px;><b>"+from+"</b></td></tr>";
                  body += "<tr><td width=150 valign=top class=emailcontent style=padding:8px;>To</td><td width=200 valign=top class=emailcontent style=padding:8px;><b>"+to+"</b></td></tr>";
                  body += "</tbody></table><br><br><br>";
                  body += "<tr><td bgcolor=black height=5></td></tr>";
                  body += "<tr> <td style=padding:10px; class=emailcontent><br>Thanks &amp;Regards <br/>";
                  body += "<strong><i><font color=blue>p8programmer</font></i></strong></td>";
                  body += "<br><br></tr>";
                  body += "<td style=padding:10px; class=emailcontent>***************************************************************************************************************<br>";
                
               
                 
                  messageBodyPart.setContent(body,"text/html");
                  multipart.addBodyPart(messageBodyPart);
                 
                                  
                  logger.debug("...Body Part Created...");
                  message.setContent(multipart);
                  Transport.send(message);
                 
                  //logger.info("message sent successfully....");
                  logger.info("......Message sent successfully.....");
                  }
                  catch (MessagingException mex) {
                      mex.printStackTrace();
                      logger.error("Exception occured while sending mail :"+mex);
                  }

No comments:

Post a Comment