I want to send a password email to a user, however the customer wants an image embedded (inline) in the email.
I created an email, saved the data to a txt file, during my code I read in the template but when I send it the line endings are broken and therefore the MIME data is broken. I just get =3D
What am I doing wrong?
string FILENAME = Server.MapPath("~/GuestUserTemplate.txt");
StreamReader objStreamReader = File.OpenText(FILENAME);
string sEmailTemplate = "";
string input = null;
while ((input = objStreamReader.ReadLine()) != null)
{
sEmailTemplate = sEmailTemplate + input;
}
objStreamReader.Close();
/* send an email */
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress(sToEmail));
msg.From = new MailAddress(sFromEmail);
msg.Subject = sEmailSubject;
msg.Body = sEmailTemplate;
//try
{
client.Send(msg);
}
//catch (Exception excm)
{
}
Just done a bit more detective work. The email I am sending out has this in the header:
MIME-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Where as an email which has inline images has:
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----_=_NextPart_001_01C9C98D.6552117E";
type="multipart/alternative"
It seems that I need to set the Content type to multipart but I am not sure how...
I'm not quite sure what kind of text you are loading (and appending to what?) but I would recommend you create a real template, e.g. your email text with placeholders that will be replaced with user's name, etc.
Use <img src="cid:logo.png" /> - for the inline image in the HTML body of the message (in your template).
You will then need to add the corresponding image to the LinkedResources collection of the MailMessage and set its ContentID header to "logo.png" or whatever you call it. After that go and send your mail (multipart content type will be set automatically for you based on the structure of the mail message).
P.S.: use SendAsync() or write the mail to the local pickup queue of your own smtp server, otherwise you tie up your ASP.NET worker thread. Connecting to remote smtp servers/web services etc. takes a considerable amount of time (compared to the request execution time) and the worker thread is sitting there waiting and unable to service other incoming requests.