Reputation: 463
I want to send a mail with a vbscript without opening Lotus Notes UI. I managed to write a script that allows to send email with attachments and everything works fine. However the application is launched when I run the macro and I want it to work silenty (without launching the application).
Here is my code:
Dim noSession As Object
Dim noDatabase As Object
Dim noDocument As Object
Dim paths() As String
'Instantiate the Lotus Notes COM's Objects.
Set MailSession = CreateObject("Notes.NotesSession")
Set MailDatabase = MailSession.GetDatabase("", "")
'If Lotus Notes is not open then open the mail-part of it.
If MailDatabase.IsOpen = False Then MailDatabase.OPENMAIL
'Create the list of recipients.
vaRecipients = VBA.Array(EmailTo)
vaCCRecipients = VBA.Array(EmailCC)
vaBCCRecipients = VBA.Array(EmailBCC)
Set MailDoc = MailDatabase.CreateDocument
MailDoc.Form = "Memo"
MailDoc.sendTo = vaRecipients
MailDoc.CopyTo = vaCCRecipients
MailDoc.BlindCopyTo = vaBCCRecipients
MailDoc.Subject = Subject
MailDoc.Body = Body
MailDoc.SAVEMESSAGEONSEND = True
MailDoc.ReplyTo = Sender
MailDoc.SMTPOriginator = Sender
MailDoc.Sender = Sender
MailDoc.principal = Sender
MailDoc.inetprincipal = Sender
MailDoc.from = Sender
MailDoc.inetfrom = Sender
MailDoc.displayfrom = Sender
paths = Split(AttachmentPaths, ";")
Dim richTextItem As Object
Dim AttachmentPath As String
For i = LBound(paths()) To UBound(paths())
AttachmentPath = paths(i)
Set richTextItem = MailDoc.CreateRichTextItem("Attachment" & i)
Call richTextItem.EmbedObject(1454, "", AttachmentPath)
Next i
MailDoc.Send 0, vaRecipients
'Release objects from memory.
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing
MsgBox "The e-mail has successfully been created and distributed", vbInformation
The problem is that Lotus notes UI is launched everytime I run the macro because of this line:
If MailDatabase.IsOpen = False Then MailDatabase.OPENMAIL
Upvotes: 4
Views: 331
Reputation: 12060
You are wrong in your assumption that OPENMAIL is the cause for the client opening.
The real problem is your usage of the OLE- based objectclass "Notes.NotesSession". If you really want to get away without opening the notes- Client you need to use the COM- based objectclass "Lotus.NotesSession" or "Domino.NotesSession".
The problem is: When using Lotus.NotesSession you need to initialize your session with password yourself (your password has to be in the code) or a black DOS- Prompt will ask your for your Notes- Password. When using "Notes.NotesSession", the client itself is used for authentication. That is why it opens when running your code. If it is already open, nothing will happen.
You can check this old post for more details, its principles are still true.
The code is nearly the same as with the other class, you also need a properly installed Notes Client. You just need to initialize the session instead of just using it:
Set MailSession = CreateObject("Lotus.NotesSession")
MailSession.Initialize( "yourpassword" )
If you don't enter a password then you will be prompted for it.
Upvotes: 4