Adding an attachment to email using C#

前端 未结 4 1482
广开言路
广开言路 2020-12-01 13:45

I\'m using the following code from this answer Sending email in .NET through Gmail. The trouble I\'m having is adding an attachment to the email. How would I add an attachme

相关标签:
4条回答
  • 2020-12-01 14:08

    The message object created from your new MailMessage method call has a property .Attachments.

    For example:

    message.Attachments.Add(new Attachment(PathToAttachment));
    
    0 讨论(0)
  • 2020-12-01 14:21

    Using the Attachment class as proposed in the MSDN:

    // 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);
    
    0 讨论(0)
  • 2020-12-01 14:21

    A one line answer:

    mail.Attachments.Add(new System.Net.Mail.Attachment("pathToAttachment"));
    
    0 讨论(0)
  • 2020-12-01 14:22

    Correct your code like this

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("your attachment file");
    mail.Attachments.Add(attachment);
    

    http://csharp.net-informations.com/communications/csharp-email-attachment.htm

    hope this will help you.

    ricky

    0 讨论(0)
提交回复
热议问题