【问题标题】:Getting syntax error while email attachment电子邮件附件时出现语法错误
【发布时间】:2014-04-28 10:37:40
【问题描述】:

我正在尝试使用文件上传附加 excel 文件,但出现语法错误。

我尝试了所有可能,但无法解决。

我们将不胜感激。

得到错误是-

the best overloaded method match for 'system.collections.objectmodel.collection
<system.net.mail.attachment>.Add(System.Net.Mail.Attachment)' has some invalid arguments

我的代码是-

protected void btnSend_Click(object sender, EventArgs e)
{
    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
    msg.From = new MailAddress(txtFrom.Text);
    msg.To.Add(txtTo.Text);
    msg.Subject = txtSubject.Text;
   if (FileUpload1.HasFile)
    {
        String FileName = FileUpload1.PostedFile.FileName;
       // MailAttachment mailAttachment = new MailAttachment(FileName, MailEncoding.Base64);
        System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, 
                                       FileName);
        msg.Attachments.Add(mailAttachment);
    }

    using (SmtpClient client = new SmtpClient())
    {
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Send(msg);
    }
}

【问题讨论】:

    标签: asp.net email email-attachments


    【解决方案1】:

    .NET 框架中有两个命名空间允许发送邮件:System.Net.MailSystem.Web.Mail。两者都包含相似的类。我怀疑文件顶部的using 指令中有对 System.Web.Mail 的引用。

    您可以使用完全限定名称System.Net.Mail.MailMessage 创建 MailMessage 对象。另一方面,对于 MailAttachment,您没有限定命名空间。为了完成这项工作,您需要决定采用哪个命名空间。 System.Net.Mail 提供了一个 SMTP 实现。所以我建议只使用 System.Net.Mail 中的类:

    if (FileUpload1.HasFile)
    {
        String FileName = FileUpload1.PostedFile.FileName;
        System.Net.Mail.Attachment mailAttachment = 
                new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, 
                                               FileName);
    // ...
    

    【讨论】:

    • 异常---找不到文件'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\ExpenseReport (5).xls'。
    • @AzharShahid:您需要指定文件的完整路径。检查FileName 变量的内容。
    • @Markus- 我正在从文件上传控件获取文件。
    • @AzharShahid:我已经更新了使用 FileUpload 流的答案。希望对您有所帮助。
    • 也更改了我有问题的代码.. 出现语法错误
    【解决方案2】:

    MailAttachment 不是从System.Net.Mail.Attachment 派生的。请改用System.Net.Mail.Attachment

    public static void CreateMessageWithAttachment(string server)
    {
        // Specify the file to be attached and sent. 
        // This example assumes that a file named Data.xls exists in the 
        // current working directory. 
        string file = "data.xls";
        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "jane@contoso.com",
           "ben@contoso.com",
           "Quarterly data report.",
           "See the attached spreadsheet.");
    
        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(file);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);
    

    http://msdn.microsoft.com/en-us/library/vstudio/system.net.mail.attachment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-24
      • 2016-02-26
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      相关资源
      最近更新 更多