在“附件mail1 =新附件(路径);”中获取文件附件错误&安培;在catch块中抛出异常“操作已经超时。”在发送电子邮件 [英] Getting an error with file attachment in "Attachment mail1 = new Attachment(path);" & throwing an exception in catch block "The operation has timed out." in sending email

查看:122
本文介绍了在“附件mail1 =新附件(路径);”中获取文件附件错误&安培;在catch块中抛出异常“操作已经超时。”在发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()));
Fileupload1.PostedFile.SaveAs(Server.MapPath("~\\File\\" + FileName.Trim()));
    string path = "~\\File\\" + FileName;
    lblUpload.Text = "file is uploaded";
  Attachment mail1 = new Attachment(Server.MapPath("~\\File\\" + FileName.Trim())); 
      obj.Attachments.Add(mail1);
}
else {
    lblUpload.Text = "file not uploaded";
}

SmtpClient smt = new SmtpClient("smtp.gmail.com",465 );

smt.EnableSsl = true;
 smt.Timeout = 10000;
smt.UseDefaultCredentials = false;
smt.Credentials = new System.Net.NetworkCredential(txtFrom.Text, txtPwd.Text, "www.gmail.com");

 smt.DeliveryMethod = SmtpDeliveryMethod.Network;
        obj.BodyEncoding = UTF8Encoding.UTF8;
        smt.Credentials = CredentialCache.DefaultNetworkCredentials;

try
{
    smt.Send(obj);
}
catch(Exception e1) // The operation has timed out.
{
    throw e1;
}

推荐答案

附加时使用完整路径

use full path when you attaching
Attachment mail1 = new Attachment(Server.MapPath("~\\File\\" + FileName.Trim());



并查看这些答案,以便使用gmail发送邮件
http:// stackoverflow .com / questions / 1311749 / c-sharp-smtpclient-class-not-able-send-email-using-gmail [ ^ ]


这篇关于在“附件mail1 =新附件(路径);”中获取文件附件错误&安培;在catch块中抛出异常“操作已经超时。”在发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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