发送电子邮件使用System.Net.Mail attchement [英] Send email with attchement using System.Net.Mail

查看:2248
本文介绍了发送电子邮件使用System.Net.Mail attchement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用System.Net.Mail通过我的应用程序发送电子邮件。我试图发送一个带有附件的电子邮件与下面的代码



 收集和LT;字符串> MailAttachments =新的收集和LT;串>(); 
MailAttachments.Add(C:\\Sample.JPG);
= MAILMESSAGE MAILMESSAGE新();
的foreach(在emailNotificationData.MailAttachments字符串文件路径)
{
的FileStream FILESTREAM = File.OpenWrite(文件路径);使用
(FILESTREAM)
{
附件附件=新的附件(FILESTREAM,文件路径);
mailMessage.Attachments.Add(附件);
}
}
SmtpClient smtpClient =新SmtpClient();
smtpClient.Host = SmtpHost;
smtpClient.Send(MAILMESSAGE);

当我发送带有附件的电子邮件,它抛出一个异常,如下所示。

 无法访问已关闭的文件。 
在System.IO .__ Error.FileNotOpen()
在System.IO.FileStream.Read(byte []数组,偏移的Int32,的Int32计数)$ B $在System.Net.Mime.MimePart b 。发送(BaseWriter作家)
在System.Net.Mime.MimeMultiPart.Send(BaseWriter作家)
在System.Net.Mail.Message.Send(BaseWriter作家,布尔sendEnvelope)
。在System.Net.Mail.MailMessage.Send(BaseWriter作家,布尔sendEnvelope)
在System.Net.Mail.SmtpClient.Send(消息MAILMESSAGE)


解决方案

的结束大括号的语句关闭文件流>:

 使用(FILESTREAM)
{
附件附件=新的附件(FILESTREAM,文件路径);
mailMessage.Attachments.Add(附件);
} //< - 文件流在这里收



不过,流阅读的 stmpClient.Send(MAILMESSAGE),它不再被打开。



最简单的时间解决方案是只提供文件名称,而不是流:

 收集和LT;字符串> MailAttachments =新的收集和LT;串>(); 
MailAttachments.Add(C:\\Sample.JPG);

= MAILMESSAGE MAILMESSAGE新();
的foreach(在emailNotificationData.MailAttachments字符串文件路径)
{
附件附件=新的附件(文件路径);
mailMessage.Attachments.Add(附件);
}
SmtpClient smtpClient =新SmtpClient();
smtpClient.Host = SmtpHost;
smtpClient.Send(MAILMESSAGE);

通过该解决方案,.NET库将不用担心打开,读取和关闭文件。


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.

这篇关于发送电子邮件使用System.Net.Mail attchement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆