使用fileupload& ;;给我一个文件附件错误发送邮件失败 [英] Giving me an Error with file attachment using fileupload & failure of a sending mail
本文介绍了使用fileupload& ;;给我一个文件附件错误发送邮件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void btnSubmit_Click(对象发件人,EventArgs e)
{
MailMessage obj = new MailMessage(txtFrom.Text,txtTo.Text, txtSubject.Text,txtBody.Text);
if (Fileupload1.HasFile)
{
String FileName = Path.GetFileName(Fileupload1.PostedFile.FileName);
Fileupload1.PostedFile.SaveAs(Server.MapPath( 〜\\File\\ + FileName.Trim())); //
string path = 〜\\File \\ + FileName.Trim(); < span class =code-comment> //
lblUpload.Text = 上传文件;
附件mail1 = 新附件(路径); // 无法找到DirectoryNotFoundException文件或目录。
obj.Attachments。加入(MAIL1);
}
else
{
lblUpload.Text = 文件未上传;
}
SmtpClient smt = new SmtpClient();
smt.Port = 587 ;
smt.Port = 465 ;
smt.Host = smtp.gmail.com;
smt.Host = smtp.mail.yahoo.com;
smt.Host = smtp.live.com;
smt.EnableSsl = true ;
smt.UseDefaultCredentials = false ;
smt.Credentials = new System.Net.NetworkCredential(txtFrom.Text,txtPwd.Text);
尝试
{
smt.Send(obj);
}
catch (例外e1) // System.Net.Mail.SmtpException发送邮件失败
{
throw e1;
}
}
[edit]已添加代码块 - OriginalGriff [/ edit]
[edit_2]缩进缩小,缺少}和//添加以避免格式化问题 - Nelek [/ edit_2]
解决方案
< blockquote>先生,因为我可以看到你写的变量名路径,尝试调试它我肯定你想要的路径和路径是什么进入变量会有很大不同。
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage obj = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
if (Fileupload1.HasFile)
{
String FileName = Path.GetFileName(Fileupload1.PostedFile.FileName);
Fileupload1.PostedFile.SaveAs(Server.MapPath("~\\File\\" + FileName.Trim())); // "
string path = "~\\File\\" + FileName.Trim(); // "
lblUpload.Text = "file is uploaded";
Attachment mail1 = new Attachment(path); // DirectoryNotFoundException file or directory cannot be found.
obj.Attachments.Add(mail1);
}
else
{
lblUpload.Text = "file not uploaded";
}
SmtpClient smt = new SmtpClient();
smt.Port = 587;
smt.Port = 465;
smt.Host = "smtp.gmail.com";
smt.Host = "smtp.mail.yahoo.com";
smt.Host = "smtp.live.com";
smt.EnableSsl = true;
smt.UseDefaultCredentials = false;
smt.Credentials = new System.Net.NetworkCredential(txtFrom.Text, txtPwd.Text);
try
{
smt.Send(obj);
}
catch(Exception e1)// System.Net.Mail.SmtpException Failur sending mail
{
throw e1;
}
}
[edit]Code block added - OriginalGriff[/edit]
[edit_2]Indentation reduced, missing "}" and //" added to avoid formating problems - Nelek[/edit_2]
解决方案
sir, as i can see that the variable name "Path" you have written, try debugging it and i am sure that the path you want and the path what is coming in the variable would be far different.
这篇关于使用fileupload& ;;给我一个文件附件错误发送邮件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文