简体   繁体   中英

Sending mail with c# html formatted

I know there are a lot of question similar to this one but I cannot seem to find the right answer. So in my page I have CKEditorControl I want to use its content and send it as text. The problem is that the email is send with all the tags and they are not rendered put pasted as plain text.

  public class MailSender
{
    private readonly MailMessage mailMessage;
    private SmtpClient smtpClient;
    private string fromEmail = myMail;
    private string fromPass = myPass;

    public MailSender()
    {
        this.mailMessage = new MailMessage();

        this.mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
        this.mailMessage.BodyEncoding = Encoding.UTF8;

        this.mailMessage.From = new MailAddress(MyMail);


    }

    public void Send(string subject, string body, params string[] to)
    {
        this.mailMessage.Body = body;           
        this.mailMessage.Subject = subject;
        this.mailMessage.IsBodyHtml = true;
        foreach (var mail in to)
        {
            this.mailMessage.To.Add(mail);
        }

        using (this.smtpClient = new SmtpClient("smtp.gmail.com", 25))
        {
            this.smtpClient.EnableSsl = true;
            this.smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            this.smtpClient.UseDefaultCredentials = false;
            this.smtpClient.Credentials = new NetworkCredential(this.fromEmail, this.fromPass);
            this.smtpClient.Send(this.mailMessage.From.ToString(), this.mailMessage.To.ToString(), this.mailMessage.Subject, this.mailMessage.Body);
        }

    }
}

This is the class which is responsible for sending my emails.

Here is code that I've wrote an worked perfectly for me

Default.aspx

<CKEditor:CKEditorControl ID="CKEditor1" BasePath="~/_Samples/ckeditor/" runat="server"></CKEditor:CKEditorControl>
    <asp:Button ID="SaveButton" runat="server" OnClick="SaveButton_Click" Text="Save Me" />

Default.aspx.cs

protected void SaveButton_Click(object sender, EventArgs e)
        {            
            Mail.SendMail("email@gmail.com", "cke", CKEditor1.Text);
        }

Email.cs

public static void SendMail(string To, string Subject, string Body)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(SmtpUserName, SmtpFrom);
            message.To.Add(new MailAddress(To));
            message.Subject = Subject;
            message.Body = Body;
            message.IsBodyHtml = true;
            message.BodyEncoding = Encoding.UTF8;
            message.SubjectEncoding = Encoding.UTF8;

            var client = new SmtpClient(SmtpAddress, SmtpPort)
            {
                Credentials = new NetworkCredential(SmtpUserName, SmtpPassword),
            };

            client.Send(message);            
        }

I used lost of rich text editor features, bold, italic, colors ... and received email with bold, italic and colorful text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM