0% found this document useful (0 votes)
80 views

Creating The Attachment

This document shows how to send an email with an inline attachment by bypassing the SmtpClient class and instead using a custom SmtpDirect class. It creates an email message with an inline attachment, sets properties like content ID and disposition, and then uses the SmtpDirect class to send the message, allowing the correct Content-Type header to be added.

Uploaded by

Laks Sadeesh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Creating The Attachment

This document shows how to send an email with an inline attachment by bypassing the SmtpClient class and instead using a custom SmtpDirect class. It creates an email message with an inline attachment, sets properties like content ID and disposition, and then uses the SmtpDirect class to send the message, allowing the correct Content-Type header to be added.

Uploaded by

Laks Sadeesh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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("[email protected]", "[email protected]"); // 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