System.Net.Mail发送附件总是失败
protected void btnSend_Click(object sender, EventArgs e){
if (this.txtReceiver.Text != string.Empty && this.txtSender.Text != string.Empty)
{
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress(txtSender.Text.Trim(), "TT", Encoding.UTF8);
myMail.To.Add(new MailAddress(txtReceiver.Text.Trim(), "Troy", Encoding.UTF8));
myMail.Subject = txtSubject.Text.Trim();
myMail.Body = txtContent.Text.Trim();
myMail.Priority = MailPriority.High;
string sFilePath = this.upFile.PostedFile.FileName;
FileInfo fi = new FileInfo(sFilePath);
if (fi.Exists)
{
Attachment myAttachment = new Attachment(sFilePath, );
disposition = myAttachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(sFilePath);
disposition.ModificationDate = File.GetLastWriteTime(sFilePath);
disposition.ReadDate = File.GetLastAccessTime(sFilePath);
myMail.Attachments.Add(myAttachment);
}
SmtpClient client = new SmtpClient("smtp., 25);
client.Credentials = new (this.txtSUser.Text.Trim(), this.txtEPwd.Text.Trim());
client.Send(myMail);
}
}
这是代码,问题是,如果我在FileUpload控件中添加一个文件,路径是C:\user\desktop,那么FileInfo对象fi在调用exists()函数就永远是false,如果把if条件拿掉,提示该文件不在C:\Program Files (x86)\Common Files\microsoft shared\DevServer里面。然后我试着把文件放进这个文件夹中,又发现一个问题,提示是invalid mail header '周',为什么不添加附件什么问题都没有,只有一添加一个附件就会有问题呢?求高手解答!!最好两个问题都能给点提示,文件路径和mail header的编码问题。。