Example usage for javax.mail.internet MimeMessage toString

List of usage examples for javax.mail.internet MimeMessage toString

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:br.ufc.ivela.commons.mail.MailService.java

public List<MimeMessage> send(String[] to, String from, String subject, String velocityTemplate, Map[] params)
        throws MessagingException {
    if (to == null || to.length <= 0) {
        throw new IllegalArgumentException("You cannot send an email without recipients");
    }//from   www .  ja  va2s  .c o m

    if (from == null || from.isEmpty()) {
        throw new IllegalArgumentException("You cannot send an email without a sender");
    }

    if (subject == null || subject.isEmpty()) {
        throw new IllegalArgumentException("You cannot send an email without a subject");
    }

    if (params == null || params.length <= 0) {
        params = new Map[1];
        params[0] = new HashMap();
    }

    List<MimeMessage> messagesNotSent = new ArrayList<MimeMessage>(to.length);

    MimeMessage[] messagesToSend = new MimeMessage[to.length];

    if (disabled)
        return messagesNotSent;

    for (int i = 0; i < to.length; i++) {
        Map param = i < params.length ? params[i] : params[params.length - 1];
        messagesToSend[i] = createMessage(to[i], from, subject, velocityTemplate, param);
    }

    for (MimeMessage message : messagesToSend) {
        try {
            mailSender.send(message);
        } catch (Exception e) {
            log.error("Error sending Email to:" + message.toString(), e);
            messagesNotSent.add(message);
        }
    }

    return messagesNotSent;
}

From source file:org.smigo.config.DevelopmentConfiguration.java

@Bean
public JavaMailSenderImpl javaMailSender() {
    return new JavaMailSenderImpl() {
        @Override// w w w . j  a v  a  2 s .  c  om
        public String getDefaultEncoding() {
            return "UTF-8";
        }

        @Override
        public void send(SimpleMailMessage simpleMessage) throws MailException {
            try {
                //Thread.sleep(2000);
                String text = simpleMessage.getText();
                String subject = simpleMessage.getSubject();
                FileUtils.writeStringToFile(MAIL_FILE, text, Charset.defaultCharset());
                FileUtils.writeStringToFile(MAIL_FILE_TXT, subject + System.lineSeparator() + text,
                        Charset.defaultCharset());
            } catch (Exception e) {
                throw new MailSendException("Error sending email to " + simpleMessage.getFrom(), e);
            }
        }

        @Override
        public void send(MimeMessage mimeMessage) throws MailException {
            try {
                final String s = IOUtils.toString(mimeMessage.getInputStream());
                FileUtils.writeStringToFile(MAIL_FILE, s, Charset.defaultCharset());
            } catch (IOException | MessagingException e) {
                throw new MailSendException("Error sending email: " + mimeMessage.toString(), e);
            }
        }

        @Override
        public void send(SimpleMailMessage[] simpleMessages) throws MailException {
            throw new MailPreparationException("Method not supported");
        }
    };
}

From source file:EmailNotificationTest.java

public void setMailMock() {
    // setup mail sender mock invocations
    reset(this.javaMailSender);
    when(this.javaMailSender.createMimeMessage()).thenCallRealMethod();

    Answer dumpMessage = new Answer() {
        @Override/*from   w  w  w .  j a va  2s .  c o m*/
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object arg = invocation.getArguments()[0];
            if (arg instanceof MimeMessage) {
                MimeMessage m = (MimeMessage) arg;
                System.out.println("EMAIL DUMP:");
                m.writeTo(System.out);
            } else if (arg instanceof SimpleMailMessage) {
                SimpleMailMessage m = (SimpleMailMessage) arg;
                System.out.println("EMAIL DUMP:");
                System.out.println(m.toString());
            } else {
                throw new Error("Could not print email: " + arg);
            }
            return null;
        }
    };
    doAnswer(dumpMessage).when(this.javaMailSender).send(any(MimeMessage.class));
    doAnswer(dumpMessage).when(this.javaMailSender).send(any(SimpleMailMessage.class));
}

From source file:org.toobsframework.email.SmtpMailSender.java

public void sendEmail(EmailBean email) throws MailException, MessagingException {
    JavaMailSender javaMailSender = (JavaMailSender) javaMailSenders.get(email.getMailSenderKey());
    if (javaMailSender == null) {
        throw new MessagingException(email.getMailSenderKey() + " is an invalid mailSenderKey");
    }/*from  w ww . ja  v  a2s . co m*/
    if (this.getMailProperties() != null) {
        ((JavaMailSenderImpl) javaMailSender).setJavaMailProperties(this.getMailProperties());
    }
    try {
        String[] recipients = this.processRecipients(email.getRecipients());
        String[] bccs = new String[email.getBccs().size()];
        for (int i = 0; i < recipients.length; i++) {
            MimeMessage message = null;
            MimeMessageHelper helper = null;
            String thisRecipient = recipients[i];
            switch (email.getType()) {
            case EmailBean.MESSAGE_TYPE_TEXT:
                message = javaMailSender.createMimeMessage();
                helper = new MimeMessageHelper(message, false, "us-ascii");
                helper.setSubject(email.getEmailSubject());
                helper.setFrom(email.getEmailSender());

                helper.setTo(thisRecipient);
                helper.setBcc((String[]) email.getBccs().toArray(bccs));
                helper.setText(email.getMessageText(), false);
                log.info("Sending message " + message.toString());
                javaMailSender.send(message);
                break;
            case EmailBean.MESSAGE_TYPE_HTML:
                message = javaMailSender.createMimeMessage();
                helper = new MimeMessageHelper(message, true, "us-ascii");
                helper.setSubject(email.getEmailSubject());
                helper.setFrom(email.getEmailSender());

                helper.setTo(thisRecipient);
                helper.setBcc((String[]) email.getBccs().toArray(bccs));
                helper.setText(email.getMessageText(), email.getMessageHtml());
                log.info("Sending message " + message.toString());
                javaMailSender.send(message);
                break;
            }
        }
    } catch (Exception e) {
        log.error("Mail exception " + e.getMessage(), e);
        throw new MessagingException(e.getMessage());
    }
}

From source file:br.ufc.ivela.commons.mail.MailService.java

public List<MimeMessage> send(String[] to, String from, String subject, String content)
        throws MessagingException {

    if (to == null || to.length <= 0) {
        throw new IllegalArgumentException("You cannot send an email without recipients");
    }/* w w  w .  jav  a2s  .co m*/

    if (from == null || from.isEmpty()) {
        throw new IllegalArgumentException("You cannot send an email without a sender");
    }

    if (subject == null || subject.isEmpty()) {
        throw new IllegalArgumentException("You cannot send an email without a subject");
    }

    content = content != null ? content : "";

    // Construct the message
    MimeMessage[] messagesToSend = new MimeMessage[to.length];

    for (int i = 0; i < to.length; i++) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setTo(to[i]);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(content, true);
        messagesToSend[i] = message;
    }

    List<MimeMessage> messagesNotSent = new ArrayList<MimeMessage>(messagesToSend.length);

    if (disabled)
        return messagesNotSent;

    for (MimeMessage message : messagesToSend) {
        try {
            this.mailSender.send(message);
        } catch (Exception e) {
            log.error("Error sending Email to:" + message.toString(), e);
            messagesNotSent.add(message);
        }
    }

    return messagesNotSent;
}