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();
}
}
4

1 回答 1

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);
    }
  }
}
于 2012-08-14T18:35:02.397 回答