我想使用MailKit设置“邮件程序/通讯”。我的网站堆栈基于Blazor Web程序集,并使用.Razor组件。
我想知道是否有一种方法可以消耗我编写的剃刀组件,以将HTML输出到我用来生成电子邮件正文的MimeMessage对象中,以及该体系结构是什么样/实现的最佳方式这个吗?
相似的问题(尽管不是Blazor):
答案 0 :(得分:0)
我正在将Blazor与MailKit结合使用:Google Email Viewer in Server Side Blazor
我使用MarkupString
这样显示电子邮件内容:
@using MimeKit
@using MessageReader
@strError
<div style="padding:2px; vertical-align:top">
<div><i>@MimeKitMessage.Date.ToString()</i></div>
<div><b>From:</b> @MimeKitMessage.From.ToString()</div>
<div><b>To:</b> @MimeKitMessage.To.ToString()</div>
<div><b>Subject:</b> @MimeKitMessage.Subject</div>
<br />
<div>@((MarkupString)@htmlEmail)</div>
</div>
@code {
[Parameter] public Message paramMessage { get; set; }
MimeMessage MimeKitMessage;
string strError = "";
string htmlEmail = "";
protected override void OnInitialized()
{
try
{
if (paramMessage != null)
{
string converted = paramMessage.Raw.Replace('-', '+');
converted = converted.Replace('_', '/');
byte[] decodedByte = Convert.FromBase64String(converted);
using (Stream stream = new MemoryStream(decodedByte))
{
// Convert to MimeKit from GMail
// Load a MimeMessage from a stream
MimeKitMessage = MimeMessage.Load(stream);
// Convert any embedded images
var visitor = new HtmlPreviewVisitor();
MimeKitMessage.Accept(visitor);
htmlEmail = visitor.HtmlBody;
//If the email has attachments we can get them here
//var attachments = visitor.Attachments;
}
}
}
catch (Exception ex)
{
strError = ex.Message;
}
}
}