mercredi, mai 09, 2007

Send mail with embedded image c# 2.0

Fist of all, we use the "System.Net.Mail" namespace for this task. Code to send a mail using c# 2.0 :

MailMessage message = new MailMessage();
message.From = new MailAddress("Sender@test.com");
message.To.Add(new MailAddress(to@test.com));
message.CC.Add(new MailAddress(cc@test.com));
message.Subject = "Message subject";
message.Body = "<html><body></body></html>";
message.IsBodyHtml = true;
message.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.Host = "MySMTPserver";
client.Send(message);
To add embedded image :

MailMessage message = new MailMessage();
message.From = new MailAddress("Sender@test.com");
message.To.Add(new MailAddress(to@test.com));
message.CC.Add(new MailAddress(cc@test.com));
message.Subject = "Message subject";

Attachment TopImageScreen = new Attachment(@"c:\TopImageFile.jpg");
TopImageScreen.ContentId = "TopImage";
message.Attachments.Add(TopImageScreen);
message.Body = "<html><body><img src=\"cid:TopImage\" style=\"border:0; \"></body></html>";

message.IsBodyHtml = true;
message.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.Host = "MySMTPserver";
client.Send(message);

Aucun commentaire: