无法发送带有附件的电子邮件 [英] Unable to send E-mail with Attachment

查看:210
本文介绍了无法发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我试图利用管理员网站上的电子邮件发送任务。

My issue is this that I am trying to make use of email sending task at my website for the admin individual.

当他从可用列表中选择电子邮件ID时数据,添加附件并发送电子邮件,但用户从未收到过。但是,如果他使用的是简单邮件,即。

When he selects the email ids from the available data, adds an attachment and sends the email, it was never received by user. However, if he is using simple mailing ie. without any attachment, user receives it.

您能帮忙吗?

我的编码如下:-

public partial class SahibAdmin_emailNewsletter : System.Web.UI.Page
{
  // ... 

  private void SendNewsletter(string emailId)
  {
        System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
        message.To = emailId;
        message.From = "info@sahibimports.com";
        message.Subject = "Please See: Newsletter from Sahib imports";
        message.BodyFormat = System.Web.Mail.MailFormat.Text;
        message.Body = txtBody.Text.ToString();
        if (msgUpload.HasFile)
        {
              //string strFileName = msgUpload.FileName;
              //msgUpload.PostedFile.SaveAs(Server.MapPath(strFileName));
              //System.Web.Mail.MailAttachment attach =
              //  new System.Web.Mail.MailAttachment(Server.MapPath(strFileName));
              //message.Attachments.Add(attach);

              message.Attachments.Add(new Attachment(
                 FileUpload.PostedFile.InputStream, FileUpload.FileName));

        }
        System.Web.Mail.SmtpMail.Send(message);
        Response.Flush();

  }


推荐答案

http://msdn.microsoft.com/en-us/library/6sdktyws.aspx 您的附件构造函数正在尝试使用第二个参数设置ContentType,但是您要传递文件名,确定正确吗?

According to http://msdn.microsoft.com/en-us/library/6sdktyws.aspx your Attachment constructor is trying to set the ContentType with the second parameter but you're passing in a filename, are you sure that is correct?

您可能应该将该部分更改为以下内容:

You should probably change that part to something like:

ContentType contentType = // create suitable type here based on your file format
Attachment attachment = new Attachment(
    FileUpload.PostedFile.InputStream,
    contentType
);
attachment.ContentDisposition.FileName = FileUpload.FileName;
message.Attachments.Add(attachment);

这篇关于无法发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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