Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

// creating the attachment System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"c:\\test.png"); inline.ContentDisposition.

Inline = true; // sending the message MailMessage email = new MailMessage(); // set the information of the message (subject, body ecc...) // send the message System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost"); smtp.Send(email); email.Dispose();

In this way, the sent message misses the right Content-Type section in the header (to have an Inline attachment is necessary the "multipart/related" Content-Type). To resolve this situation is possible to by-pass the SmtpClient and use one custom class for SMTP client. This class provides to connect/communicate with the SMTP server and to add the right Content-Type to the message. using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net.Mail; using System.Net.Mime; namespace devSmtp { class Program { static void Main(string[] args) { // This example show you how to send one email message with an INLINE attachment. // You can use this example also without the support of CDO or other type of SmtpClient. // creating the email message MailMessage email = new MailMessage("test@yourdomain.something", "test@yourdomain.something"); // information email.Subject = "INLINE attachment TEST"; email.IsBodyHtml = true; email.Body = "<div style=\"font-family:Arial\">This is an INLINE attachment:<br /><br /><img src=\"@@IMAGE@@\"

alt=\"\"><br /><br />Thanks for downloading this example.</div>"; // generate the contentID string using the datetime string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm"; // create the INLINE attachment string attachmentPath = Environment.CurrentDirectory + @"\test.png"; Attachment inline = new Attachment(attachmentPath); inline.ContentDisposition.Inline = true; inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline; inline.ContentId = contentID; inline.ContentType.MediaType = "image/png"; inline.ContentType.Name = Path.GetFileName(attachmentPath); email.Attachments.Add(inline); // replace the tag with the correct content ID email.Body = email.Body.Replace("@@IMAGE@@", "cid:" + contentID); // sending the email with the SmtpDirect class (not using the System.Net.Mail.SmtpClient class) SmtpDirect smtp = new SmtpDirect("localhost"); smtp.Send(email); email.Dispose(); } } }

You might also like