-1

I want to create excel file and send it as attachment via email hear is my code for creating excel file but i can't send it by email , the email sent but without attachment.

this for creating excel file.


DataTable dt = new DataTable("Grid");
            dt.Columns.AddRange(new DataColumn[4] { new DataColumn("H_Id"),
                                    new DataColumn("H_Name"),
                                    new DataColumn("H_PassNo"),
                                    new DataColumn("H_Pass_Issue"),
 });

            var marydatas = from MarryData in this._context.MarryData.Take(10)
                            select MarryData;

            foreach (var marryData in marydatas)
            {
                dt.Rows.Add(marryData.H_Id, marryData.H_Name, marryData.H_PassNo, marryData.H_Pass_Issue);}
  byte[] bytes = null;
            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(dt);
                using (MemoryStream stream = new MemoryStream())
                {
                    wb.SaveAs(stream);
                    bytes = stream.ToArray();
                }
            }

and this for sending email


var message = new MimeMessage();
            message.From.Add(new MailboxAddress("test project", "maazonuae@gmail.com"));
            message.To.Add(new MailboxAddress("naren", "a.saaeed81@gmail.com"));
            message.Subject = "test maazon";
            message.Body = new TextPart("HTML Table Exported Excel Attachment")
            {
                Text = "hello"
            };
            var builder = new BodyBuilder();

            builder.Attachments.Add("Customers.xlsx", new MemoryStream(bytes));

            var body = builder.ToMessageBody();


            var emailBody = new MimeKit.BodyBuilder
            {

            };
            emailBody.Attachments.Add("Customers.xlsx", bytes);

            using (var client = new SmtpClient())
            {
         

   client.Connect("smtp.gmail.com", 587, false);
            client.Authenticate("maazonuae@gmail.com", "maazonuae2021");
            client.Send(message);
            client.Disconnect(true);
        }

so when it run just send email without attachment and send (Text = "hello") in attached file not in body message.

thanks

  • Does this answer your question? [Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird](https://stackoverflow.com/questions/2825950/sending-email-with-attachments-from-c-attachments-arrive-as-part-1-2-in-thunde) – Mohammad Aghazadeh Oct 04 '21 at 07:51

1 Answers1

-1

i found solution

   var message = new MimeMessage();
            message.From.Add(new MailboxAddress(Joey", "joey@friends.com));
            message.To.Add(new MailboxAddress("Alice", "alice@wonderland.com"));
            
            message.Subject = "How you doin?";

            var builder = new BodyBuilder();

            // Set the plain-text version of the message text
            builder.TextBody = @"Hey Alice,

                What are you up to this weekend? Monica is throwing one of her parties on
                Saturday. I was hoping you could make it.

                Will you be my +1?

                -- Joey
                ";

          
            builder.Attachments.Add("myFile.xlsx", bytes);
            // Now we just need to set the message body and we're done
            message.Body = builder.ToMessageBody();

            ////////////////////////////////////////////////////////////


            using (var client = new SmtpClient())
            {
                client.Connect("smtp.gmail.com", 587, false);
                client.Authenticate("maazonuae@gmail.com", "maazonuae2021");
                client.Send(message);
                client.Disconnect(true);
            }

  • This answer can be improved by providing more information on what the code does and how it helps the OP. – Tyler2P Oct 04 '21 at 08:30