@Test @Ignore public void testSendEmail() throws Exception { DevEmailService.getInstance().sendEmail("Hi-Munish Test Email", "cancerian0684@gmail.com", "<h3>Hello world</h3> this is a test email", Collections.<File>emptyList()); }
public void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments) { Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password")); } }); try { SMTPMessage message = new SMTPMessage(session); message.setFrom(new InternetAddress(properties.getProperty("mail.sender"))); if (commaSeparatedRecipients == null || commaSeparatedRecipients.isEmpty()) commaSeparatedRecipients = properties.getProperty("author.email"); message.addRecipients(Message.RecipientType.TO, getAddresses(commaSeparatedRecipients)); message.setSubject(subject); message.saveChanges(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader("Content-Type", "text/html; charset=UTF-8"); messageBodyPart.setText(body, "UTF-8", "html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for (File file : attachments) { if (file == null || !file.exists()) continue; messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(file.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); message.setSendPartial(true); Transport.send(message); System.out.println("Sent Expense successfully...."); } catch (MessagingException mex) { throw new RuntimeException(mex); } }
DevEmailService { public void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments) { Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password")); } }); try { SMTPMessage message = new SMTPMessage(session); message.setFrom(new InternetAddress(properties.getProperty("mail.sender"))); if (commaSeparatedRecipients == null || commaSeparatedRecipients.isEmpty()) commaSeparatedRecipients = properties.getProperty("author.email"); message.addRecipients(Message.RecipientType.TO, getAddresses(commaSeparatedRecipients)); message.setSubject(subject); message.saveChanges(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader("Content-Type", "text/html; charset=UTF-8"); messageBodyPart.setText(body, "UTF-8", "html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for (File file : attachments) { if (file == null || !file.exists()) continue; messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(file.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); message.setSendPartial(true); Transport.send(message); System.out.println("Sent Expense successfully...."); } catch (MessagingException mex) { throw new RuntimeException(mex); } } }
DevEmailService { public void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments) { Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password")); } }); try { SMTPMessage message = new SMTPMessage(session); message.setFrom(new InternetAddress(properties.getProperty("mail.sender"))); if (commaSeparatedRecipients == null || commaSeparatedRecipients.isEmpty()) commaSeparatedRecipients = properties.getProperty("author.email"); message.addRecipients(Message.RecipientType.TO, getAddresses(commaSeparatedRecipients)); message.setSubject(subject); message.saveChanges(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader("Content-Type", "text/html; charset=UTF-8"); messageBodyPart.setText(body, "UTF-8", "html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for (File file : attachments) { if (file == null || !file.exists()) continue; messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(file.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); message.setSendPartial(true); Transport.send(message); System.out.println("Sent Expense successfully...."); } catch (MessagingException mex) { throw new RuntimeException(mex); } } private  DevEmailService(); }
DevEmailService { public void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments) { Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password")); } }); try { SMTPMessage message = new SMTPMessage(session); message.setFrom(new InternetAddress(properties.getProperty("mail.sender"))); if (commaSeparatedRecipients == null || commaSeparatedRecipients.isEmpty()) commaSeparatedRecipients = properties.getProperty("author.email"); message.addRecipients(Message.RecipientType.TO, getAddresses(commaSeparatedRecipients)); message.setSubject(subject); message.saveChanges(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader("Content-Type", "text/html; charset=UTF-8"); messageBodyPart.setText(body, "UTF-8", "html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for (File file : attachments) { if (file == null || !file.exists()) continue; messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(file.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); message.setSendPartial(true); Transport.send(message); System.out.println("Sent Expense successfully...."); } catch (MessagingException mex) { throw new RuntimeException(mex); } } private  DevEmailService(); static DevEmailService getInstance(); static void main(String[] args); void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments); void sendEmail(String subject, String commaSeparatedRecipients, String cc, String body, List<byte[]> attachments, List<String> fileNames); InternetAddress[] getAddresses(String commaSeparatedRecipients); }
DevEmailService { public void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments) { Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password")); } }); try { SMTPMessage message = new SMTPMessage(session); message.setFrom(new InternetAddress(properties.getProperty("mail.sender"))); if (commaSeparatedRecipients == null || commaSeparatedRecipients.isEmpty()) commaSeparatedRecipients = properties.getProperty("author.email"); message.addRecipients(Message.RecipientType.TO, getAddresses(commaSeparatedRecipients)); message.setSubject(subject); message.saveChanges(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader("Content-Type", "text/html; charset=UTF-8"); messageBodyPart.setText(body, "UTF-8", "html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for (File file : attachments) { if (file == null || !file.exists()) continue; messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(file.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); message.setSendPartial(true); Transport.send(message); System.out.println("Sent Expense successfully...."); } catch (MessagingException mex) { throw new RuntimeException(mex); } } private  DevEmailService(); static DevEmailService getInstance(); static void main(String[] args); void sendEmail(String subject, String commaSeparatedRecipients, String body, List<File> attachments); void sendEmail(String subject, String commaSeparatedRecipients, String cc, String body, List<byte[]> attachments, List<String> fileNames); InternetAddress[] getAddresses(String commaSeparatedRecipients);  }