I have code that essentially sends an email, it creates the email with a MIME HTML body and a Richtext PDF attachment. The weird part is, is that when the message is saved, both the HTML Body is there and the attachment at the top are both present.
When I send this out externally the PDF attachment is no longer there. If I comment out the HTML MIME part of the code the attachment sends attached at the top externally just fine.
Why is one preventing the other and how would I get both to send the same way the document is saved?
Any help would be appreciated.
Public Sub COM_Email_Send()
Dim NSession As Object
Dim NMailDb As Object
Dim NDocument As Object
Dim NBody As Object
Dim NChild As Object
Dim Nstream As Object
Dim RichTextHeader As Object
Dim i As Long
Dim Row As Long
Dim Recipient As String
Dim File As String
Dim attachmentFile As String
Dim Data As String
Dim AttachedOb As Object
Dim EmbedOb As Object
Dim NHeader As Object
Dim strFileType As Variant
Dim MIMEDoc As Object
Set NSession = CreateObject("Lotus.NotesSession")
Call NSession.Initialize("password")
Set NMailDb = NSession.GetDatabase("directory", "server")
If Not NMailDb.IsOpen = True Then
Call NMailDb.Open
End If
Row = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To Row
Recipient = Worksheets("Sheet1").Range("B" & i)
If Recipient <> "" Then
File = Worksheets("Sheet1").Range("A" & i).Value
attachmentFile = "Directory" & File
Data = Format(Now(), "dd/mm/yyyy")
Set NDocument = NMailDb.CreateDocument
Set Nstream = NSession.CreateStream
Call NDocument.replaceitemvalue("Form", "Memo")
Call NDocument.replaceitemvalue("SendTo", Recipient)
Call NDocument.replaceitemvalue("Subject", "Please see your clearance documents attached " & Data)
Call NDocument.replaceitemvalue("Sender", "[email protected]")
If attachmentFile <> "" Then
Set AttachedOb = NDocument.Createrichtextitem("attachmentFile")
Set EmbedOb = AttachedOb.embedobject(1454, "", attachmentFile, "")
End If
Call Nstream.Open("Directory\HTML BODY.htm")
Set NBody = NDocument.CreateMIMEEntity '("memo")
Set RichTextHeader = NBody.CreateHeader("Content-Type")
Call RichTextHeader.SetHeaderVal("multipart/mixed")
Set MIMEDoc = NBody.CreateChildEntity()
Call MIMEDoc.SetContentFromBytes(Nstream, "text/html", ENC_IDENTITY_BINARY)
Call Nstream.Close
NDocument.savemessageonsend = True
Call NDocument.replaceitemvalue("PostedDate", Now())
Call NDocument.Send(False)
Set NDocument = Nothing
Set Nstream = Nothing
End If
Next i
End Sub
*Edited code Following Richards guidance
Public Sub COM_Email_Send()
Dim NSession As Object
Dim NMailDb As Object
Dim NDocument As Object
Dim NBody As Object
Dim NChild As Object
Dim Nstream As Object
Dim Header As Object
Dim HeaderChild As Object
Dim i As Long
Dim Row As Long
Dim Recipient As String
Dim File As String
Dim attachmentFile As String
Dim Data As String
Dim AttachedOb As Object
Dim EmbedOb As Object
Dim NHeader As Object
Dim strFileType As Variant
Dim MIMEDoc As Object
Set NSession = CreateObject("Lotus.NotesSession")
Call NSession.Initialize("password")
Set NMailDb = NSession.GetDatabase("server directory", "server")
If Not NMailDb.IsOpen = True Then
Call NMailDb.Open
End If
Row = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To Row
Recipient = Worksheets("Sheet1").Range("B" & i)
If Recipient <> "" Then
File = Worksheets("Sheet1").Range("A" & i).Value
attachmentFile = "Directory" & File
Data = Format(Now(), "dd/mm/yyyy")
Set NDocument = NMailDb.CreateDocument
Set Nstream = NSession.CreateStream
Call NDocument.replaceitemvalue("Form", "Memo")
Call NDocument.replaceitemvalue("SendTo", Recipient)
Call NDocument.replaceitemvalue("Subject", "Please see your clearance documents attached " & Data)
Call NDocument.replaceitemvalue("Sender", "[email protected]")
Set NBody = NDocument.CreateMIMEEntity
Call Nstream.Open("Directory")
Set MIMEDoc = NBody.CreateChildEntity()
Set Header = MIMEDoc.Createheader("Content-Type")
Call Header.SetHeaderVal("multipart/mixed")
Call MIMEDoc.SetContentFromBytes(Nstream, "text/html", ENC_IDENTITY_BINARY)
Call Nstream.Close
Call Nstream.Truncate
Call Nstream.Open("Directory" & File)
Set NChild = NBody.CreateChildEntity()
Set HeaderChild = NChild.Createheader("Content-Type")
Call HeaderChild.SetHeaderVal("multipart/mixed")
Set HeaderChild = NChild.Createheader("Content-Disposition")
Call HeaderChild.SetHeaderVal("attachment; filename=" & File)
Set HeaderChild = NChild.Createheader("Content-ID")
Call HeaderChild.SetHeaderVal(File)
Set HeaderChild = NChild.Createheader("Content-Transfer-Encoding")
Call HeaderChild.SetHeaderVal(base64)
Call NChild.SetContentFromBytes(Nstream, "application/pdf", ENC_BASE64)
Call Nstream.Close
NDocument.savemessageonsend = True
Call NDocument.replaceitemvalue("PostedDate", Now())
Call NDocument.Send(False)
Set NDocument = Nothing
Set Nstream = Nothing
End If
Next i
End Sub
Document now attaches as PDF below the HTML body but is blank.
CodePudding user response:
An email message can either be built with Notes Rich Text or with MIME. It can't be built with both.
If you want to send both, you will have to construct a multipart/mixed message (a parent NotesMIME entity with multipart/mixed for the content-type, like you have above, but it needs to have two child NotesMIMEEntity parts. One child NotesMIMEEntity will be for the text/HTML content -- it will need its own content-type set to text/html. The second child NotesMIMEEntity will be for the attachment. You will have to read the attachment data from the file and encode it as base64. You can find a LotusScript function that does base64 encoding here. Then you will set the content of the child entity from the base64 data, and set the headers for this with appropriate info for the file. E.g., if it is a PDF you are going to want MIME headers that look something like this:
Content-Type: application/pdf; name="MyPDFFile.pdf"
Content-Disposition: attachment; filename="MyPDFFile.pdf"
Content-Transfer-Encoding: base64
For most other file types, I think application/octet-stream is what you will want, but it's been a while and there could be other type/subtype combinations available. Whenever I have any questions about what MIME headers should look like, I manually send an email to my gmail account containing an example of the content that I want and use the 'show original' feature to examine the tree structure and details of the headers.