错误CS1729:'Attachment'不包含带有2个参数的构造函数

时间:2012-08-14 18:15:21

标签: asp.net visual-studio-2010 c#-4.0

我正在尝试通过smtp协议发送带附件的邮件,所以我在http://csharpdotnetfreak.blogspot.com/2009/10/send-email-with-attachment-in-aspnet.html中找到了本教程。并尝试了下面的简单编码,对象是为附件正确创建的,但它告诉我错误不带2个参数。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class composemail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SendMail()
{
    MailMessage mail = new MailMessage();
    mail.To.Add(YourEmail.Text);
    mail.From = new MailAddress(YourName.Text);
    mail.Subject = YourSubject.Text;
    mail.Body = Comments.Text;
    mail.IsBodyHtml = true;
    if (FileUpload1.HasFile)
    {
        mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    SendMail();
}
}

1 个答案:

答案 0 :(得分:1)

正如M4N所提到的,你不能直接在课堂上拥有代码。您需要将其封装在方法中:

using System.IO;
using System.Net.Mail;

namespace AttachmentTest
{
  class Program
  {
    static void Main(string[] args)
    {
      var mail = new MailMessage();
      var fs = new FileStream("somepath", FileMode.Open);
      var att = new Attachment(fs, "");
      mail.Attachments.Add(att);
    }
  }
}