错误:无法从“字符串”转换到“MimeKit.MimeMessage”

发布于 2025-01-18 01:56:46 字数 1028 浏览 3 评论 0原文

我想发送电子邮件,

        public readonly IMjmlServices _mjmlServices;
            bodyBuilder.HtmlBody += $"</tr></ mj - table ></ mj - column ></ mj - section ></ mj - body ></ mjml >";
            bodyBuilder.TextBody += $"This is some plain text";
            var result = _mjmlServices.Render(bodyBuilder.HtmlBody).Result;
            SmtpClient client = new SmtpClient();
                try
                {
                    client.Connect("Smtp.gmail.com", 465, true);
                    client.Authenticate("", "");
                    client.Send(result.Html);
                    Console.WriteLine("----- EMAIL SENT!! -----");
                }

但在 client.Send(result.Html); 处出现错误,指出错误:无法从“string”转换为“MimeKit.MimeMessage” 就像 this

如何将此字符串转换为MimeKit.MimeMessage然后将其发送为 电子邮件?

如何将此字符串转换为MimeKit.MimeMessage,然后将其作为电子邮件发送?

I want to send a email

        public readonly IMjmlServices _mjmlServices;
            bodyBuilder.HtmlBody += 
quot;</tr></ mj - table ></ mj - column ></ mj - section ></ mj - body ></ mjml >";
            bodyBuilder.TextBody += 
quot;This is some plain text";
            var result = _mjmlServices.Render(bodyBuilder.HtmlBody).Result;
            SmtpClient client = new SmtpClient();
                try
                {
                    client.Connect("Smtp.gmail.com", 465, true);
                    client.Authenticate("", "");
                    client.Send(result.Html);
                    Console.WriteLine("----- EMAIL SENT!! -----");
                }

but at client.Send(result.Html);it is giving error saying that Error: cannot convert from "string" to "MimeKit.MimeMessage" like this

How to convert this string to MimeKit.MimeMessage and then send this as email?

How to convert this string to MimeKit.MimeMessage and then send this as email?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔书几行 2025-01-25 01:56:47

“如何将此字符串转换为 MimeKit.MimeMessage,然后将其作为电子邮件发送”

我认为您尝试使用 SmtpClient 发送电子邮件的方式不是
正确的。使用 IMjmlServices 您应该尝试以下方式:

MjmlEmailService:

 public readonly IMjmlServices _mjmlServices;
 public EmailController(IMjmlServices _mjml) => (_mjmlServices) = (_mjml);


 public async Task MjmlEmailService()
        {
            var prehtml = "<mjml><mj-body><mj-container><mj-section><mj-column><mj-text>Hello From MJML Service!</mj-text></mj-column></mj-section></mj-container></mj-body></mjml>";
            var result = await _mjmlServices.Render(prehtml);
            SmtpEmailProcessor(result.Html);
        }

注意:这里您必须从MjmlResponse接口传递result.Html,该接口将是电子邮件正文,而SMTP 请求已发送。这里最好将两个服务分开,因为 MjmlEmailServiceSmtpClient 有不同的关注点。


SmtpClient:

public void SmtpEmailProcessor(string emailBody)
        {
            try
            {
                SmtpClient objsmtp = new SmtpClient("smtp.gmail.com", 587);
                objsmtp.UseDefaultCredentials = false;
                objsmtp.Credentials = new NetworkCredential("YourEmailId", "YourEmailPassword");
                objsmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                objsmtp.EnableSsl = true;
                MailMessage msg = new MailMessage("From Email", "To Email", "Subject: MJML Test", emailBody);
                msg.IsBodyHtml = true;
                objsmtp.Timeout = 50000;
                objsmtp.Send(msg);
            }
            catch (Exception ex) { }
        }

注意: 您可以查看有效的 SmtpClient 请求在此参考

输出:

在此处输入图像描述

希望上述步骤能够相应地指导您。

"How to convert this string to MimeKit.MimeMessage and then send this as email"

I think the way you are trying to send email using SmtpClient is not
correct. using IMjmlServices you should try below way:

MjmlEmailService:

 public readonly IMjmlServices _mjmlServices;
 public EmailController(IMjmlServices _mjml) => (_mjmlServices) = (_mjml);


 public async Task MjmlEmailService()
        {
            var prehtml = "<mjml><mj-body><mj-container><mj-section><mj-column><mj-text>Hello From MJML Service!</mj-text></mj-column></mj-section></mj-container></mj-body></mjml>";
            var result = await _mjmlServices.Render(prehtml);
            SmtpEmailProcessor(result.Html);
        }

Note: Here you have to pass the result.Html from MjmlResponse interface which will be email body while SMTP request sent. Here it better to make two service separated because MjmlEmailService and SmtpClient has different concern.

SmtpClient:

public void SmtpEmailProcessor(string emailBody)
        {
            try
            {
                SmtpClient objsmtp = new SmtpClient("smtp.gmail.com", 587);
                objsmtp.UseDefaultCredentials = false;
                objsmtp.Credentials = new NetworkCredential("YourEmailId", "YourEmailPassword");
                objsmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                objsmtp.EnableSsl = true;
                MailMessage msg = new MailMessage("From Email", "To Email", "Subject: MJML Test", emailBody);
                msg.IsBodyHtml = true;
                objsmtp.Timeout = 50000;
                objsmtp.Send(msg);
            }
            catch (Exception ex) { }
        }

Note: You can have a look on valid SmtpClient request here in this reference

Output:

enter image description here

Hope above steps guided you accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文