wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Using a web service to send an eMail


In an integration project with other platforms the need arose to send messages that are triggered by a web service. In Domino this is just a few lines of code. Added bonus: the message is fully "embedded experiences" enabled. This is the LotusScript class:
Public Class SendADocumentWithPDFAttachment

    Public sendTo As String
    Public subject As String
    Public textBody As String
    Public htmlBody As String
    Public callBackURL As String
    Public attachmentAsMime As String

    Public Function sendMessage(message As SendADocumentWithPDFAttachment) As String
        Dim s As New NotesSession
        Dim db As NotesDatabase
        Dim doc As NotesDocument
        Dim header As NotesMIMEHeader
        Dim body As NotesMIMEEntity

        On Error Goto Err_sendMessage

        'We are dealing with a MINME message here!
        s.Convertmime = False
        Set db = s.Currentdatabase
        Set doc = db.Createdocument()
        Call doc.Replaceitemvalue("Form", "Memo")
        Call doc.Replaceitemvalue("From", s.Username)
        Call doc.Replaceitemvalue("Subject", message.subject)
        Call doc.Replaceitemvalue("SendTo", message.sendTo)
        'Add other fields as you deem fit

        'Prepare the message body
        Set body = doc.Createmimeentity("Body")
        body.Createheader("Content-Type").Setheaderval("multipart/alternative")
        body.Createheader("MIME-Version").Setheaderval("1.0")
        body.Createheader("Subject").Setheaderval(message.subject)

        'Adding the content
        If message.textBody <> "" Then
            Call Me.addContent(s, body, message.textBody,"text/plain;charset=UTF-8")
        End If  
        If message.htmlBody <> "" Then
            Call Me.addContent(s, body, message.htmlBody,"text/html;charset=UTF-8")
        End If
        If message.callBackURL <> "" Then
            Call Me.addContent(s, body,|{ "url"  : "|+message.callBackURL+|" }|,"application/embed+json;charset=UTF-8")
        End If
        Call doc.Save(True, True)

        'Sending has 2 options: use the send() function or copy the document
        'into the mail.box. We use send here
        Call doc.send(False)
        s.Convertmime = True

        sendMessage = "Message submitted"
Exit_sendMessage:
        Exit Function

Err_sendMessage:
        sendMessage = Error$
        Resume Exit_sendMessage

    End Function

    Private Sub addContent(session As NotesSession, body As NotesMIMEEntity, content As String, contentType As String)
        Dim mime As NotesMIMEEntity
        Dim contentStream As NotesStream
        On Error Goto Err_addContent
        Set contentStream = session.Createstream()
        Set mime = body.Createchildentity()
        Call contentStream.Writetext(content)
        contentStream.Position = 0
        Call mime.setcontentFromText(contentStream,contentType,ENC_BASE64)
Exit_addContent:
        Exit Sub

Err_addContent:
        Print Error$
        Resume Exit_addContent
    End Sub
End Class
##
Once you add that to a Domino web service you get the following web service definition:
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="urn:DefaultNamespace"
              xmlns="http://schemas.xmlsoap.org/wsdl/"
              xmlns:apachesoap="http://xml.apache.org/xml-soap"
              xmlns:impl="urn:DefaultNamespace"
              xmlns:intf="urn:DefaultNamespace"
              xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
              xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
             xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <wsdl:types>
  <schema targetNamespace="urn:DefaultNamespace" xmlns="http://www.w3.org/2001/XMLSchema">
   <complexType name="SendADocumentWithPDFAttachment">
    <sequence/>
   </complexType>
   <element name="MESSAGE" type="impl:SendADocumentWithPDFAttachment"/>
   <element name="SENDMESSAGEReturn" type="xsd:string"/>
  </schema>
 </wsdl:types>
 <message name="SENDMESSAGERequest">
  <part element="impl:MESSAGE" name="MESSAGE"/>
 </message>
 <message name="SENDMESSAGEResponse">
  <part element="impl:SENDMESSAGEReturn" name="SENDMESSAGEReturn"/>
 </message>
 <portType name="SendADocument">
  <operation name="SENDMESSAGE">
   <input message="impl:SENDMESSAGERequest" name="SENDMESSAGERequest"/>
   <output message="impl:SENDMESSAGEResponse" name="SENDMESSAGEResponse"/>
  </operation>
 </portType>
 <binding name="DominoSoapBinding" type="impl:SendADocument">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="SENDMESSAGE">
   <wsdlsoap:operation soapAction="SENDMESSAGE"/>
   <input name="SENDMESSAGERequest">
    <wsdlsoap:body use="literal"/>
   </input>
   <output name="SENDMESSAGEResponse">
    <wsdlsoap:body use="literal"/>
   </output>
  </operation>
 </binding>
 <service name="SendADocumentService">
  <port binding="impl:DominoSoapBinding" name="Domino">
   <wsdlsoap:address location="http://localhost"/>
  </port>
 </service>
</definitions>

To use that service the web service client needs to Base64 encode the PDF attachment
As usual YMMV

Posted by on 06 January 2014 | Comments (0) | categories: IBM Notes

Comments

  1. No comments yet, be the first to comment