Get file to send as attachment from byte array

后端 未结 2 495
囚心锁ツ
囚心锁ツ 2021-01-05 14:13

I have an ASP.NET MVC application that has to send an email to a list of recipients with an attachment detailing a specific \"Project\". I get the details for this from a re

2条回答
  •  悲&欢浪女
    2021-01-05 14:56

    Get the file data in a byte[]

    byte[] binaryFile = //  get your file data from the SSRS ...
    string filename = "SSRS.pdf";
    

    Prepare a list or array of the destination addresses:

    string[] addresses = // get addresses somehow (db/hardcoded/config/...)
    

    sending smtp message through SmtpClient:

    MailMessage mailMessage= new MailMessage();
    
    mailMessage.From = new MailAddress("sender email address goes here");
    
    // Loop all your clients addresses
    foreach (string address in addresses)
    {
        mailMessage.To.Add(address);    
    }
    
    mailMessage.Subject = "your message subject goes here";
    mailMessage.Body = "your message body goes here";
    
    MemoryStream memoryStream = new MemoryStream(binaryFile);
    mailMessage.Attachments.Add( new Attachment( memoryStream, filename , MediaTypeNames.Application.Pdf ));
    
    SmtpClient smtpClient = new SmtpClient();
    smtpClient.Send(mailMessage);
    

提交回复
热议问题