Below is the source code that will send email with attachment.
using System.Net.Mail;
MailMessage oMsg = new MailMessage();
oMsg.From = new MailAddress(“Email”, “Name”);
oMsg.To.Add(new MailAddress(“Email”,”Name”));
oMsg.Subject = “subject”;
oMsg.Body = “Mail contain”;
Attachment data = new Attachment(“File1″);
oMsg.Attachments.Add(data);
SmtpClient c = new SmtpClient(“SMTP”);
c.Send(oMsg);
oMsg.Dispose();
I have been facing problem with cannot delete my attachment from source. After troubleshooting, I found out that I forgot to put oMsg.Dispose(). Make sure you do so.
Related topics:
Email chinese character – C# source code
[...] Related topics: Send email with attachment c# source code [...]
thank you, had the same problem
@Marijke: You are welcome.
For all clarity.. To make DotNet free the attachment you should dispose of the attachment, not the Mail object!
Disposing of the mail object does result in disposing the attachment aswel!
But, in my case i can not dispose the Mail object untill sometime later, this is why the solution posted here was quite useless to me.. But it dit help me on my way.
Here is C# code example of clearing the attachment.. simply use the Dispose of the attachment object itself instead of the mail object.
Attachment tempAtt = new Attachment(Attachment);
xMail.Attachments.Add(tempAtt);
tempAtt.Dispose();