【问题标题】:asp.net send email with excel attachementasp.net 发送带有 excel 附件的电子邮件
【发布时间】:2021-11-24 07:58:25
【问题描述】:

我想创建 excel 文件并通过电子邮件将其作为附件发送 听到是我创建 excel 文件的代码,但我无法通过电子邮件发送,发送的电子邮件但没有附件。

这个用于创建 excel 文件。


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

这个用于发送电子邮件


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

因此,当它运行时,只需发送不带附件的电子邮件,并在附件中发送 (Text = "hello"),而不是在正文中。

谢谢

【问题讨论】:

标签: asp.net-core razor-pages


【解决方案1】:

我找到了解决办法

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

【讨论】:

  • 可以通过提供有关代码的作用以及它如何帮助 OP 的更多信息来改进此答案。
猜你喜欢
  • 2019-09-25
  • 2015-09-09
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
相关资源
最近更新 更多