Exemple #1
0
  protected BodyPart getBodyPartForAttachment(DataHandler handler, String name)
      throws MessagingException {
    BodyPart part = new MimeBodyPart();
    part.setDataHandler(handler);
    part.setDescription(name);

    part.setFileName(StringUtils.defaultString(handler.getName(), name));
    return part;
  }
Exemple #2
0
  /**
   * 发送带附件的(HTML)邮件
   *
   * @param entity 待发送的邮件信息
   */
  public boolean sendAffixMail(MailSenderModel entity) {
    // 判断是否需要身份认证
    MyAuthenticator authenticator = null;
    // 如果需要身份认证,则创建一个密码验证器
    if (entity.isValidate()) {
      authenticator = new MyAuthenticator(entity.getUserName(), entity.getPassword());
    }
    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session.getDefaultInstance(entity.getProperties(), authenticator);
    try {
      // 根据session创建一个邮件消息
      Message mailMessage = new MimeMessage(sendMailSession);
      // 创建邮件发送者地址
      Address from = new InternetAddress(entity.getFromAddress());
      // 设置邮件消息的发送者
      mailMessage.setFrom(from);
      // 创建邮件的接收者地址,并设置到邮件消息中
      // Address to = new InternetAddress(entity.getToAddress());
      // Message.RecipientType.TO属性表示接收者的类型为TO
      mailMessage.setRecipients(
          Message.RecipientType.TO, InternetAddress.parse(entity.getToAddress()));
      // 抄送
      if (entity.getCcAddress() != null && !"".equals(entity.getCcAddress())) {
        mailMessage.setRecipients(
            Message.RecipientType.CC, InternetAddress.parse(entity.getCcAddress()));
      }

      // 暗送
      if (entity.getBccAddress() != null && !"".equals(entity.getBccAddress())) {
        mailMessage.setRecipients(
            Message.RecipientType.BCC, InternetAddress.parse(entity.getBccAddress()));
      }
      // 设置邮件消息的主题
      mailMessage.setSubject(entity.getSubject());
      // 设置邮件消息发送的时间
      mailMessage.setSentDate(new Date());
      // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
      Multipart mainPart = new MimeMultipart();
      // 创建一个包含HTML内容的MimeBodyPart
      BodyPart html = new MimeBodyPart();
      // 设置HTML内容
      html.setContent(entity.getContent(), "text/html; charset=" + DEFAULT_ENCODING);
      mainPart.addBodyPart(html);

      // 组装附件
      MimeBodyPart file;
      String[] filePaths = entity.getAttachFileNames();
      FileDataSource file_datasource = null;
      if (filePaths != null && filePaths.length > 0) {
        for (int i = 0; i < filePaths.length; i++) {
          file = new MimeBodyPart();
          file_datasource = new FileDataSource(new File(filePaths[i]));
          DataHandler dh = new DataHandler(file_datasource);
          file.setDataHandler(dh);
          // 附件区别内嵌内容的一个特点是有文件名,为防止中文乱码要编码
          file.setFileName(MimeUtility.encodeText(dh.getName(), DEFAULT_ENCODING, null));
          System.out.println(dh.getName());
          mainPart.addBodyPart(file);
        }
      }

      // 将MiniMultipart对象设置为邮件内容
      mailMessage.setContent(mainPart);

      // 发送邮件
      Transport.send(mailMessage);
      return true;
    } catch (MessagingException ex) {
      ex.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }
 public void uploadModule(DataHandler dh) throws RemoteException {
   ModuleUploadData moduleUploadData = new ModuleUploadData();
   moduleUploadData.setFileName(dh.getName().substring(dh.getName().lastIndexOf('/') + 1));
   moduleUploadData.setDataHandler(dh);
   log.info((moduleAdminServiceStub.uploadModule(new ModuleUploadData[] {moduleUploadData})));
 }
Exemple #4
0
  /*
   * (non-Javadoc)
   *
   * @see org.mule.transformers.AbstractEventAwareTransformer#transform(java.lang.Object,
   *      java.lang.String, org.mule.umo.UMOEventContext)
   */
  public Object transform(Object src, String encoding, UMOEventContext context)
      throws TransformerException {

    String endpointAddress = endpoint.getEndpointURI().getAddress();
    SmtpConnector connector = (SmtpConnector) endpoint.getConnector();
    UMOMessage eventMsg = context.getMessage();

    String to = eventMsg.getStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, endpointAddress);
    String cc =
        eventMsg.getStringProperty(
            MailProperties.CC_ADDRESSES_PROPERTY, connector.getCcAddresses());
    String bcc =
        eventMsg.getStringProperty(
            MailProperties.BCC_ADDRESSES_PROPERTY, connector.getBccAddresses());
    String from =
        eventMsg.getStringProperty(
            MailProperties.FROM_ADDRESS_PROPERTY, connector.getFromAddress());
    String replyTo =
        eventMsg.getStringProperty(
            MailProperties.REPLY_TO_ADDRESSES_PROPERTY, connector.getReplyToAddresses());
    String subject =
        eventMsg.getStringProperty(MailProperties.SUBJECT_PROPERTY, connector.getSubject());
    String contentType =
        eventMsg.getStringProperty(
            MailProperties.CONTENT_TYPE_PROPERTY, connector.getContentType());

    Properties headers = new Properties();
    Properties customHeaders = connector.getCustomHeaders();

    if (customHeaders != null && !customHeaders.isEmpty()) {
      headers.putAll(customHeaders);
    }

    Properties otherHeaders =
        (Properties) eventMsg.getProperty(MailProperties.CUSTOM_HEADERS_MAP_PROPERTY);
    if (otherHeaders != null && !otherHeaders.isEmpty()) {
      Map props = new HashMap(MuleManager.getInstance().getProperties());
      for (Iterator iterator = eventMsg.getPropertyNames().iterator(); iterator.hasNext(); ) {
        String propertyKey = (String) iterator.next();
        props.put(propertyKey, eventMsg.getProperty(propertyKey));
      }
      headers.putAll(templateParser.parse(props, otherHeaders));
    }

    if (logger.isDebugEnabled()) {
      StringBuffer buf = new StringBuffer(256);
      buf.append("Constructing email using:\n");
      buf.append("To: ").append(to);
      buf.append("From: ").append(from);
      buf.append("CC: ").append(cc);
      buf.append("BCC: ").append(bcc);
      buf.append("Subject: ").append(subject);
      buf.append("ReplyTo: ").append(replyTo);
      buf.append("Content type: ").append(contentType);
      buf.append("Payload type: ").append(src.getClass().getName());
      buf.append("Custom Headers: ").append(PropertiesUtils.propertiesToString(headers, false));
      logger.debug(buf.toString());
    }

    try {
      Message email =
          new MimeMessage(
              (Session) endpoint.getConnector().getDispatcher(endpoint).getDelegateSession());

      // set mail details
      email.setRecipients(Message.RecipientType.TO, MailUtils.stringToInternetAddresses(to));
      email.setSentDate(Calendar.getInstance().getTime());
      if (StringUtils.isNotBlank(from)) {
        email.setFrom(MailUtils.stringToInternetAddresses(from)[0]);
      }
      if (StringUtils.isNotBlank(cc)) {
        email.setRecipients(Message.RecipientType.CC, MailUtils.stringToInternetAddresses(cc));
      }
      if (StringUtils.isNotBlank(bcc)) {
        email.setRecipients(Message.RecipientType.BCC, MailUtils.stringToInternetAddresses(bcc));
      }
      if (StringUtils.isNotBlank(replyTo)) {
        email.setReplyTo(MailUtils.stringToInternetAddresses(replyTo));
      }
      email.setSubject(subject);

      for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iterator.next();
        email.setHeader(entry.getKey().toString(), entry.getValue().toString());
      }

      // Create Multipart to put BodyParts in
      Multipart multipart = new MimeMultipart();

      // Create Text Message
      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText("My Text");
      multipart.addBodyPart(messageBodyPart);

      // Create Attachment
      messageBodyPart = new MimeBodyPart();
      DataHandler dataHandler = (DataHandler) src;
      messageBodyPart.setDataHandler(dataHandler);
      messageBodyPart.setFileName(dataHandler.getName());
      multipart.addBodyPart(messageBodyPart);

      email.setContent(multipart);

      return email;
    } catch (Exception e) {
      throw new TransformerException(this, e);
    }
  }