Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format text in email when using smtp

Tags:

c#

smtp

I'm using the following method to send an email. I want to be able to format the email with bold text.

Ex.

From: name

Email: email address

Message: message

How would I do this?

    protected void SendMail()
    {
        var fromAddress = "[email protected]";
        var toAddress = "[email protected]";
        const string fromPassword = "mypassword";

        string subject = "New Email from Website";
        string body = "From: " + name.Text + "\n";
        body += "Email: " + email.Text + "\n";
        body += "Message: \n" + message.Text + "\n";

        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }
like image 991
JAck28 Avatar asked Jun 21 '13 23:06

JAck28


People also ask

How do I send plain text in Outlook?

On the File tab, choose Options > Mail. Under Compose messages, in the Compose messages in this format list, click HTML, Plain Text, or Rich Text.

Why email received in plain text instead of HTML?

The problem is almost certainly related to the recipient. Some recipients (especially corporate users) for reasons of security prefer to receive messages in plain text format. If they set their systems to display plain text they will reply in plain text.


1 Answers

Set isBodyHtml to true, the following code describes it, To send a bold text you can use "<b> My bold text </b> ". To send an italicized text you can use "<i> Italic text </i> ". To send an underlined text you can use "<u> underlined text </u>".

You can copy and use the following method. By using this method, it will be very easy to send emails.

public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
    try
    {
        MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
        MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 25,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new System.Net.NetworkCredential("your email address", "your password")
        };

        using (MailMessage message = new MailMessage(FromAddr, ToAddr)
        {
            Subject = Subject,
            Body = Body,
            IsBodyHtml = IsBodyHTML,
            BodyEncoding = System.Text.Encoding.UTF8,

        })
        {
            smtp.Send(message);
        }
        return true;
    }
    catch
    {
        return false;
    }
}

When you call this method call it like this

 SendEmail("Here address to" , "Here to name" , "Here from", "here from name", "here subject" , here Body, " Here whether HTML or Plain" )
like image 196
Kas Avatar answered Oct 17 '22 05:10

Kas