简体   繁体   中英

C# SMTPClient - send attachment to the e-mail

I'm using the code below within a script task in SSIS in order to send an e-mail. What I also want, is to send an attachment to the e-mail which is a file on the local computer and the name of the file is formed by name of the file and GETDATE() function and is stored in a variable. How can I adapt my code in order to use the file from that variable as the attachment?

  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);

I think this should work, if I have understood your question properly,

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM