MailMessage c# - How to make it HTML and add images etc?
Asked Answered
L

5

23
string to = "[email protected]";
 string body = "Test";
 SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
 MailMessage mailObj = new MailMessage(urEmail, to, subject, body);
 SMTPServer.Send(mailObj);

This is how i am currently sending a test email. How do i make this html and be able to make the email sent out look better by adding images etc?

Thanks

Lail answered 24/10, 2011 at 8:49 Comment(1)
Better not call a SmtpClient object as SMTPServer.Attenuation
L
48

On the MailMessage set the property IsBodyHtml to true.

string to = "[email protected]";
string body = "Test";
SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
MailMessage mailObj = new MailMessage(urEmail, to, subject, body);

mailObj.IsBodyHtml = true; // This line

SMTPServer.Send(mailObj);
Lindholm answered 24/10, 2011 at 8:53 Comment(0)
M
8

You have to set mailObj .IsBodyHtml = true;

Meisel answered 24/10, 2011 at 8:52 Comment(0)
M
2

you can use the following idea to take an ASPX page and render it to a string:

StringWriter writer = new StringWriter();
Server.Execute("Login.aspx", writer);
string html = writer.ToString();

If you then set the MailMessage.IsBodyHtml to true you can send an HTML message. If you want to use images and other stuff make sure that the receiver of the email can access those images.

Mensa answered 24/10, 2011 at 8:54 Comment(0)
P
1

There are two ways of doing this:

  1. Embed the images inside your mail. (see this question)

  2. Link to the images through your src attribute of the image tag inside your HTML mail. This needs you to host the image files somewhere on a webserver which the recipients can access.

In both cases you will need to send the mail with a html body.

mailObj.IsBodyHtml = true;
Patten answered 24/10, 2011 at 9:8 Comment(0)
S
-1

For your question about adding Image to your email, if your asking for embedding then you can use Anchor tags of HTML or else attach the image file to the mail by using mailObj.Attachments.Add() method i guess.

But the best way is to send the images as attachments because some firewalls just blocks the embedded images but allows attachments. So that way your better safer in delivering the content, though its not a perfect way.

Southsoutheast answered 24/10, 2011 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.