在AlternateView
上创建MailMessage
后,如何修改内容?
在我的邮件生成过程中,我首先创建了MailMessage
和HTML AlternateView
,然后在MailMessage
上执行了一系列转换。
这些转换需要能够更新AlternateView
中的HTML,并影响附加到LinkedResources
的{{1}}。
AlternateView
我可以使用;
将AlternateView内容检索为文本 MailMessage _email;
public void PrepareEmail(string subject, string htmlBody)
{
// Create the Email
_email = new MailMessage();
// Set the Subject
_email.Subject = subject;
_email.SubjectEncoding = Encoding.UTF8;
// Set the HTML Body
_email.BodyEncoding = Encoding.UTF8; // needed?
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
htmlBody,
null,
MediaTypeNames.Text.Html
);
_email.AlternateViews.Add(htmlView);
}
public void TransformEmail()
{
// Perform a runtime-defined series of transformations,
// which can modify any part of the Email, AlternateView, or LinkedResources.
// Primarily, I am modifying HTML, and adding LinkedResources.
}
但似乎没有任何方法可以更新 public string GetAlternateViewText(AlternateView av)
{
using (MemoryStream s = new MemoryStream())
{
// Convert the Stream to a MemoryStream,
// so that we can use ToArray()
CopyStream(
av.ContentStream,
s
);
return Encoding.UTF8.GetString(
s.ToArray()
);
}
}
public void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[16 * 1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
的内容。