2012-08-14 264 views
0

我试图通过smtp协议发送带有附件的邮件,所以我在http://csharpdotnetfreak.blogspot.com/2009/10/send-email-with-attachment-in-aspnet.html中找到了本教程。并尝试了以下简单的编码,该对象正确地创建了附件,但它告诉我不会带2个参数的错误。错误CS1729:'附件'不包含需要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(); 
} 
} 
+0

什么是确切的错误消息? – 2012-08-14 18:19:38

+0

嗨。谢谢我的错误是:错误CS1729:'附件'不包含带2个参数的构造函数 – chris 2012-08-14 18:22:30

+0

这确实很奇怪。顺便说一句。 '''''''''''''''''''''''''''''''''''''''' – 2012-08-14 18:25:55

回答

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); 
    } 
    } 
} 
+0

你好,谢谢你,我试着你告诉和它的工作。但检查下面的链接,我提到这个链接学习他们已经提到像这样.. http://csharpdotnetfreak.blogspot.com/2009/10/send-email-with-attachment-in-aspnet.html – chris 2012-08-14 18:58:59

+0

在该链接他们做得正确。代码封装在方法中。 – 2012-08-14 19:06:24

相关问题