我通过.net的smtp发送文件:
private void SendMail()
{
var _msg = new MailMessage();
const string cEmailUsername = "xxxxxx";
const string cEmailPassword = "zzzzzz";
const string cSmtpClient = "yyyyyyy";
try
{
using (var smtp = new SmtpClient(cSmtpClient))
{
_msg.From = string.IsNullOrEmpty(Configuration.CompanyEmailAddress)
? new MailAddress(cEmailUsername)
: new MailAddress(Configuration.CompanyEmailAddress.ToLower());
_msg.Subject = string.Format("Request for .Net Support - {0} - {1}", Configuration.CompanyID,
Configuration.GetCompanyName());
_msg.Body = string.Format("Propane.exe date: {0}{1}{1}{2}", Configuration.VersionDate, Environment.NewLine,
"Check parsing log file attached.");
_msg.To.Add("eric@suburbansoftware.com");
_msg.Attachments.Add(new Attachment(_logFile));
var cred2 = new NetworkCredential(cEmailUsername, cEmailPassword);
smtp.Port = 587;
smtp.Credentials = cred2;
smtp.Timeout = 0;
smtp.Send(_msg);
}
var oldfile = _logFile + ".old";
if (File.Exists(oldfile))
{
File.Delete(oldfile);
}
File.Move(_logFile,oldfile);
}
catch (Exception ex)
{
richTextBox_Message.Text += ex.Message;
}
}
当我到达file.move时,我收到一个正在使用的文件。如果我注释掉使用部分,移动就可以了。
有没有办法解决这个问题,还是我做错了?
答案 0 :(得分:2)
Using
仅适用于smtp
,而不适用于_msg
。此变量是包含附件的变量,不会被丢弃。在完成using
部分后立即包含此代码,一切都应该没问题:
_msg.Dispose();
_msg = null;