0

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.

1 Answer 1

1

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 protected]", "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.

Sign up to request clarification or add additional context in comments.

2 Comments

This didn't work. What is happening is that the code from CKEditorControl is with html tags for example <h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HEADER</h1> And I want to be rendered as a normal header, instead it pasted as the text you see
Yes, the problem was that I was sending the message body and not the whole message. Thank you for the advice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.