Igor Klimenko
Igor Klimenko

Reputation: 29

Sending rich text to email

I faced a problem sending the text as html markup in a message to mail. At first, I convert rtf to html

string GetHtmlContent(string someRTFtext)
{
    RichEditDocumentServer server = new RichEditDocumentServer();
    server.RtfText = someRTFtext;
    server.Options.Export.Html.CssPropertiesExportType = DevExpress.XtraRichEdit.Export.Html.CssPropertiesExportType.Inline;
    server.Options.Export.Html.DefaultCharacterPropertiesExportToCss = true;
    server.Options.Export.Html.EmbedImages = true;
    server.Options.Export.Html.ExportRootTag = DevExpress.XtraRichEdit.Export.Html.ExportRootTag.Body;

    return server.HtmlText;
}

This method return me

<body>
<style type="text/css">
    .cs2E86D3A6{text-align:center;text-indent:0pt;margin:0pt 0pt 0pt 0pt}
    .cs88F66593{color:#800080;background-color:transparent;font-family:Arial;font-size:8pt;font-weight:bold;font-style:normal;}
</style>
<p class="cs2E86D3A6"><span class="cs88F66593">Форматированый текст</span></p></body>

but the post came simple text, without any selections, fonts, etc. Here is a method of sending

try
{
    string login = ConfigurationManager.AppSettings["EmailLogin"];
    string password = ConfigurationManager.AppSettings["EmailPassword"];

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(login);

    if (lbStudents.Items.Count == 0)
        MessageBox.Show("Error.", "Sending massage", MessageBoxButtons.OK, MessageBoxIcon.Information);

    foreach (SmallStudent student in lbStudents.Items)
    {
        mail.To.Add(new MailAddress(student.Email));
    }

    if (txtSubject.Text.Trim() == String.Empty)
    {
        MessageBox.Show("Error.", "Sending massage",
        MessageBoxButtons.OK, MessageBoxIcon.Information);
        return;
    }

    mail.Subject = txtSubject.Text.Trim();
    mail.Body = GetHtmlContent(rtfEditor.DocumentRtf);
    mail.IsBodyHtml = true;
    mail.BodyEncoding = Encoding.UTF8;

    SmtpClient client = new SmtpClient();
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.Credentials = new NetworkCredential(login.Split('@')[0], password);
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Send(mail);
    mail.Dispose();
}
catch (Exception exception)
{
    MessageBox.Show("Exception: " + exception.Message, 
                    "Sending massage", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);
}

Upvotes: 3

Views: 3450

Answers (1)

Douglas
Douglas

Reputation: 54887

Several email clients, including Gmail webmail, do not support <style> elements; see CSS Support Guide for Email Clients.

You'll get better compatibility by inlining your styles through style="..." attributes:

<body>
<p style="text-align:center;text-indent:0pt;margin:0pt 0pt 0pt 0pt">
    <span style="color:#800080;background-color:transparent;font-family:Arial;font-size:8pt;font-weight:bold;font-style:normal;">Форматированый текст</span>
</p>
</body>

This may introduce a lot of repetition, so consider using an automated process for inlining the styles if you have many styled elements.

Upvotes: 2

Related Questions