Package org.springframework.mail.javamail

Examples of org.springframework.mail.javamail.MimeMessageHelper


   * @param attachment 附件,该附件为一个map,将key来做文件名,value来做文件的形式发送出去
   */
  private void doSend(String sendTo,String sendFrom,String subject,String content,Map<String, File> attachment) {
    try {
      MimeMessage msg = mailSender.createMimeMessage();
      MimeMessageHelper helper = new MimeMessageHelper(msg, true, encoding);

      helper.setTo(sendTo);
      helper.setFrom(sendFrom);
      helper.setSubject(subject);
      helper.setText(content, true);

      if (!MapUtils.isEmpty(attachment)) {
        for (Entry<String, File> entry : attachment.entrySet()) {
          helper.addAttachment(entry.getKey(), entry.getValue());
        }
      }

      mailSender.send(msg);
      logger.info("邮件发送成功");
View Full Code Here


   
    logger.info("Preparando envio de e-mail...");
   
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
      public void prepare(MimeMessage mimeMessage) throws Exception {
        MimeMessageHelper MMhelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        MMhelper.setTo(to);
        MMhelper.setFrom(from);
        MMhelper.setSubject(subject);
        MMhelper.setText(text, true);
      }
    };
   
    try {
      logger.info("Enviando e-mail...");
View Full Code Here

                            String subject, String attachmentName)
    throws MessagingException {
        MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();

        // use the true flag to indicate you need a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setTo(recipients);

        // use the default sending if no sender specified
        if (sender == null) {
            helper.setFrom(defaultFrom);
        } else {
           helper.setFrom(sender);
        }

        helper.setText(bodyText);
        helper.setSubject(subject);

        helper.addAttachment(attachmentName, resource);

        ((JavaMailSenderImpl) mailSender).send(message);
    }
View Full Code Here

  protected void doTask(final Map<Object, Object> context) {
    final String from = ((JavaMailSenderImpl)mailSender).getUsername();
    MimeMessagePreparator preparator = new MimeMessagePreparator(){
      public void prepare(MimeMessage mimeMessage) throws Exception {
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "UTF-8");
        // default from use @code JavaMailSender, @code AbstractEmailTask Implementer can override it
        helper.setFrom(from);
        prepareMessage(helper, context);
      }
    };
    mailSender.send(preparator);
  }
View Full Code Here

*/
public class SimpleHtmlMessageSender extends AbstractMessageSender {

  public void sendMessage() throws MessagingException {
    MimeMessage msg = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(msg);

    helper.setTo(to);
    helper.setFrom(from);
    helper.setSubject(subject);
    helper.setText("<html><head></head><body><h1>Hello World!"
        + "</h1></body></html>", true);

    sender.send(msg);
  }
View Full Code Here

*/
public class InlineImageMessageSender extends AbstractMessageSender {

  public void sendMessage() throws MessagingException {
    MimeMessage msg = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(msg, true);

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

    helper.setText("<html><head></head><body><h1>Hello World!</h1>"
        + "<img src=\"cid:abc\"></body></html>", true);

    // add the image
    FileSystemResource img = new FileSystemResource(new File(
        "./ch15/src/images/apress.gif"));
    helper.addInline("abc", img);

    sender.send(msg);
  }
View Full Code Here

*/
public class AttachmentMessageSender extends AbstractMessageSender {

  public void sendMessage() throws MessagingException {
    MimeMessage msg = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(msg, true);

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

    helper.setText(
        "<html><head></head><body><h1>Hello World!</h1></body></html>",
        true);

    // add the image
    FileSystemResource img = new FileSystemResource(new File(
        "./ch15/src/images/apress.gif"));
    helper.addAttachment("apress.gif", img);

    sender.send(msg);
  }
View Full Code Here

  @Override
  public final MimeMessage compose(MimeMessage mimeMessage, C context) throws MailPreparationException {

    // create a mime message helper
    MimeMessageHelper helper;
    try {
      helper = new MimeMessageHelper(mimeMessage, true, context.getEncoding());
    }
    catch(final MessagingException me) {
      throw new MailPreparationException("Unable to create the mime message helper", me);
    }

    // apply the routing
    final MailRouting mailRouting = context.getRouting();
    if(mailRouting == null) {
      throw new MailPreparationException("No email routing specified.");
    }
    try {
      // sender
      final NameEmail sender = mailRouting.getSender();
      if(sender != null) {
        helper.setFrom(sender.getEmailAddress(), sender.getName());
      }

      List<NameEmail> list;

      // recipients
      list = mailRouting.getRecipients();
      if(list.isEmpty()) {
        throw new MailPreparationException("No email recipients specified");
      }
      for(final NameEmail email : list) {
        helper.addTo(email.getEmailAddress(), email.getName());
      }

      // ccs
      list = mailRouting.getCcList();
      if(list != null && list.size() > 0) {
        for(final NameEmail email : list) {
          helper.addCc(email.getEmailAddress(), email.getName());
        }
      }

      // bccs
      list = mailRouting.getBccList();
      if(list != null && list.size() > 0) {
        for(final NameEmail email : list) {
          helper.addBcc(email.getEmailAddress(), email.getName());
        }
      }

    }
    catch(final UnsupportedEncodingException uee) {
      throw new MailPreparationException("Unsupported mime message encoding: " + uee.getMessage(), uee);
    }
    catch(final MessagingException me) {
      throw new MailPreparationException("Trouble performing the initial mime message compose:" + me.getMessage(), me);
    }

    // do implemenation specific composing
    composeImpl(helper, context);

    // add attachments
    try {
      final List<Attachment> attachments = context.getAttachments();
      if(!attachments.isEmpty()) {
        for(final Attachment attachment : attachments) {
          helper.addAttachment(attachment.getName(), attachment.getDataSource());
        }
      }

    }
    catch(final MessagingException me) {
      throw new MailPreparationException("Unable to add email attachments:" + me.getMessage(), me);
    }

    return helper.getMimeMessage();
  }
View Full Code Here

  protected abstract void composeImpl(MimeMessageHelper helper, C context) throws MailPreparationException;

  public final MimeMessage compose(MimeMessage mimeMessage, C context) throws MailPreparationException {

    // create a mime message helper
    MimeMessageHelper helper;
    try {
      helper = new MimeMessageHelper(mimeMessage, true, context.getEncoding());
    }
    catch(final MessagingException me) {
      throw new MailPreparationException("Unable to create the mime message helper", me);
    }

    // apply the routing
    final MailRouting mailRouting = context.getRouting();
    if(mailRouting == null) {
      throw new MailPreparationException("No email routing specified.");
    }
    try {
      // sender
      final NameEmail sender = mailRouting.getSender();
      if(sender != null) {
        helper.setFrom(sender.getEmailAddress(), sender.getName());
      }

      List<NameEmail> list;

      // recipients
      list = mailRouting.getRecipients();
      if(list.isEmpty()) {
        throw new MailPreparationException("No email recipients specified");
      }
      for(final NameEmail email : list) {
        helper.addTo(email.getEmailAddress(), email.getName());
      }

      // ccs
      list = mailRouting.getCcList();
      if(list != null && list.size() > 0) {
        for(final NameEmail email : list) {
          helper.addCc(email.getEmailAddress(), email.getName());
        }
      }

      // bccs
      list = mailRouting.getBccList();
      if(list != null && list.size() > 0) {
        for(final NameEmail email : list) {
          helper.addBcc(email.getEmailAddress(), email.getName());
        }
      }

    }
    catch(final UnsupportedEncodingException uee) {
      throw new MailPreparationException("Unsupported mime message encoding: " + uee.getMessage(), uee);
    }
    catch(final MessagingException me) {
      throw new MailPreparationException("Trouble performing the initial mime message compose:" + me.getMessage(), me);
    }

    // do implemenation specific composing
    composeImpl(helper, context);

    // add attachments
    try {
      final List<Attachment> attachments = context.getAttachments();
      if(!attachments.isEmpty()) {
        for(final Attachment attachment : attachments) {
          helper.addAttachment(attachment.getName(), attachment.getDataSource());
        }
      }

    }
    catch(final MessagingException me) {
      throw new MailPreparationException("Unable to add email attachments:" + me.getMessage(), me);
    }

    return helper.getMimeMessage();
  }
View Full Code Here

                    if (StringUtils.isNotBlank(smtpPassword)) {
                        sender.setPassword(smtpPassword);
                    }

                    MimeMessage message = sender.createMimeMessage();
                    MimeMessageHelper helper = new MimeMessageHelper(message, true);
                    helper.setTo(to);
                    helper.setFrom(task.getSender());
                    helper.setSubject(task.getSubject());
                    helper.setText(task.getTextBody(), task.getHtmlBody());

                    sender.send(message);

                    execution.setStatus(Status.SENT.name());
View Full Code Here

TOP

Related Classes of org.springframework.mail.javamail.MimeMessageHelper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.