Example usage for org.springframework.mail MailParseException MailParseException

List of usage examples for org.springframework.mail MailParseException MailParseException

Introduction

In this page you can find the example usage for org.springframework.mail MailParseException MailParseException.

Prototype

public MailParseException(Throwable cause) 

Source Link

Document

Constructor for MailParseException.

Usage

From source file:org.revo.controller.Mymail.java

public void sendMail(String from, String to, String subject, String contents) {

    MimeMessage message = mailSender.createMimeMessage();

    try {//  w  w w  .  j a  v a 2 s  .  co m
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(contents);

    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);
}

From source file:com.rinxor.cloud.service.mail.Mailer.java

public void sendMail(String dear, String content) {

    MimeMessage message = mailSender.createMimeMessage();

    try {/*from  ww w  .  j a v a  2s  .  c  o  m*/
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(simpleMailMessage.getFrom());
        helper.setTo(simpleMailMessage.getTo());
        helper.setSubject(simpleMailMessage.getSubject());
        helper.setText(String.format(simpleMailMessage.getText(), dear, content));

        //FileSystemResource file = new FileSystemResource("C:\\log.txt");
        //helper.addAttachment(file.getFilename(), file);
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);
}

From source file:com.rinxor.cloud.service.mail.Mailer.java

public void sendMail(String from, String to, String subject, String msg) {

    MimeMessage message = mailSender.createMimeMessage();

    try {//  w ww  .jav  a2 s  .c  o m
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(String.format(msg));

        //FileSystemResource file = new FileSystemResource("C:\\log.txt");
        //helper.addAttachment(file.getFilename(), file);
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);
}

From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java

/**
 * This test will create a Mime Message that contains an Attachment, send it
 * to an SMTP Server (Using Wiser) and retrieve and process the Mime Message.
 *
 * This test verifies that the parsing of the retrieved Mime Message is
 * successful and that the correct number of {@link EmailFragment}s is created.
 *///w ww  .  ja v  a2  s .c om
@Test
public void testProcessingOfEmailAttachments() {

    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);

    final MimeMessage message = mailSender.createMimeMessage();
    final String pictureName = "picture.png";

    final ByteArrayResource byteArrayResource = getFileData(pictureName);

    try {

        final MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom("testfrom@springintegration.org");
        helper.setTo("testto@springintegration.org");
        helper.setSubject("Parsing of Attachments");
        helper.setText("Spring Integration Rocks!");

        helper.addAttachment(pictureName, byteArrayResource, "image/png");

    } catch (MessagingException e) {
        throw new MailParseException(e);
    }

    mailSender.send(message);

    final List<WiserMessage> wiserMessages = wiser.getMessages();

    Assert.assertTrue(wiserMessages.size() == 1);

    boolean foundTextMessage = false;
    boolean foundPicture = false;

    for (WiserMessage wiserMessage : wiserMessages) {

        final List<EmailFragment> emailFragments = new ArrayList<EmailFragment>();

        try {
            final MimeMessage mailMessage = wiserMessage.getMimeMessage();
            EmailParserUtils.handleMessage(null, mailMessage, emailFragments);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving Mime Message.");
        }

        Assert.assertTrue(emailFragments.size() == 2);

        for (EmailFragment emailFragment : emailFragments) {
            if ("picture.png".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }

            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }
        }

        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);

    }
}

From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java

/**
 * This test will create a Mime Message that in return contains another
 * mime message. The nested mime message contains an attachment.
 *
 * The root message consist of both HTML and Text message.
 *
 *///from w w  w  .  j a v  a  2 s . c om
@Test
public void testProcessingOfNestedEmailAttachments() {

    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);

    final MimeMessage rootMessage = mailSender.createMimeMessage();

    try {

        final MimeMessageHelper messageHelper = new MimeMessageHelper(rootMessage, true);

        messageHelper.setFrom("testfrom@springintegration.org");
        messageHelper.setTo("testto@springintegration.org");
        messageHelper.setSubject("Parsing of Attachments");
        messageHelper.setText("Spring Integration Rocks!", "Spring Integration <b>Rocks</b>!");

        final String pictureName = "picture.png";

        final ByteArrayResource byteArrayResource = getFileData(pictureName);

        messageHelper.addInline("picture12345", byteArrayResource, "image/png");

    } catch (MessagingException e) {
        throw new MailParseException(e);
    }

    mailSender.send(rootMessage);

    final List<WiserMessage> wiserMessages = wiser.getMessages();

    Assert.assertTrue(wiserMessages.size() == 1);

    boolean foundTextMessage = false;
    boolean foundPicture = false;
    boolean foundHtmlMessage = false;

    for (WiserMessage wiserMessage : wiserMessages) {

        List<EmailFragment> emailFragments = new ArrayList<EmailFragment>();

        try {

            final MimeMessage mailMessage = wiserMessage.getMimeMessage();
            EmailParserUtils.handleMessage(null, mailMessage, emailFragments);

        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving Mime Message.");
        }

        Assert.assertTrue(emailFragments.size() == 3);

        for (EmailFragment emailFragment : emailFragments) {
            if ("<picture12345>".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }

            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }

            if ("message.html".equals(emailFragment.getFilename())) {
                foundHtmlMessage = true;
            }
        }

        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);
        Assert.assertTrue(foundHtmlMessage);

    }
}

From source file:org.springframework.mail.javamail.JavaMailSenderImpl.java

public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException {
    try {//  ww  w .  jav a2  s  . c  o  m
        List mimeMessages = new ArrayList(mimeMessagePreparators.length);
        for (int i = 0; i < mimeMessagePreparators.length; i++) {
            MimeMessage mimeMessage = createMimeMessage();
            mimeMessagePreparators[i].prepare(mimeMessage);
            mimeMessages.add(mimeMessage);
        }
        send((MimeMessage[]) mimeMessages.toArray(new MimeMessage[mimeMessages.size()]));
    } catch (MailException ex) {
        throw ex;
    } catch (MessagingException ex) {
        throw new MailParseException(ex);
    } catch (IOException ex) {
        throw new MailPreparationException(ex);
    } catch (Exception ex) {
        throw new MailPreparationException(ex);
    }
}