我正在尝试通过 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();
}
}