C#SMTPClient-将附件发送到电子邮件

时间:2018-08-13 13:59:35

标签: c# ssis

我正在SSIS中的脚本任务中使用下面的代码来发送电子邮件。我还想要向电子邮件发送附件,该附件是本地计算机上的一个文件,文件名由文件名和GETDATE()函数组成,并存储在变量中。我该如何修改我的代码,以便将该变量中的文件用作附件?

  MailMessage msg = new MailMessage("myemail@gmail.com", "t@mail.ro", "Teee", "WWWWW");
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    client.EnableSsl=true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Credentials = new NetworkCredential("myemail@gmail.com", "password");

    System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
    contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
    contentType.Name = "ERPOrder(s)_20180810050711.csv";
    msg.Attachments.Add(new Attachment("‪C:\\FMLogistics\\ERPOrder(s)_20180810050711.csv", contentType));

    client.Send(msg);

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,我认为这应该可行

string FileName = "ERPOrder(s)_20180810050711.csv";
string FilePath = Path.Combine("‪C:\\FMLogistics", FileName);

contentType.Name = FileName;
msg.Attachments.Add(new Attachment(FilePath, contentType));