Reputation: 25
I modified a code that I found to send MIME HTML mails with attachments. It works when the mail is open in gmail, but in Apple Mail or Lotus Notes there isn't any attachment in the body. The plaintext part does not arrive either, but what interests me are the attachments.
Here is my code:
Set MailDoc = New NotesDocument( mailbox )
session.convertMIME = False
Dim body As NotesMIMEEntity
Dim mimeHeader As NotesMIMEHeader
Dim mimeEntity As NotesMIMEEntity
Dim stream As NotesStream
Set body = mailDoc.CreateMIMEEntity
Set mimeHeader = body.CreateHeader({MIME-Version})
Call mimeHeader.SetHeaderVal("1.0")
Set mimeHeader = body.CreateHeader("Content-Type")
Call mimeHeader.SetHeaderValAndParams({multipart/alternative;boundary="=NextPart_="})
' create another MIME part for the attachment. (repeat as needed)
If (docEval.HasItem("$File")) Then
Dim AttachmentsField As NotesRichTextItem
Set AttachmentsField = docEval.GetFirstItem( "fc_adjunto" )
If IsArray(AttachmentsField.EmbeddedObjects) Then ' Isarray validates for attachments ignoring embedded text
ForAll att In AttachmentsField.EmbeddedObjects
If att.Type = EMBED_ATTACHMENT Then
Call errorLog.LogAction("Inicio attch: " + att.Name())
Set mimeEntity = body.CreateChildEntity( )
Set mimeHeader = mimeEntity.CreateHeader("Content-Disposition")
aliasFilename = CStr(att.Name())
Call mimeHeader.SetHeaderValAndParams(|attachment; filename=| & aliasFilename)
Set stream = session.CreateStream
attachFilename = AttachToFile(session, docEval, CStr(att.Name())) 'Call o.ExtractFile(Attachfoldername & "\" & o.Name())
Call errorLog.LogAction("Inicio attch name: " + attachFilename)
stream.Open attachFileName, "binary"
Call mimeEntity.SetContentFromBytes(stream, "application/octet-stream", ENC_IDENTITY_BINARY)
mimeEntity.EncodeContent(ENC_BASE64)
Kill attachFilename
End If
End ForAll
End If
End If
'Send the plain text part first (this never work for me!!)
Set mimeEntity = body.createChildEntity()
Set stream = session.createStream()
Call WritePlainText(stream)
Call mimeEntity.setContentFromText(stream, {text/plain;charset="iso-8859-1"}, ENC_IDENTITY_8BIT)
'Send the HTML part.
Call stream.Truncate()
'Send the HTML part.
Set mimeEntity = body.createChildEntity()
Call WriteHTML(stream, elHtml)
Call mimeEntity.setContentFromText(stream, {text/html;charset="iso-8859-1"},ENC_IDENTITY_8BIT)
'Close the stream and Send it
Call stream.Truncate()
Call stream.Close()
Call maildoc.Closemimeentities(True)
Call MailDoc.ReplaceItemValue("EncryptOnSend" , True )
Call MailDoc.ReplaceItemValue("PostedDate" , Now )
Call MailDoc.ReplaceItemValue("From" , Remitente )
Call MailDoc.ReplaceItemValue("SendFrom" , Remitente )
Call MailDoc.ReplaceItemValue("Principal" , Remitente )
Call MailDoc.ReplaceItemValue( "SendTo" , sendTo )
Call MailDoc.ReplaceItemValue( "CopyTo" , copyTo )
Call MailDoc.ReplaceItemValue( "BlindCopyTo" , bccTo )
Call MailDoc.ReplaceItemValue( "Subject" , Subject )
Call MailDoc.save( True , False ) 'Send it
session.convertMIME = True
Any idea what could I miss? Thanks
Upvotes: 0
Views: 1578
Reputation: 25
The order I used was:
multipart/mixed multipart/alternative text/plain text/HTML application/octet-stream application/octet-stream (etc...) Thanks @Richard
First I've changed
SetHeaderValAndParams({multipart/alternative;boundary="=NextPart_="})
To:
SetHeaderValAndParams({multipart/mixed;boundary="=NextPart_="})
Then the plain text:
setContentFromText(stream, {text/plain}, ENC_NONE)
Then the html:
setContentFromText(stream, {text/html;charset="iso-8859-1"}, ENC_NONE)
Finally:
Repeat as needed... (attachments)
stream.Open attachFileName, "binary" (attachFileName full path)
mc.SetContentFromBytes(stream, "application/octet-stream", ENC_IDENTITY_BINARY)
Upvotes: 1
Reputation: 2359
IMHO far better code you can find at: http://www.codestore.net/store.nsf/unid/BLOG-20091022-0419
Upvotes: 0
Reputation: 14628
It looks to me like you are using only a multipart/alternative section. I don't see a multipart/mixed section. I believe that you need to have a nested structure like this:
multipart/mixed
application/octet-stream
application/octet-stream (etc...)
multipart/alternative
text/plain
text/HTML
Send yourself a hand-crafted email that looks the way that you want it to, and verify the MIME headers in the received message to be sure I've got this right ('View - Show - Page Source' in the Notes client or 'Show Original' in gmail), but I'm looking at several messages with attachments in my own inbox, and they've all got this structure.
Upvotes: 2