Adding an attachment to email using C#

前端 未结 4 1491
广开言路
广开言路 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: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);
    

提交回复
热议问题