 |
|
 |
Send an Email in C# with Inline attachments |
Visits: 23963 |
Monday, August 28, 2006 |
This is a SMTP client implementation in C# permits to send Inline attachment in
your email messages (using the
MailMessage class of
System.Net.Mail namespace is
possible only to send normal attachments).
To show an image inside the body of the email without link to external site is necessary
to add the attachment in the header of message and call it from the HTML section
of the body. Using the MailMessage
and the SmtpClient
classes is not enough set
the Inline property to True to show the image:
// 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();
}
}
}
Click here to download the example of this article
Well, this is for sure a solution, but I wouldn't recommend it. Microsoft has actually added a lot of features in the 2.0 framework while changing System.Web.Mail to System.Net.Mail, and there are types and methods that allows you to send e-mails with embedded/inline attachments.
Please refer to http://www.systemnetmail.com/faq/4.4.aspx for more details.
|
| Written by Troels Thomsen - Friday, October 20, 2006 at 5:25 PM |
|
I want to send a outlook calendar request even when i dont have outlook on my machin. I tried to send a vacalender file , its going as attachment, not as a calender request. Tell me how can do the same , so that i can able to send the meeting request as same as we send outlook.
|
| Written by Pravin - Wednesday, November 15, 2006 at 7:48 AM |
|
Nice work!
|
| Written by Nemo - Saturday, November 18, 2006 at 8:56 PM |
|
I do not agree with Troels Thomsen, this sample uses .NET 2.0 classes in a nice way.
works good for me, I like it much better than Troels Thomsen“s sample.
|
| Written by Markus Fischer - Thursday, March 08, 2007 at 6:26 PM |
|
Thank you very much (o "Grazie molte", visto che sei italiano come me ;) ) for this great example! It will be very useful to me, and, in the future, i'll try to implement simple smtp authentication.
|
| Written by Korgull - Friday, March 23, 2007 at 5:23 PM |
|
Can anyone tell me why if you change the literal in email.Subject = "INLINE attachment TEST";
to anything else this why this stops working?
|
| Written by Kirk - Friday, July 27, 2007 at 12:04 PM |
|
Can anyone tell me why if you change the literal in email.Subject = "INLINE attachment TEST";
to anything else why this stops working?
Oh and the example presented at http://www.systemnetmail.com/faq/4.4.aspx#4.4 doesn't work at all.
Sorry about the quasi dbl post I just used a lot of things from the other site noted with little luck.
|
| Written by Kirk - Friday, July 27, 2007 at 12:23 PM |
|
please disregard my last post. It was working find just going into my junk email folder, wow hehe.
|
| Written by Kirk - Friday, July 27, 2007 at 5:31 PM |
|
Hi
Thanks a lot for this example! It works fine for me. Now I would like to send a html document instead (a word documet that I have saved as html). Can anybode give me some hints. please?
Best regrads
|
| Written by Kersta - Thursday, November 08, 2007 at 5:20 PM |
|
Thanks for the valuable input. It really helped me to add the logo in the mail.
|
| Written by Yogesh Murgude - Wednesday, January 30, 2008 at 2:43 PM |
|
This Example saved me. System.Net.Mail doesnt wrap the email correctly when using LinkedResources and AlternateViews. It creates a multipart/mixed email but it is malformed. This example fixes that problem. Thank You
|
| Written by Chris - Monday, February 04, 2008 at 6:51 AM |
|
I wonder why the size of the mail of this example bigger that the size of a mail in Word mail merge.
I used a sample image for both ways. One (A) manually send using Word mail merge, one (B) used this code. But the size of the mails were significantly different. A=1/4B
So I want to know what shoud we do to reduce the size in coding.
|
| Written by Boom - Thursday, April 10, 2008 at 5:17 PM |
|
I have been migrating from System.Web.Mail to System.Net.Mail this afternoon, and the code in your example has been of great help.
Many thanks.
Mike.
|
| Written by Mike Irving - Friday, May 02, 2008 at 6:23 PM |
|
It doesn't work me. It doesn't have the ability to connect to an SMTP server that requires authentication and SSL or I missing something?
|
| Written by Kaz - Tuesday, May 20, 2008 at 12:03 PM |
|
Excellent work.. thanks a lot.. it saved lot of time for me..
|
| Written by periyasamy - Monday, August 18, 2008 at 3:12 PM |
Post new comment
|
 |
|
 |
|
Versione italiana
CATEGORIES

Ajax (2)
ASP.NET (11)
C# Code (4)
IIS (1)
Silverlight (1)
Sql Server 2000 (1)
Varie (4)
Visual Studio 2005 (3)
ARCHIVE

May 2008 (1)
April 2008 (1)
January 2008 (4)
December 2007 (1)
May 2007 (1)
February 2007 (1)
December 2006 (3)
October 2006 (1)
September 2006 (3)
August 2006 (1)
June 2006 (1)
May 2006 (1)
ABOUT ME [curriculum]

CREDITS

|