Home > Ask the Domino Experts > Web Development and Downloads Questions & Answers > Attach file in LotusScript to MIME mail
Ask The Domino Expert: Questions & Answers
EMAIL THIS

Attach file in LotusScript to MIME mail

Jens Bruntt EXPERT RESPONSE FROM: Jens Bruntt

Pose a Question
Other Domino Categories
Meet all Domino Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 23 March 2005
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.

Jens Bruntt

Do you have comments on this Ask the Expert question and response? Let us know.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Web Development and Downloads
Programmatically copy and hide attachments in Lotus Notes rich-text fields
Programmatically edit a rich-text field table from within the Lotus Notes client
Troubleshooting Lotus Notes Domino tabbed table problems
Verify Lotus Notes user access to Web pages
Can LotusScript agent display search results using specific view?
Creating Word doc from form works in Notes 5.0.2 but not 5.0.11
XML vs. JavaScript to fill in dropdown on form
Debugging Web agents
Configure an IM hot spot on Web page
Can I use PHP to change tables in MySQL DB running off Domino?

Web Development for Lotus Notes Domino
Trap JavaScript runtime errors in Domino Web apps
Write HTML and JavaScript in Notes view rows and columns on the Web
JavaScript detects Web browser type and version in Notes/Domino 8.0.2
Top 10 Lotus Notes/Domino coding and development tips of 2008
Top 10 issues when developing Lotus Notes Domino Internet applications
Top 10 Lotus Notes Domino programming and development tips of 2007
Programmatically copy and hide attachments in Lotus Notes rich-text fields
Programmatically edit a rich-text field table from within the Lotus Notes client
Troubleshooting Lotus Notes Domino tabbed table problems
How to validate Lotus Notes forms on a Domino server without losing entered data

LotusScript
LotusScript agent parses ACL to Microsoft Notepad
LotusScript finds the first occurrence of a string from the right
Clear Recent Contacts view and prevent repopulation in Lotus Notes 8.x
Search Microsoft Active Directory with LotusScript
Three steps to trap and handle save conflicts with LotusScript
Troubleshoot agents by displaying LotusScript variables online
LotusScript sorts lists alphabetically
Run or restart Notes/Domino agents via text messages
LotusScript code rebuilds corrupted busytime.nsf file
Soft-code item names to facilitate LotusScript management

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Lotus Notes Domino on Blackberry and mobile devices
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts