public void sendNotificationMail( String toList, String ccList, String bccList, String emailSubject, String emailText, boolean isException, String attachmentpos) throws Exception { try { // In case of Exception email, Read the Property file to get To/CC/Sub from property file. if (isException) { PropertyFileReader propertyReader = PropertyFileReader.getInstance(); toList = propertyReader.getStringProperty("errorMailTo"); ccList = propertyReader.getStringProperty("errorMailCC"); emailSubject = propertyReader.getStringProperty("errorMailSubject"); } // bcc & attachmentPos is hardcoded NULL in case of exception email sendMail(toList, ccList, bccList, emailSubject, emailText, null); /*emailNotificationBean.setAllValues(toList, ccList, bccList, emailSubject, emailText); sendMail(toList, ccList, bccList, emailSubject, emailText, attachmentpos); /*NotificationCenterBean emailNotificationBean = new NotificationCenterBean(); emailNotificationBean.setAllValues(toList, ccList, bccList, emailSubject, emailText); System.out.println(emailNotificationBean); notificationCenterService.saveNotification(emailNotificationBean);*/ } catch (Exception e) { e.printStackTrace(); } }
private void doInit() throws Exception { PropertyFileReader propertReader = PropertyFileReader.getInstance(); emailId = propertReader.getStringProperty("errorMailFrom"); password = propertReader.getStringProperty("errorMailPassword"); protocol = propertReader.getStringProperty("mailProtocol"); host = propertReader.getStringProperty("mailHost"); smtpHost = propertReader.getStringProperty("mailSmtpHost"); smtpPortNo = propertReader.getIntProperty("mailSmtpPortNo"); props = System.getProperties(); props.setProperty("mail.store.protocol", protocol); Authenticator auth = new EmailAuthentication(emailId, password); session = Session.getDefaultInstance(props, auth); store = session.getStore("pop3"); store.connect(host, emailId, password); props.put("mail.smtp.port", smtpPortNo); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "false"); props.put("mail.smtp.ssl.enable", "false"); }
private void sendMail( String to, String cc, String bcc, String subject, String msgContent, List<EmailAttachmentBean> emailAttachmentList) throws Exception { PropertyFileReader propertyFileReader = PropertyFileReader.getInstance(); doInit(); msgContent += "<br/><br/><br/><br/> This is a system generated email, do not reply to this email id."; MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(emailId)); // Set To: header field of the header. message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); if (cc != null && cc.length() != 0) message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); if (bcc != null && bcc.length() != 0) { bcc = bcc.replaceAll(";", ","); message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc)); } // Set Subject: header field message.setSubject(subject); /* Start Here */ // Create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText( "<pre style='font-size:16px;font-family: Calibri'>" + msgContent + "</pre>", null, "html"); // Create a multipar message Multipart multipart = new MimeMultipart(); // please don't delete this code before deleting please let me know @Siva Sankar // this code is for sending email with attachment if (emailAttachmentList != null && !emailAttachmentList.isEmpty()) { for (EmailAttachmentBean emailAttachmentBean : emailAttachmentList) { MimeBodyPart attachmentPart = new MimeBodyPart(); DataSource source = new ByteArrayDataSource(emailAttachmentBean.getFileContent(), "text/html"); attachmentPart.setDataHandler(new DataHandler(source)); attachmentPart.setFileName(emailAttachmentBean.getFileName()); multipart.addBodyPart(attachmentPart); } /*if (attachmentpos != null && attachmentpos.contains("&&&&")) { MimeBodyPart attachmentPart = new MimeBodyPart(); String[] strArray = attachmentpos.split("&&&&"); String attchFileContent = strArray[0]; String attchFileName = strArray[1].trim(); DataSource source = new FileDataSource(attchFileContent); attachmentPart.setDataHandler(new DataHandler(source)); attachmentPart.setFileName(attchFileName); multipart.addBodyPart(attachmentPart); }*/ } // Set text message part multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart); // Send message Transport.send(message); /* End Here */ System.out.println("Sent message successfully.... Sent to " + to); }