Esempio n. 1
0
  public void sendMail(String plainText, String htmlText, String attachment) {

    MimeMessage message = mailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = new MimeMessageHelper(message, true);
      helper.setFrom(simpleMailMessage.getFrom());
      helper.setTo(simpleMailMessage.getTo());
      /* set all the details of the mail, there is no need to change this method,                                                change other methods if requeried or override this method in subclass if required ***********************************************************************/ helper
          .setBcc("*****@*****.**");
      /* plantext null will not work on plain html*/
      helper.setSubject(simpleMailMessage.getSubject());
      helper.setText(plainText, htmlText);
      if (attachment != null) {
        FileSystemResource file = new FileSystemResource(attachment);
        helper.addAttachment(file.getFilename(), file);
      }
      mailSender.send(message);
    } catch (MessagingException e) {
      System.out.print(e.getMessage());
      logger.error("Exception in Method:sendMail", e);
    } catch (Exception ex) {
      System.out.print(ex.getMessage());
      logger.error("Exception in Method:sendMail", ex);
    }
  }
Esempio n. 2
0
  @Test
  public void testSendMessageWithAttachment() throws Exception {
    final String ATTACHMENT_NAME = "boring-attachment.txt";

    // mock smtp server
    Wiser wiser = new Wiser();
    int port = 2525 + (int) (Math.random() * 100);
    mailSender.setPort(port);
    wiser.setPort(port);
    wiser.start();

    Date dte = new Date();
    String emailSubject = "grepster testSendMessageWithAttachment: " + dte;
    String emailBody = "Body of the grepster testSendMessageWithAttachment message sent at: " + dte;

    ClassPathResource cpResource = new ClassPathResource("/test-attachment.txt");
    // a null from should work
    mailEngine.sendMessage(
        new String[] {"*****@*****.**"}, null, cpResource, emailBody, emailSubject, ATTACHMENT_NAME);

    mailEngine.sendMessage(
        new String[] {"*****@*****.**"},
        mailMessage.getFrom(),
        cpResource,
        emailBody,
        emailSubject,
        ATTACHMENT_NAME);

    wiser.stop();
    // one without and one with from
    assertTrue(wiser.getMessages().size() == 2);

    WiserMessage wm = wiser.getMessages().get(0);
    MimeMessage mm = wm.getMimeMessage();

    Object o = wm.getMimeMessage().getContent();
    assertTrue(o instanceof MimeMultipart);
    MimeMultipart multi = (MimeMultipart) o;
    int numOfParts = multi.getCount();

    boolean hasTheAttachment = false;
    for (int i = 0; i < numOfParts; i++) {
      BodyPart bp = multi.getBodyPart(i);
      String disp = bp.getDisposition();
      if (disp == null) { // the body of the email
        Object innerContent = bp.getContent();
        MimeMultipart innerMulti = (MimeMultipart) innerContent;
        assertEquals(emailBody, innerMulti.getBodyPart(0).getContent());
      } else if (disp.equals(Part.ATTACHMENT)) { // the attachment to the email
        hasTheAttachment = true;
        assertEquals(ATTACHMENT_NAME, bp.getFileName());
      } else {
        fail("Did not expect to be able to get here.");
      }
    }
    assertTrue(hasTheAttachment);
    assertEquals(emailSubject, mm.getSubject());
  }
Esempio n. 3
0
  protected String getMailHeader(SimpleMailMessage mailMessage) {
    StringBuffer header = new StringBuffer();

    header.append("To: ");
    for (String to : mailMessage.getTo()) {
      header.append(to).append(";");
    }
    header.append("\nFrom: " + mailMessage.getFrom());
    header.append("\nSubject: " + mailMessage.getSubject());
    return header.toString();
  }
 private void logMail(SimpleMailMessage msg) {
   log.info(
       "Mail Test-Mode: Would send mail to="
           + arrayToString(msg.getTo())
           + " with subject="
           + msg.getSubject());
   log.debug(
       "Mail Test-Mode: from="
           + msg.getFrom()
           + " , cc="
           + msg.getCc()
           + " , reply-to="
           + msg.getReplyTo());
   log.debug("Mail Test-Mode: text='" + msg.getText() + "'");
 }