Send an Email in C# with Inline attachments |
Visite: 76668 |
lunedì 28 agosto 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.
|
| Scritto da Troels Thomsen - venerdì 20 ottobre 2006 alle ore 17.25 |
|
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.
|
| Scritto da Pravin - mercoledì 15 novembre 2006 alle ore 7.48 |
|
Nice work!
|
| Scritto da Nemo - sabato 18 novembre 2006 alle ore 20.56 |
|
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.
|
| Scritto da Markus Fischer - giovedì 8 marzo 2007 alle ore 18.26 |
|
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.
|
| Scritto da Korgull - venerdì 23 marzo 2007 alle ore 17.23 |
|
Can anyone tell me why if you change the literal in email.Subject = "INLINE attachment TEST";
to anything else this why this stops working?
|
| Scritto da Kirk - venerdì 27 luglio 2007 alle ore 12.04 |
|
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.
|
| Scritto da Kirk - venerdì 27 luglio 2007 alle ore 12.23 |
|
please disregard my last post. It was working find just going into my junk email folder, wow hehe.
|
| Scritto da Kirk - venerdì 27 luglio 2007 alle ore 17.31 |
|
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
|
| Scritto da Kersta - giovedì 8 novembre 2007 alle ore 17.20 |
|
Thanks for the valuable input. It really helped me to add the logo in the mail.
|
| Scritto da Yogesh Murgude - mercoledì 30 gennaio 2008 alle ore 14.43 |
|
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
|
| Scritto da Chris - lunedì 4 febbraio 2008 alle ore 6.51 |
|
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.
|
| Scritto da Boom - giovedì 10 aprile 2008 alle ore 17.17 |
|
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.
|
| Scritto da Mike Irving - venerdì 2 maggio 2008 alle ore 18.23 |
|
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?
|
| Scritto da Kaz - martedì 20 maggio 2008 alle ore 12.03 |
|
Excellent work.. thanks a lot.. it saved lot of time for me..
|
| Scritto da periyasamy - lunedì 18 agosto 2008 alle ore 15.12 |
|
That is exacly what I was looking at :) Thanks a lot!
|
| Scritto da biosys - mercoledì 10 dicembre 2008 alle ore 12.46 |
Hi,
I tried this example.
I just changed the file path, and MediaType as "image/mhtml".
But it doesn't work for me.
Here is my code.is there any thing missing
Dim filename As String "E:\test\MyReport1.mhtml"
Dim email As New System.Net.Mail.MailMessage("from", "to")
email.Subject = "INLINE attachment TEST"
email.IsBodyHtml = True
email.Body = "This is an INLINE attachment: ![]() Thanks for downloading this example. "
' generate the contentID string using the datetime
Dim contentID As String = Path.GetFileName(filename).Replace(".", "") & "@zofm"
' create the INLINE attachment
Dim attachmentPath As String = filename
Dim inline As New Attachment(attachmentPath)
inline.ContentDisposition.Inline = True
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline
inline.ContentId = contentID
inline.ContentType.MediaType = "image/mhtml"
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)
'send email
|
| Scritto da Eva - lunedì 26 gennaio 2009 alle ore 23.47 |
|
nice example
|
| Scritto da jay - martedì 17 marzo 2009 alle ore 16.06 |
|
O Bad Luck! it's show me '500-SMTP'
|
| Scritto da Frankey - venerdì 3 aprile 2009 alle ore 7.28 |
|
Hi, what is the assembly for SmtpDirect I didn't find it in .NET2 ?
Thanks
|
| Scritto da Emmanuel - giovedì 9 aprile 2009 alle ore 2.18 |
|
cool. This works. Can anyone give me pointer for embeding css file also?
|
| Scritto da Hament Verma - martedì 2 giugno 2009 alle ore 5.57 |
|
So, this is work only with your smtp library?
Pity bad.
|
| Scritto da Hyperborean - lunedì 8 giugno 2009 alle ore 10.00 |
|
I mean pity bad for MS ;)
|
| Scritto da Hyperborean - lunedì 8 giugno 2009 alle ore 10.14 |
|
ZofM, do you know how to send meeting request in html format with embedded images?
I was try to do this, but unfortunately.
|
| Scritto da Hyperborean - lunedì 8 giugno 2009 alle ore 14.53 |
|
if I want to send mail by different mail ID(not mine) then I use the below code. but i send mail it will add the ":**SPAM**:" in subject
How will i remove this spam
MailAddress strto =new MailAddress(emailTo);
msg.To.Add(strto);
msg.From = new MailAddress(emailFrom,sendername);
msg.Sender=new MailAddress emailFrom,sendername);
msg.Headers.Add("Reply-To", replyTo);
//and for authentication
SmtpClient smtp = new SmtpClient(smtpServer);
//Check Authentication
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(userEmail,password);
smtp.UseDefaultCredentials = false;
smtp.Credentials = SMTPUserInfo;
smtp.Send(msg);
when i user my email id in
msg.From = new MailAddress(userEmail,username);
then its working Fine
|
| Scritto da poonam - martedì 11 agosto 2009 alle ore 9.54 |
|
You should have a look at:
http://www.andreas-kraus.net/blog/tips-for-avoiding-spam-filters-with-systemnetmail/
... and remember if it still fails - change your mail.Priority to High
objMail.Priority = System.Web.Mail.MailPriority.High
this has never failed me :-)
|
| Scritto da chire - venerdì 28 agosto 2009 alle ore 21.17 |
|
nice example
really help me :)
thanks a lot !
|
| Scritto da controlsys - martedì 6 ottobre 2009 alle ore 10.26 |
|
Can anyone help me how do I download an Inline attachment, which is HTML file??
|
| Scritto da Tsovinar Hovhannisyan - martedì 26 gennaio 2010 alle ore 10.04 |
|
Hi,
I needed a help with the code for, verifying if an email id exists or not, but without sending mail. Can you help me out with this?
Thanks.
|
| Scritto da kummi - sabato 6 febbraio 2010 alle ore 20.45 |
|
I'm not sure this works well with email hosts requiring credentials and SSL (e.g., gmail).
|
| Scritto da Brent - mercoledì 5 maggio 2010 alle ore 23.26 |
Scrivi nuovo commento