I am using System.Net.Mail to send emails through my application. I was trying to send emails with the attachments with following code.
Collection<string> MailAttachments = new Collection<string>();
MailAttachments.Add("C:\\Sample.JPG");
mailMessage = new MailMessage();
foreach (string filePath in emailNotificationData.MailAttachments)
{
FileStream fileStream = File.OpenWrite(filePath);
using (fileStream)
{
Attachment attachment = new Attachment(fileStream, filePath);
mailMessage.Attachments.Add(attachment);
}
}
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = SmtpHost;
smtpClient.Send(mailMessage);
When I send the emails with the attachments it throw an exceptions as follows.
Cannot access a closed file.
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.Net.Mime.MimePart.Send(BaseWriter writer)
at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
at System.Net.Mail.Message.Send(BaseWriter writer, Boolean sendEnvelope)
at System.Net.Mail.MailMessage.Send(BaseWriter writer, Boolean sendEnvelope)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
The ending curly brace of your using
statement closes the file stream:
using (fileStream)
{
Attachment attachment = new Attachment(fileStream, filePath);
mailMessage.Attachments.Add(attachment);
} // <-- file stream is closed here
However, the stream is read at the time of the stmpClient.Send(mailMessage)
, where it is not open anymore.
The simplest solution is to provide just the file name instead of a stream:
Collection<string> MailAttachments = new Collection<string>();
MailAttachments.Add("C:\\Sample.JPG");
mailMessage = new MailMessage();
foreach (string filePath in emailNotificationData.MailAttachments)
{
Attachment attachment = new Attachment(filePath);
mailMessage.Attachments.Add(attachment);
}
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = SmtpHost;
smtpClient.Send(mailMessage);
With this solution, the .NET library will have to worry about opening, reading and closing the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With