Lotus Notes just updated and became IBM Notes 9.0.1 causing code I've written to send an email with an attachment to fail. Here's the code:
Public Sub SendNotesMail(ByVal attachment As String, ByVal ToRecipients As List(Of String), ByVal CcRecipients As List(Of String), ByVal 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
plsWaitFrm.Show()
Try
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 = Microsoft.VisualBasic.Left(UserName, 1) & Microsoft.VisualBasic.Right(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
'Open the mail database in notes
Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
'Already open for mail
Else
Maildb.OPENMAIL()
End If
'Set up the new mail document
MailDoc = Maildb.CREATEDOCUMENT
MailDoc.Form = "Memo"
Dim recipients(ToRecipients.Count - 1) As String
For i As Integer = 0 To ToRecipients.Count - 1 Step 1
recipients(i) = ToRecipients(i)
Next
Dim copies(CcRecipients.Count - 1) As String
For i As Integer = 0 To CcRecipients.Count - 1 Step 1
copies(i) = CcRecipients(i)
Next
'Dim recipient As String = String.Join(", ", ToRecipients)
'recipient = recipient & ","
'Dim copies As String = String.Join(", ", CcRecipients)
'copies = copies & ","
'MailDoc.sendto = recipient
MailDoc.CopyTo = copies
MailDoc.Subject = txtBxSubject.Text
MailDoc.Body = txtBxBody.Text
MailDoc.SAVEMESSAGEONSEND = saveit
'Set up the embedded object and attachment and attach it
If attachment <> "" Then
AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
EmbedObj = AttachME.EMBEDOBJECT(1454, "", attachment, "Attachment")
End If
'Send the document
MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
MailDoc.SEND(0, recipients)
Dim attachmentName As String = Path.GetFileName(attachment)
Dim recipientNames As New List(Of String)
Dim copyNames As New List(Of String)
For Each Contact In contactListLocal.Contacts
If Contact.EmailTo = True Then recipientNames.Add(Contact.First & " " & Contact.Last)
If Contact.EmailCC = True Then copyNames.Add(Contact.First & " " & Contact.Last)
Next
MsgBox("Email message sent!" & vbCr & vbCr & "To: " & String.Join(", ", recipientNames) & vbCr & vbCr & "Cc: " & String.Join(", ", copyNames) & vbCr & vbCr & "Attachment: " & attachmentName, MsgBoxStyle.OkOnly, "AzTech Satellite Share App")
Catch ex As Exception
ErrorHandler.Helpers.LogMessage(ex.Message)
MsgBox("An error occurred sending your email. Please contact AzTech IT for help.", MsgBoxStyle.OkOnly, "AzTech Satellite Share App")
Finally
'Clean Up
Maildb = Nothing
MailDoc = Nothing
AttachME = Nothing
Session = Nothing
EmbedObj = Nothing
'RaiseEvent EmailSent()
'plsWaitFrm.Close()
plsWaitFrm.Hide()
End Try
End Sub
The line causing this error: 6/12/2015 20:30:39 : The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) is:
MailDoc.CREATERICHTEXTITEM("Attachment")
Without any suggestions from Intellisense, and knowing next to nothing about the inner workings of Notes, I'm completely in the dark with this code. Does anyone have a clue what needs to change?
EDIT As suggested, I removed the code creating the second attachment - MailDoc.CREATERICHTEXTITEM("Attachment"). The email does now send, but the code still throws the above error at MailDoc.SEND(0, recipients).
EDIT #2 The emails do send, but occassionally the messages are sent twice, and at other times Notes crashes.
Is there really no one else who has this issue or that can make a recommendation?
You have two lines that call Maildoc.CreateRichTextItem. Both of them attempt to create an item named "Attachment". Is it the first or second one that throws the error? My guess is that it's the second one, which is effectively trying to create an item that already exists. That's certainly not normal practice, and I can't think of a reason why anybody would code it that way. So most likely, whatever version of Notes you were on previously handled coding like that, probably ignoring the attempt to add the duplicate item, but the newer version of the Notes API detects it as an error.
Note that it's also not really normal to create a separate NotesRichTextItem named "attachment" just for an email attachment. It works, but it probably won't look the way you expect it to. The normal way is to attach it to the NotesRichTextItem named "Body". Your code is working with a plain text item named "Body" instead, and whomever coded this probably didn't know that it could be rich text and that the same item could store the attachment.