电子邮件附件未通过

时间:2014-06-18 20:50:15

标签: c# email

我有一个c#程序,它使用fileuploader控件来收集文件名的路径以附加到电子邮件。当我在调试中使用附件运行网页时,一切都很好,电子邮件附带附件。但是,当我将其部署到服务器时,电子邮件会通过,但附件不会。

MailMessage message = new System.Net.Mail.MailMessage();
    message.To.Add("test1@test.com");
    message.Subject = "Project Created";
    message.From = new MailAddress("noreply@test.com");
    message.Body = "You have recieved a project request from " + TxtContactName.Text + " Project Name: " + TxtProjectName.Text + " Priority: " + DDLPriority.SelectedValue.ToString() + " Business Area: " + DDLBusinessArea.SelectedValue.ToString();

    if (FileUpload1.FileName.Length > 0)
    {
        if (File.Exists(FileUpload1.PostedFile.FileName))
            message.Attachments.Add(new Attachment(FileUpload1.PostedFile.FileName));
    }

    SmtpClient smtp = new SmtpClient("PMFex02");
    NetworkCredential cred = new NetworkCredential();
    cred.UserName = "test@test.com";
    cred.Password = "test";
    cred.Domain = "test.com";

    smtp.UseDefaultCredentials = false;

    smtp.Send(message);

我已尝试使用和不使用凭据设置,但它的行为方式相同。 知道为什么附件在部署到服务器时无法完成?

2 个答案:

答案 0 :(得分:0)

在邮件主题或正文中添加一些额外内容,以查看您获得的附件的距离:

MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("test1@test.com");
message.Subject = "Project Created";
message.From = new MailAddress("noreply@test.com");
message.Body = "You have recieved a project request from " + TxtContactName.Text + " Project Name: " + TxtProjectName.Text + " Priority: " + DDLPriority.SelectedValue.ToString() + " Business Area: " + DDLBusinessArea.SelectedValue.ToString();

if (FileUpload1.FileName.Length > 0)
{
  message.Subject .= string.Format(" ({0} bytes)", FileUpload1.FileName.Length);
  if (File.Exists(FileUpload1.PostedFile.FileName))
  {
    message.Subject .= " E";
    message.Attachments.Add(new Attachment(FileUpload1.PostedFile.FileName));
  }
}

它可能只对你有意义,但如果留在那里也不会有任何伤害。

答案 1 :(得分:0)

看起来您实际上并未使用您的凭据进行身份验证,这可能会导致附件被丢弃在垃圾邮件过滤器中。试试这个:

SmtpClient smtp = new SmtpClient("PMFex02");

NetworkCredential cred = new NetworkCredential();
cred.UserName = "test@test.com";
cred.Password = "test";
cred.Domain = "test.com";

smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);

您可能还希望添加一个高于默认值的smtp.Timeout值,可能会防范被认为过大的文件。