Mailkit broken attachment file C#

41 views Asked by At

I use mailkit to send some emails. Now I need to use to send some attachment files. When I send some txt files, there is no problem, but when I try to send jpg, pdf, zip - The file come broken ... Can someone help me to fix that problem ? I need to send and simple txt, and other files.

I use Mailkit to send.

Thank you very much !

        private string ReadAndReplaceAttachment(string filePath, string recipientemail, string senderemail, string sendername, string newFileName)
        {
            Replacer replacer = new Replacer();
            string tempFile = Path.Combine(Helpers.tempPath, replacer.DoReplace(newFileName, recipientemail, senderemail, sendername));//Path.GetTempPath() + Guid.NewGuid().ToString() + Path.GetExtension(filePath);
            string contentResult = "";

            using (StreamReader sr = File.OpenText(filePath))
            {
                contentResult = sr.ReadToEnd();
                contentResult = replacer.DoReplace(contentResult, recipientemail, senderemail, sendername);
                sr.Close();
            }

            using (var tw = new StreamWriter(tempFile, true))
            {
                tw.Write(contentResult);
            }

            return tempFile;
        }
                foreach (var attachment in letter.attachment)
                {
                    newAttachment = ReadAndReplaceAttachment(attachment.filePath, recipientData.email, senderData.senderEmail, senderData.senderName, attachment.fileName);
                    
                    bodyBuilder.Attachments.Add(newAttachment);

                    File.Delete(newAttachment);
                    Console.WriteLine(newAttachment);
                }

I search on stuckoverflow, but didnt find solution ...

0

There are 0 answers