问题描述
我正在使用C#.net代码发送带有附件的邮件.
邮件已成功发送,但是当我尝试从Web文件夹中删除该附件时,错误是550!该文件被另一个进程使用.
你能帮我该怎么办.
:: code ::
MailMessage邮件;
SmtpClient objsmtp;
试试
{
字符串FileName = Server.MapPath(〜//EmailTemp.htm");
字符串FileContents;
System.IO.StreamReader myStreamReader;
如果((FileName!= null))
{
myStreamReader = System.IO.File.OpenText(FileName);
FileContents = myStreamReader.ReadToEnd();
FileContents = FileContents.Replace(%Name%",tbxName.Text);
FileContents = FileContents.Replace(%Email%",tbxEmail.Text);
FileContents = FileContents.Replace(%Country%",tbxCountry.Text);
FileContents = FileContents.Replace(%Patients Testimonials%",tbxPatTestimonials.Text);
mail = new MailMessage();
字符串email ="[email protected]";
mail.To.Add(tbxEmail.Text);
mail.Bcc.Add(email);
mail.From = new MailAddress("[email protected]");
字符串attach1 = null;
字符串strFileName = null;
如果(fudReports .FileName!= null)
{
//获取对PostedFile对象的引用
//获取文件名
strFileName = Path.GetFileName(fudReports.FileName);
//在文件名前加上"attachments/",因此
//文件已保存到我们的附件目录
strFileName ="attachments/" + strFileName;
//将文件保存在服务器上
fudReports.SaveAs(Server.MapPath(strFileName));
//使用上传的文件创建电子邮件附件
附件attach = new Attachment(Server.MapPath(strFileName));
//附加新创建的电子邮件附件
mail.Attachments.Add(attach);
//存储文件名,以便我们稍后将其删除
attach1 = strFileName;
}
mail.Subject =查询邮件";
mail.Body = FileContents.ToString();
mail.Priority = MailPriority.High;
mail.IsBodyHtml = true;
objsmtp = new SmtpClient();
objsmtp.Host =服务器名称";
objsmtp.UseDefaultCredentials = true;
objsmtp.Credentials =新的System.Net.NetworkCredential("[email protected]",密码");
objsmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
objsmtp.Send(mail);
myStreamReader.Dispose();
FileName = null;
FileContents = null;
如果(attach1!= null)
File.Delete(Server.MapPath(strFileName));
attach1 = null;
}
}
catch(ex ex例外)
{
//divMess.InnerText = ex.Message;
}
终于
{
mail = null;
objsmtp = null;
}
I am using C# .net code for sending mail with attachments.
mail has been successfully send but when i try to delete that attachment from the web folder the error is 550! the file is used by another process.
can you help me what should i do.
::code::
MailMessage mail;
SmtpClient objsmtp;
try
{
string FileName = Server.MapPath("~//EmailTemp.htm");
string FileContents;
System.IO.StreamReader myStreamReader;
if ((FileName != null))
{
myStreamReader = System.IO.File.OpenText(FileName);
FileContents = myStreamReader.ReadToEnd();
FileContents = FileContents.Replace("%Name%", tbxName.Text);
FileContents = FileContents.Replace("%Email%", tbxEmail.Text);
FileContents = FileContents.Replace("%Country%", tbxCountry.Text);
FileContents = FileContents.Replace("%Patients Testimonials%", tbxPatTestimonials.Text);
mail = new MailMessage();
String email = "[email protected]";
mail.To.Add(tbxEmail.Text);
mail.Bcc.Add(email);
mail.From = new MailAddress("[email protected]");
string attach1 = null;
string strFileName = null;
if (fudReports .FileName != null)
{
// Get a reference to PostedFile object
// Get the file name
strFileName = Path.GetFileName(fudReports.FileName);
// Preced the file name with "attachments/" so
// the file is saved to our attachments directory
strFileName = "attachments/" + strFileName;
// Save the file on the server
fudReports.SaveAs(Server.MapPath(strFileName));
// Create the email attachment with the uploaded file
Attachment attach = new Attachment(Server.MapPath(strFileName));
// Attach the newly created email attachment
mail.Attachments.Add(attach);
// Store filename so we can delete it later
attach1 = strFileName;
}
mail.Subject = "Enquiry Mail";
mail.Body = FileContents.ToString();
mail.Priority = MailPriority.High;
mail.IsBodyHtml = true;
objsmtp = new SmtpClient();
objsmtp.Host = "hoshing server name";
objsmtp.UseDefaultCredentials = true;
objsmtp.Credentials = new System.Net.NetworkCredential("[email protected]", "Password");
objsmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
objsmtp.Send(mail);
myStreamReader.Dispose();
FileName = null;
FileContents = null;
if (attach1 != null)
File.Delete(Server.MapPath(strFileName));
attach1 = null;
}
}
catch (Exception ex)
{
//divMess.InnerText = ex.Message;
}
finally
{
mail = null;
objsmtp = null;
}
推荐答案
这篇关于550错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!