电子邮件附件时出现语法错误

时间:2014-04-28 10:37:40

标签: asp.net email email-attachments

我正在尝试使用文件上传附加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);
    }
}

2 个答案:

答案 0 :(得分: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);
// ...

答案 1 :(得分:0)

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