Author: Tomas Rutkauskas
How to send an email with an attachment
Answer:
Solve 1:
For Outlook mail:
1 uses
2 ComObj;
3
4 procedure MailItem;
5 const
6 olMailItem = 0;
7 var
8 Outlook: OLEVariant;
9 MailItem: Variant;
10 begin
11 try
12 Outlook := GetActiveOleObject('Outlook.Application');
13 except
14 Outlook := CreateOleObject('Outlook.Application');
15 end;
16 MailItem := Outlook.CreateItem(olMailItem);
17 MailItem.Recipients.Add(< email address here > );
18 MailItem.Subject := 'your subject';
19 MailItem.Body := 'This is sample text';
20 MailItem.Attachments.Add('C:\Windows\Win.ini');
21 MailItem.Send;
22 Outlook := Unassigned;
23 end;
Solve 2:
There have been many questions about how to send an e-mail with an attachment. I
came up with the vbs script that did it with Outlook.
Well here's the Delphi translation that uses OLE to accomplish the same thing:
Change recipientaddress@recipienthost.com to your own e-mail address and give it a
try. You must have Outlook installed, I'm not sure if this will work with Outlook
Express.
24 procedure TForm1.Button1Click(Sender: TObject);
25 const
26 { OlItemType constants }
27 olMailItem = 0;
28 olAppointmentItem = 1;
29 olContactItem = 2;
30 olTaskItem = 3;
31 olJournalItem = 4;
32 olNoteItem = 5;
33 olPostItem = 6;
34 { OlAttachmentType constants }
35 olByValue = 1;
36 olByReference = 4;
37 olEmbeddedItem = 5;
38 olOLE = 6;
39 var
40 myOlApp, myItem, myRecipient, myAttachments: OleVariant;
41 begin
42 { VBScript file to create a mail and add an attachment }
43 myOlApp := CreateOLEObject('Outlook.Application');
44 myItem := myOlApp.CreateItem(olMailItem);
45 myItem.Subject := 'This is the Subject';
46 myRecipient := myItem.Recipients.Add('recipientaddress@recipienthost.com');
47 myItem.Body := #13;
48 myItem.Body := myItem.Body + #13;
49 myItem.Body := myItem.Body + 'Hello,' + #13;
50 myItem.Body := myItem.Body + 'This code created this message and ' +
51 ' sent it and I didn'' t even have' + #13;
52 myItem.Body := myItem.Body + 'to click the send button!!!' + #13;
53 myItem.Body := myItem.Body + #13;
54 myItem.Body := myItem.Body + 'If you have any more problems, let me know' + #13;
55 myItem.Body := myItem.Body + 'rename to blah.vbs and run like this:' + #13;
56 myItem.Body := myItem.Body + 'wscript c:\blah.vbs' + #13;
57 myItem.Body := myItem.Body + #13;
58 myItem.Body := myItem.Body + 'MrBaseball34' + #13;
59 myItem.Body := myItem.Body + #13;
60 myItem.Body := myItem.Body + #13;
61 myItem.Body := myItem.Body + 'const' + #13;
62 myItem.Body := myItem.Body + ' // OlItemType constants' + #13;
63 myItem.Body := myItem.Body + ' olMailItem = 0;' + #13;
64 myItem.Body := myItem.Body + ' olAppointmentItem = 1;' + #13;
65 myItem.Body := myItem.Body + ' olContactItem = 2;' + #13;
66 myItem.Body := myItem.Body + ' olTaskItem = 3;' + #13;
67 myItem.Body := myItem.Body + ' olJournalItem = 4;' + #13;
68 myItem.Body := myItem.Body + ' olNoteItem = 5;' + #13;
69 myItem.Body := myItem.Body + ' olPostItem = 6;' + #13;
70 myItem.Body := myItem.Body + ' // OlAttachmentType constants' + #13;
71 myItem.Body := myItem.Body + ' olByValue = 1;' + #13;
72 myItem.Body := myItem.Body + ' olByReference = 4;' + #13;
73 myItem.Body := myItem.Body + ' olEmbeddedItem = 5;' + #13;
74 myItem.Body := myItem.Body + ' olOLE = 6;' + #13;
75 myItem.Body := myItem.Body + #13;
76 myItem.Body := myItem.Body + 'var' + #13;
77 myItem.Body := myItem.Body + ' myOlApp, myItem, myRecipient, myAttachments:
78 OleVariant;'
79 myItem.Body := myItem.Body + 'begin' + #13;
80 myItem.Body := myItem.Body + ' myOlApp := CreateObject(''Outlook.Application'')'
81 + #13;
82 myItem.Body := myItem.Body + ' myItem := myOlApp.CreateItem(olMailItem)' + #13;
83 myItem.Body := myItem.Body + ' myItem.Subject := ''This is the Subject''' + #13;
84 myItem.Body := myItem.Body + ' myItem.Body := ''This is the body''' + #13;
85 myItem.Body := myItem.Body + ' myRecipient :=
86 myItem.Recipients.Add('thost.com')' + #13;
87 myItem.Body := myItem.Body + ' myAttachments := myItem.Attachments' + #13;
88 myItem.Body := myItem.Body + ' // Now let''s attach the files...' + #13;
89 myItem.Body := myItem.Body + ' myAttachments.Add ''C:\blah.txt'', olByValue, 1,
90 'h.txt Attachment''' + #13;
91 myItem.Body := myItem.Body + '
92 myItem.Body := myItem.Body + ' myOlApp := VarNull;' + #13;
93 myItem.Body := myItem.Body + ' myItem := VarNull;' + #13;
94 myItem.Body := myItem.Body + ' myRecipient := VarNull;' + #13;
95 myItem.Body := myItem.Body + ' myAttachments := VarNull;' + #13;
96 myItem.Body := myItem.Body + 'end;' + #13;
97 { Now let's attach the files... }
98 myAttachments := myItem.Attachments;
99 myAttachments.Add('C:\blah.txt', olByValue, 1, 'Blah.txt Attachment');
100 myItem.Send;
101 myOlApp := VarNull;
102 myItem := VarNull;
103 myRecipient := VarNull;
104 myAttachments := VarNull;
105 end;
106
107 The way you do it, your string is recreated an shuffled around a lot of times. The
108 way directly above, you'll only have one large string, being slowly filled with the
109 text. So instead of
110
111 myItem.Body := #13;
112 myItem.Body := myItem.Body + #13;
113 myItem.Body := myItem.Body + '
114 myItem.Body := myItem.Body + 'This code created this message and ' +
115 ' sent it and I didn''t even have' + #13;
116 { etc... }
117
118 //you could write
119
120 myItem.Body := #13 + #13 + 'Hello' + #13 + 'This code created this message and ' +
121 'sent it and I didn''t even have' + #13 +
122 { etc... }
Solve 3:
123 uses
124 Mapi;
125
126 procedure TForm1.Button1Click(Sender: TObject);
127 const
128 MyToName = 'Name';
129 MyToAddress = 'to@domain.com';
130 MyCCAddress = 'cc@domain.com';
131 MySubject = 'Subject';
132 MyBody = 'Some' + #13#10 + 'Text';
133 MyFileName = 'c:\config.sys';
134 MySendError = 'Error sending mail';
135 var
136 MyMapiMessage: TMapiMessage;
137 MyRecipients: array of TMapiRecipDesc;
138 MyAttachments: array of TMapiFileDesc;
139 begin
140 {Recipient addresses}
141 SetLength(MyRecipients, 2);
142 FillChar(MyRecipients[0], Length(MyRecipients) * SizeOf(TMapiRecipDesc), 0);
143 with MyRecipients[0] do
144 begin
145 ulRecipClass := MAPI_TO;
146 lpszName := PChar(MyToName);
147 lpszAddress := PChar(MyToAddress);
148 end;
149 with MyRecipients[1] do
150 begin
151 ulRecipClass := MAPI_CC;
152 lpszAddress := PChar(MyCCAddress);
153 end;
154 {Attach a file}
155 SetLength(MyAttachments, 1);
156 FillChar(MyAttachments[0], SizeOf(MyAttachments), 0);
157 MyAttachments[0].nPosition := Cardinal(-1);
158 MyAttachments[0].lpszPathName := PChar('' + MyFileName);
159 {Fill the message structure}
160 FillChar(MyMapiMessage, SizeOf(TMapiMessage), 0);
161 with MyMapiMessage do
162 begin
163 lpszSubject := PChar(MySubject);
164 lpszNoteText := PChar(MyBody);
165 nRecipCount := Length(MyRecipients);
166 lpRecips := @MyRecipients[0];
167 if Length(MyAttachments) > 0 then
168 begin
169 nFileCount := Length(MyAttachments);
170 lpFiles := @MyAttachments[0];
171 end;
172 end;
173 if MapiSendMail(0, Application.Handle, MyMapiMessage, MAPI_DIALOG or
174 MAPI_LOGON_UI or MAPI_NEW_SESSION, 0) <> 0 then
175 MessageDlg(MySendError, mtError, [mbOK], 0);
176 end;
Solve 4:
177 procedure TForm1.Button1Click(Sender: TObject);
178 var
179 App: _Application;
180 Item: MailItem;
181 begin
182 App := CoOutlookApplication.Create;
183 Item := App.CreateItem(olMailItem) as MailItem;
184 Item.Subject := 'My Subject';
185 Item.To_ := 'nobody@nowhere.com';
186 Item.Attachments.Add('C:\test.txt', EmptyParam, EmptyParam, EmptyParam);
187 Item.Send;
188 end;
|