How to send the embedded image email (Inline Attachment)

Source: Internet
Author: User
Tags mailmessage smtpclient
[Code: c #]

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Net. Mail;

Namespace SystemNetMailTest
{
Class Program
{
Static void Main (string [] args)
{
String myMailEncoding = "UTF-8 ";

String myFromEmail = "testtest.com ";
String myFromName = "courier sender ";

String myToEmail = "testtest.com ";
String myToName = "courier receiver ";

MailAddress from =
New MailAddress (myFromEmail, myFromName,
Encoding. GetEncoding (myMailEncoding ));
MailAddress to =
New MailAddress (myToEmail, myToName,
Encoding. GetEncoding (myMailEncoding ));

MailMessage myMessage = new MailMessage (from, );

MyMessage. Subject = "Subject ";
MyMessage. SubjectEncoding = Encoding. GetEncoding (myMailEncoding );

MyMessage. Body = "\ "/> ";
MyMessage. BodyEncoding = Encoding. GetEncoding (myMailEncoding );

MyMessage. IsBodyHtml = true;

MyMessage. Priority = MailPriority. High;

// Set the Attachment statement case (Attachment)

String strFilePath = @ "C: \ Logo.gif ";

System. Net. Mail. Attachment attachment1 =
New System. Net. Mail. Attachment (strFilePath );
Attachment1.Name=System. IO. Path. GetFileName (strFilePath);
Attachment1.NameEncoding = Encoding. GetEncoding (myMailEncoding );
Attachment1.TransferEncoding = System. Net. Mime. TransferEncoding. Base64;

// Set the Attachment as an Inline Attachment)
Attachment1.ContentDisposition. Inline = true;
Attachment1.ContentDisposition. DispositionType =
System. Net. Mime. DispositionTypeNames. Inline;

MyMessage. Attachments. Add (attachment1 );

SmtpClient smtp = new SmtpClient ("localhost ");

Try
{
Smtp. Send (myMessage );
Console. WriteLine (DateTime. Now. ToString () + "sent successfully ");
}
Catch
{
Console. WriteLine (DateTime. Now. ToString () + "message loss !!! ");
}

Console. ReadKey ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.