带有文本和附件的WSO2 ESB电子邮件

时间:2014-12-09 16:14:16

标签: wso2 wso2esb

我正在尝试从我的代理服务发送和发送电子邮件。电子邮件已发送但仅附件。邮件正文未发送。我的要求是在附件中发送包含正文和错误详细信息的电子邮件。

我的代理代码是:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="EmailSender"
       transports="jms"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log/>
         <property name="transport.mail.Format" value="Attachment" scope="axis2"/>
         <property name="AttachmentFile" value="Error.txt" scope="axis2"/>
         <property name="messageType"
                   value="text/html"
                   scope="axis2"
                   type="STRING"/>
         <property name="ContentType"
                   value="text/html"
                   scope="axis2"
                   type="STRING"/>
         <property name="Subject"
                   value="GLBookingError"
                   scope="transport"
                   type="STRING"/>
         <property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
         <property name="FORCE_SC_ACCEPTED"
                   value="true"
                   scope="axis2"
                   type="STRING"/>
         <property name="ClientApiNonBlocking" scope="axis2" action="remove"/>
         <payloadFactory media-type="xml">
            <format>
               <Error xmlns="">
   $1
</Error><Body>$2</Body>
            </format>
            <args>
               <arg evaluator="xml" expression="$body"/>
               <arg value="Dear user,Unfortunately an error occurred within the WSO2 ESB environment.The specific details of this error can be found within the attachment. Best regards WSO2 ESB Support Team"/>
            </args>
         </payloadFactory>
         <send>
            <endpoint key="conf:/endpoints/EmailSender_EPLOCAL.xml"/>
         </send>
      </inSequence>
      <outSequence/>
      <faultSequence>
         <log separator="..... Email Sender Fault Sequence....."
              description="INFOLOG"/>
         <log level="custom"
              category="ERROR"
              separator="EmailSender-ErrorLog"
              description="EmailErrorLog">
            <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
            <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
            <property name="ERROR_DETAIL" expression="get-property('ERROR_DETAIL')"/>
            <property name="ERROR_EXCEPTION" expression="get-property('ERROR_EXCEPTION')"/>
         </log>
         <property name="SET_ROLLBACK_ONLY"
                   value="true"
                   scope="default"
                   type="STRING"
                   description="RollBack"/>
      </faultSequence>
   </target>
   <parameter name="transport.jms.ContentType">
      <rules>
         <jmsProperty>contentType</jmsProperty>
         <default>application/xml</default>
      </rules>
   </parameter>
   <parameter name="transport.jms.ConnectionFactory">myQueueConnectionFactory</parameter>
   <parameter name="transport.jms.DestinationType">queue</parameter>
   <parameter name="transport.jms.Destination">ErrorQueue.01</parameter>
   <description/>
</proxy>

第一个参数是Error附件,第二个参数是body。目前它正在发送带有两个参数的附件。

1 个答案:

答案 0 :(得分:2)

在分析了WSO2 ESB的内部代码之后,我发现当我们尝试发送多部分消息时,WSO2已经放置了硬编码字符串(Web服务附件)消息。如果您想要自定义消息,请编辑源代码并重新编译jar。

你可以在{WSO2 ESB_HOME}/repository/components/patches/patch000x/axis2_1.6.1.wso2v10.jar.找到相应的罐子 (上述路径特定于WSO2 ESB 4.8.0)

在这个罐子里转到/org/apache/axis2/transport/mail/MailTransportSender.java。编辑此文件并在上面的位置替换修补的jar。

以下是sendEmail函数的代码:(带****的消息是我的自定义消息)

private String sendMail(MailOutTransportInfo outInfo, MessageContext msgContext)
    throws AxisFault, MessagingException, IOException
  {
    OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);

    format.setProperty("org.apache.axiom.om.OMFormat.use.cteBase64.forNonTextualAttachments", Boolean.valueOf(true));

    MessageFormatter messageFormatter = BaseUtils.getMessageFormatter(msgContext);

    if (this.log.isDebugEnabled()) {
      this.log.debug("Creating MIME message using message formatter " + 
        messageFormatter.getClass().getSimpleName());
    }

    WSMimeMessage message = null;
    if (outInfo.getFromAddress() != null)
      message = new WSMimeMessage(this.session, outInfo.getFromAddress().getAddress());
    else {
      message = new WSMimeMessage(this.session, "");
    }

    Map trpHeaders = (Map)msgContext.getProperty("TRANSPORT_HEADERS");
    if ((this.log.isDebugEnabled()) && (trpHeaders != null)) {
      this.log.debug("Using transport headers: " + trpHeaders);
    }

    if ((outInfo.getTargetAddresses() != null) && (outInfo.getFromAddress() != null)) {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting From header to " + outInfo.getFromAddress().getAddress() + 
          " from OutTransportInfo");
      }
      message.setFrom(outInfo.getFromAddress());
      message.setReplyTo(new Address[] { outInfo.getFromAddress() });
    } else if ((trpHeaders != null) && (trpHeaders.containsKey("From"))) {
      InternetAddress from = 
        new InternetAddress((String)trpHeaders.get("From"));
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting From header to " + from.getAddress() + 
          " from transport headers");
      }
      message.setFrom(from);
      message.setReplyTo(new Address[] { from });
    }
    else if (this.smtpFromAddress != null) {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting From header to " + this.smtpFromAddress.getAddress() + 
          " from transport configuration");
      }
      message.setFrom(this.smtpFromAddress);
      message.setReplyTo(new Address[] { this.smtpFromAddress });
    } else {
      handleException("From address for outgoing message cannot be determined");
    }

    if ((trpHeaders != null) && (trpHeaders.containsKey("To"))) {
      Address[] to = 
        InternetAddress.parse((String)trpHeaders.get("To"));
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting To header to " + InternetAddress.toString(to) + 
          " from transport headers");
      }
      message.setRecipients(Message.RecipientType.TO, to);
    } else if (outInfo.getTargetAddresses() != null) {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting To header to " + InternetAddress.toString(
          outInfo.getTargetAddresses()) + " from OutTransportInfo");
      }
      message.setRecipients(Message.RecipientType.TO, outInfo.getTargetAddresses());
    } else {
      handleException("To address for outgoing message cannot be determined");
    }

    if ((trpHeaders != null) && (trpHeaders.containsKey("Cc"))) {
      Address[] cc = 
        InternetAddress.parse((String)trpHeaders.get("Cc"));
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting Cc header to " + InternetAddress.toString(cc) + 
          " from transport headers");
      }
      message.setRecipients(Message.RecipientType.CC, cc);
    } else if (outInfo.getCcAddresses() != null) {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting Cc header to " + InternetAddress.toString(
          outInfo.getCcAddresses()) + " from OutTransportInfo");
      }
      message.setRecipients(Message.RecipientType.CC, outInfo.getCcAddresses());
    }

    if ((trpHeaders != null) && (trpHeaders.containsKey("Bcc"))) {
      InternetAddress[] bcc = 
        InternetAddress.parse((String)trpHeaders.get("Bcc"));
      if (this.log.isDebugEnabled()) {
        this.log.debug("Adding Bcc header values " + InternetAddress.toString(bcc) + 
          " from transport headers");
      }
      message.addRecipients(Message.RecipientType.BCC, bcc);
    }
    if (this.smtpBccAddresses != null) {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Adding Bcc header values " + InternetAddress.toString(this.smtpBccAddresses) + 
          " from transport configuration");
      }
      message.addRecipients(Message.RecipientType.BCC, this.smtpBccAddresses);
    }

    if ((trpHeaders != null) && (trpHeaders.containsKey("Subject"))) {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting Subject header to '" + trpHeaders.get(
          "Subject") + "' from transport headers");
      }
      message.setSubject((String)trpHeaders.get("Subject"));
    } else if (outInfo.getSubject() != null) {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Setting Subject header to '" + outInfo.getSubject() + 
          "' from transport headers");
      }
      message.setSubject(outInfo.getSubject());
    } else {
      if (this.log.isDebugEnabled()) {
        this.log.debug("Generating default Subject header from SOAP action");
      }
      message.setSubject("SOAPAction: " + msgContext.getSoapAction());
    }

    if (outInfo.getRequestMessageID() != null) {
      message.setHeader("In-Reply-To", outInfo.getRequestMessageID());
      message.setHeader("References", outInfo.getRequestMessageID());
    }
    else {
      if ((trpHeaders != null) && 
        (trpHeaders.containsKey("In-Reply-To"))) {
        message.setHeader("In-Reply-To", 
          (String)trpHeaders.get("In-Reply-To"));
      }
      if ((trpHeaders != null) && (trpHeaders.containsKey("References"))) {
        message.setHeader("References", 
          (String)trpHeaders.get("References"));
      }

    }

    message.setSentDate(new Date());

    message.setHeader("SOAPAction", msgContext.getSoapAction());
    MessageFormatterEx messageFormatterEx;
    MessageFormatterEx messageFormatterEx;
    if ((messageFormatter instanceof MessageFormatterEx))
      messageFormatterEx = (MessageFormatterEx)messageFormatter;
    else {
      messageFormatterEx = new MessageFormatterExAdapter(messageFormatter);
    }

    DataHandler dataHandler = new DataHandler(messageFormatterEx.getDataSource(msgContext, format, msgContext.getSoapAction()));

    MimeMultipart mimeMultiPart = null;

    String mFormat = (String)msgContext.getProperty("transport.mail.Format");
    if (mFormat == null) {
      mFormat = this.defaultMailFormat;
    }

    if (this.log.isDebugEnabled())
      this.log.debug("Using mail format '" + mFormat + "'");
    MimePart mainPart;
    String fileName;
    MimePart mainPart;
    if ("Multipart".equals(mFormat)) {
      mimeMultiPart = new MimeMultipart();
      MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
      mimeBodyPart1.setContent("*****Dear user,\r\n \r\nUnfortunately an error occurred within the WSO2 ESB environment.****", "text/plain");
      MimeBodyPart mimeBodyPart2 = new MimeBodyPart();
      mimeMultiPart.addBodyPart(mimeBodyPart1);
      mimeMultiPart.addBodyPart(mimeBodyPart2);
      message.setContent(mimeMultiPart);
      mainPart = mimeBodyPart2;
    }
    else
    {
      MimePart mainPart;
      if ("Attachment".equals(mFormat)) {
        mimeMultiPart = new MimeMultipart();
        MimeBodyPart mimeBodyPart1 = new MimeBodyPart();

        mimeBodyPart1.setContent("****Dear user,\r\n \r\nUnfortunately an error occurred within the WSO2 ESB environment.*****", "text/plain");
        MimeBodyPart mimeBodyPart2 = new MimeBodyPart();
        mimeMultiPart.addBodyPart(mimeBodyPart1);
        mimeMultiPart.addBodyPart(mimeBodyPart2);
        message.setContent(mimeMultiPart);

        fileName = (String)msgContext.getProperty(
          "AttachmentFile");
        if (fileName != null)
          mimeBodyPart2.setFileName(fileName);
        else {
          mimeBodyPart2.setFileName("attachment");
        }

        mainPart = mimeBodyPart2;
      } else {
        mainPart = message;
      }
    }
    try {
      mainPart.setHeader("SOAPAction", msgContext.getSoapAction());
      mainPart.setDataHandler(dataHandler);

      if (msgContext.getOptions().getProperty("Content-Transfer-Encoding") != null) {
        mainPart.setHeader("Content-Transfer-Encoding", 
          (String)msgContext.getOptions().getProperty("Content-Transfer-Encoding"));
      } else {
        String contentType = dataHandler.getContentType().toLowerCase();
        if ((!contentType.startsWith("multipart/")) && (CommonUtils.isTextualPart(contentType))) {
          mainPart.setHeader("Content-Transfer-Encoding", "quoted-printable");
        }

      }

      if (msgContext.getOptions().getProperty("transport.mail.custom.headers") != null) {
        Map customTransportHeaders = (Map)msgContext.getOptions().getProperty("transport.mail.custom.headers");
        for (fileName = customTransportHeaders.keySet().iterator(); fileName.hasNext(); ) { Object header = fileName.next();
          mainPart.setHeader((String)header, (String)customTransportHeaders.get(header));
        }

      }

      this.log.debug("Sending message");
      Transport.send(message);

      this.metrics.incrementMessagesSent(msgContext);
      long bytesSent = message.getBytesSent();
      if (bytesSent != -1L)
        this.metrics.incrementBytesSent(msgContext, bytesSent);
    }
    catch (MessagingException e)
    {
      this.metrics.incrementFaultsSending();
      handleException("Error creating mail message or sending it to the configured server", e);
    }

    return message.getMessageID();
  }