0

I added a pdf file to my MailMassege and After sending it, I got the Email with pdf file that cannot be opened and it's size is much smaller than the original file, what am I missing ?? this is my code :

 public  void createMailWithBytesAttachment(string to,string from, string pdfUrl, string body,string FileName,string subject) {
            MailMessage oMail = new MailMessage();
            using (WebClient client = new WebClient())
            {
                var bytes = client.DownloadData(pdfUrl);
                string base64String = Convert.ToBase64String(bytes);
            }
            var fileName = FileName;
            var myByteArray = System.Text.Encoding.UTF8.GetBytes(pdfUrl);
            MemoryStream strm = new MemoryStream(myByteArray);
            Attachment data = new Attachment(strm, fileName);
            ContentDisposition disposition = data.ContentDisposition;
            //data.ContentId = fileName;
            //data.ContentDisposition.Inline = true;
            oMail.Attachments.Add(data);
            oMail.From = new MailAddress(from);
            oMail.To.Add(to);
            oMail.Subject = subject;
            oMail.Body = body;
            SmtpClient oSmtp = new SmtpClient();
            SmtpClient oServer = new SmtpClient("xxx.xxx.xxx.xxx");
            oServer.Port = yy;
            oServer.EnableSsl = false;
            try
            {
                oServer.Send(oMail);
            }
Damkulul
  • 1,406
  • 2
  • 25
  • 59
  • 1
    The effect of `System.Text.Encoding.UTF8.GetBytes(pdfUrl)` is to encode the URL itself as bytes, not the content found at the URL. – Noah May 09 '21 at 22:21
  • 1
    If `client.DownloadData(pdfUrl)` really does return bytes, then just pass that to the `MemoryStream`, with no conversions in-between. – Noah May 09 '21 at 22:44
  • @Noah thank you please write it as answer and i'll mark it as solved – Damkulul May 10 '21 at 05:38

0 Answers0