WinBatch® Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: Die on December 11, 2013, 03:27:38 AM

Title: Email using the System.Net.Mail class : hjow to add an attachment
Post by: Die on December 11, 2013, 03:27:38 AM
I'm trying to send email using the system.net.mail class (based on  http://forum.winbatch.com/index.php?topic=179.msg601#msg601). Without attachment it works.

I try to add an attachment using the command : Mailmessage.Attachments.Add("c:\temp\file.txt")

This creates winbatch error 1298: Ole: Error code not recognized
The extra error info gives :

COM/CLR Exception:
mscorlib
Method 'System.Net.Mail.AttachmentCollection.Add' not found

Anybody any idea how to add attachments (one or multiple) to an email?
Title: Re: Email using the System.Net.Mail class : hjow to add an attachment
Post by: stanl on December 11, 2013, 07:28:25 AM
I believe you are going to have to create an instance, something like


attachment = ObjectClrNew('System.Net.Mail.Attachment', file)
then
Mailmessage.Attachments.Add(attachment)

but I am sure it is a bit more complicated than this.





Title: Re: Email using the System.Net.Mail class : hjow to add an attachment
Post by: JTaylor on December 11, 2013, 07:55:09 AM
You are correct on the first part Stan.  That will do the job.

Incorrect in that it is not more complicated  :)

It can get more complicated though.


Jim
Title: Re: Email using the System.Net.Mail class : hjow to add an attachment
Post by: Deana on December 11, 2013, 09:27:53 AM
Check out the following code sample that includes an email file attachment: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/dotNet/System_Net/Mail+Simple~SMTP~Email~Message.txt
Title: Re: Email using the System.Net.Mail class : hjow to add an attachment
Post by: stanl on December 12, 2013, 08:15:16 AM
Quote from: JTaylor on December 11, 2013, 07:55:09 AM
You are correct on the first part Stan.  That will do the job.

Incorrect in that it is not more complicated  :)

It can get more complicated though.


Jim

As I believe I said. The attachment class is overloaded, and it appears you often need to specify a MediaType when attaching pictures, or an avi. Also, there are touchy size limits
Title: Re: Email using the System.Net.Mail class : hjow to add an attachment
Post by: JTaylor on December 12, 2013, 08:49:13 AM
I know....I was partly affirming that what you posted would work but also trying to brighten your day...but apparently failed  :'(

Jim
Title: Re: Email using the System.Net.Mail class : hjow to add an attachment
Post by: Die on December 12, 2013, 11:31:12 PM
Stanl and Deana, thanks for the reply. The intermediate step with the creation of the instance solved my problem. Mails are going out with the attachment. Thanks.