Attach file in LotusScript to MIME mail
How can I attach a file in LotusScript to a MIME mail I am creating in LotusScript?
The basic code, downloaded from the Web, I am using in my tests is as follows:
'Declare Variables Dim s As New NotesSession Dim db As NotesDatabase Dim body As NotesMIMEEntity Dim stream As NotesStream Dim host As String Set db = s.CurrentDatabase s.ConvertMIME = False ' Do not convert MIME to rich text Set stream = s.CreateStream 'Begin creating the message doc to send Dim message As New NotesDocument (db) message.Form="memo" Set body = message.CreateMIMEEntity 'Basic profile of email message.Subject = "Sample HTML email via MIME" message.SendTo = "ammujica" ' Open the HTML (Title doesn't matter since it doesn't appear anywhere) Call stream.WriteText ("<html><head><title>Sample HTML email via MIME</title>") ' BEGIN: Inline Stylesheet Call stream.WriteText (| <style type="text/css"> <!-- .text, td, tr, p, br, body { COLOR: #666666; FONT-FAMILY: Arial, Helvetica, sans-serif; FONT-SIZE: 12px; } a { font-family: Arial, Helvetica, sans-serif; color: #663399; FONT-WEIGHT: bold; text-decoration: none; } --> </style> |) ' END: Inline Stylesheet Call stream.WriteText ({</head>}) Call stream.WriteText ({<body text="#666666" bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">}) ' BEGIN: HTML body Call stream.WriteText ({ <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF"> </table> }) ' ' HTML body ' Call stream.WriteText ({</body></html>}) Call body.SetContentFromText (stream, "text/html;charset=iso-8859-1", ENC_NONE) 'Send the email Call message.Send (False) s.ConvertMIME = True ' Restore conversion
View member feedback to this Ask the Expert Q&A.
I have modified your sample code. It now works including sending a word file as an attachment at the bottom of the text.
'Declare Variables Dim s As New NotesSession Dim db As NotesDatabase Dim body As NotesMIMEEntity, bodyChild As NotesMimeEntity Dim header As NotesMIMEHeader Dim stream As NotesStream Dim host As String Dim message As NotesDocument Set db = s.CurrentDatabase s.ConvertMIME = False ' Do not convert MIME to rich text Set stream = s.CreateStream Set message = db.CreateDocument message.Form = "memo" Set body = message.CreateMIMEEntity message.Subject = "Sample HTML email via MIME" message.SendTo = "some@mailaddress" Set stream = s.CreateStream() ' Open the HTML (Title doesn't matter since it doesn't appear anywhere) Call stream.WriteText ("<html><head><title>Sample HTML email via MIME</title>") ' BEGIN: Inline Stylesheet Call stream.WriteText (| <style type="text/css"> <!-- .text, td, tr, p, br, body { COLOR: #666666; FONT-FAMILY: Arial, Helvetica, sans-serif; FONT-SIZE: 12px; } a { font-family: Arial, Helvetica, sans-serif; color: #663399; FONT-WEIGHT: bold; text-decoration: none; } --> </style> |) ' END: Inline Stylesheet Call stream.WriteText ({</head>}) Call stream.WriteText ({<body text="#666666" bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">}) ' BEGIN: HTML body Call stream.WriteText ({ <table width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">Some text here in the table</table> }) Call Stream.WriteText ({some text outiside the table<br>}) Call stream.WriteText ({</body></html>}) 'Child mime entity which is going to contain the HTML which we put in the stream Set bodyChild = body.CreateChildEntity() Call bodyChild.SetContentFromText (stream, "text/html;charset=iso-8859- 1", ENC_NONE) Call stream.Close Call stream.Truncate 'Committing the stream to the child mime entity - done 'A new child mime entity to hold a file attachment Set bodyChild = body.CreateChildEntity() Set header = bodyChild.createHeader("Content-Type") Call header.setHeaderVal("multipart/mixed") Set header = bodyChild.createHeader("Content-Disposition") Call header.setHeaderVal ("attachment; filename=01-2005.doc") '01- 2005.doc should be replaced with the file name of the file you need to attach Set header = bodyChild.createHeader("Content-ID") Call header.setHeaderVal("01-2005.doc") '01-2005.doc should be replaced with the file name of the file you need to attach Set stream = s.CreateStream() If Not stream.Open ("c:\temp\01-2005.doc", _ "binary") Then Print "Open failed" End If If stream.Bytes = 0 Then Print "File has no content" End If Call bodyChild.SetContentFromBytes (stream, "application/msword", ENC_IDENTITY_BINARY) %REM | application/msword needs to match the file type that you are embedding. Examples: image/gif application/msword application/pdf application/zip %END REM 'Send the email Call message.Send (False) s.ConvertMIME = True ' Restore conversion
MEMBER FEEDBACK TO THIS ASK THE EXPERT Q&A:
Thanks for the really useful article about how to create a MIME-message with attachments! However, when implementing this, I noticed that the attachment names got truncated at certain characters (space, round bracers) and that very long attachment names had their name abruptly cut of at character 78. This is due to the fact that the attachment names should be surrounded by quotes.
The following code:
Set header = bodyChild.createHeader("Content-Disposition") Call header.setHeaderVal ("attachment; filename=01-2005.doc") Set header = bodyChild.createHeader("Content-ID") Call header.setHeaderVal("01-2005.doc")
Should be:
Set header = bodyChild.createHeader("Content-Disposition") Call header.setHeaderValAndParams ( |attachment; filename="01-2005.doc"| ) Set header = bodyChild.createHeader("Content-ID") Call header.setHeaderVal( |"01-2005.doc"| )
I have two questions.
1. Why is "Content-ID" used, because when sending an e-mail with Outlook Express it is not generated? Is this header really necessary?
2. Is it possible to disable "folding" the MIME headers? Long lines now always have a linebreak at the 78th character, meaning that long attachment names get chopped off. The linebreak is translated into a space in the resulting attachment name.
Rainer K.
******************************************
As to the suggestion to change the code, I agree. It looks like the suggested change would be a somewhat better solution. Then there are the questions, and to be honest, I dont have any good answers. My experience with MIME is limited.
For the first question: As far as I remember, the Content-ID needs to be there in order to make sure that the attachment gets attached properly in the Lotus Notes document. I am pretty sure that leaving it out gave me some kind of problem.
As for the second question, my answer is simply: Not as far as I know.
Do you have comments on this Ask the Expert question and response? Let us know.