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")

        '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 Function

        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 Sub

        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"
  <schema targetNamespace="urn:DefaultNamespace" xmlns="http://www.w3.org/2001/XMLSchema">
   <complexType name="SendADocumentWithPDFAttachment">
   <element name="MESSAGE" type="impl:SendADocumentWithPDFAttachment"/>
   <element name="SENDMESSAGEReturn" type="xsd:string"/>
 <message name="SENDMESSAGERequest">
  <part element="impl:MESSAGE" name="MESSAGE"/>
 <message name="SENDMESSAGEResponse">
  <part element="impl:SENDMESSAGEReturn" name="SENDMESSAGEReturn"/>
 <portType name="SendADocument">
  <operation name="SENDMESSAGE">
   <input message="impl:SENDMESSAGERequest" name="SENDMESSAGERequest"/>
   <output message="impl:SENDMESSAGEResponse" name="SENDMESSAGEResponse"/>
 <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"/>
   <output name="SENDMESSAGEResponse">
    <wsdlsoap:body use="literal"/>
 <service name="SendADocumentService">
  <port binding="impl:DominoSoapBinding" name="Domino">
   <wsdlsoap:address location="http://localhost"/>

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


  1. No comments yet, be the first to comment