Send Mail Using Lotus Notes


'This public sub will send a mail and attachment if neccessary to the recipient including the body text. 
'Requires that notes client is installed on the system. 


Public Sub SendNotesMail(Subject As String, attachment As String, recipient As String, bodytext As String, saveit As Boolean) 
'Set up the objects required for Automation into lotus notes 
    Dim Maildb As Object 'The mail database 
    Dim UserName As String 'The current users notes name 
    Dim MailDbName As String 'THe current users notes mail database name 



    Dim MailDoc As Object 'The mail document itself 
    Dim AttachME As Object 'The attachment richtextfile object 
    Dim Session As Object 'The notes session 
    Dim EmbedObj As Object 'The embedded object (Attachment) 
    'Start a session to notes 
    Set Session = CreateObject("Notes.NotesSession") 
    'Get the sessions username and then calculate the mail file name 
    'You may or may not need this as for MailDBname with some systems you 
    'can pass an empty string 
    UserName = Session.UserName 
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf" 
    'Open the mail database in notes 
    Set Maildb = Session.GETDATABASE("", MailDbName) 
     If Maildb.IsOpen = True Then 
          'Already open for mail 
     Else 
         Maildb.OPENMAIL 
     End If 
    'Set up the new mail document 
    Set MailDoc = Maildb.CREATEDOCUMENT 
    MailDoc.Form = "Memo" 
    MailDoc.sendto = recipient 
    MailDoc.Subject = Subject 
    MailDoc.Body = bodytext 
    MailDoc.SAVEMESSAGEONSEND = saveit 
    'Set up the embedded object and attachment and attach it 
    If attachment <> "" Then 
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment") 
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", attachment, "Attachment") 
        MailDoc.CREATERICHTEXTITEM ("Attachment") 
    End If 
    'Send the document 
    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder 
    MailDoc.SEND 0, recipient 
    'Clean Up 
    Set Maildb = Nothing 
    Set MailDoc = Nothing 
    Set AttachME = Nothing 
    Set Session = Nothing 
    Set EmbedObj = Nothing 
End Sub