I have the following code to send an email, when a user signup for the newsletter:
lblStatus.Text = "Tak for din tilmelding.";
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
string firstName = FirstnameTextBox.Text;
string lastName = LastnameTextBox.Text;
string email = EmailTextBox.Text;
string sport = SportDropDownList.SelectedValue;
string club = ClubTextBox.Text;
string title = TitleDropDownList.SelectedValue;
string fromName = "Carsten Oldengaard";
string fromEmail = "[email protected]";
string subject = "Tilmelding til nyhedsmail";
string smtp = "mailout1.surf-town.net";
string htmlEmail = string.Empty;
string plainEmail = string.Empty;
string path = Server.MapPath("images/logo.jpg");
MailAddress fromAddress = new MailAddress(fromEmail, fromName);
MailAddress toAddress = new MailAddress(email);
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = subject;
message.IsBodyHtml = true;
//LinkedResource logo = new LinkedResource(path, MediaTypeNames.Image.Jpeg);
//logo.ContentId = "companylogo";
htmlEmail = @"<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body>
<p><b>Tak for din tilmelding.</b></p><p>Du har tilmeldt dig med følgende oplysninger:</p><table border='0' cellspacing='0' cellpadding='2'><tr><td width='100'>Fornavn: </td><td>" + firstName +
@"</td></tr><tr><td width='100'>Efternavn: </td><td>" + lastName + @"</td></tr><tr><td width='100'>E-mail: </td><td>" + email + @"</td></tr><tr><td width='100'>Sportsgren: </td><td>" + sport + @"</td></tr><tr><td width='100'>Forening: </td><td>" + club + @"</td></tr><tr><td width='100'>Titel: </td><td>" + title + @"</td></tr></table><br /><br />" +
@"<p>Du kan til enhver tid <a href='http://oldengaard.dk/da/nyheder/afmelding.aspx' target='_blank'>framelde dig her.</a></p>Carsten Oldengaard<br />Tlf.: 4018 1874<br /><br />
<a href='http://oldengaard.dk' target='_blank'><img src='http://oldengaard.dk/usercontrols/NyhedsTilmelding/images/logo.jpg' /></a></body></html>";
plainEmail = @"" + @"Tak for din tilmelding." + @"\n\n" + @"Du har tilmeldt dig med følgende oplysninger:" + @"\n\n" + @"Fornavn: " + firstName + @"\n" + @"Efternavn: " + lastName +
@"\n" + @"E-mail: " + email + @"\n" + @"Sportsgren: " + sport + @"\n" + @"Forening: " + club + @"\n" + @"Titel: " + title + @"\n\n" +
@"Du kan til enhver tid framelde dig her: http://oldengaard.dk/da/nyheder/afmelding.aspx" + @"\n\n" + @"Carsten Oldengaard" + @"\n" + @"Tlf.: 4018 1874";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlEmail, null, "text/html");
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainEmail, null, "text/plain");
//htmlView.LinkedResources.Add(logo);
message.AlternateViews.Add(htmlView);
message.AlternateViews.Add(plainView);
smtpClient.Host = smtp;
smtpClient.Send(message);
This works, but the link is the absolute path to the image.. and I've seen some examples, where the image is embed instead and use these lines:
string path = Server.MapPath("images/logo.jpg"); LinkedResource logo = new LinkedResource(path, MediaTypeNames.Image.Jpeg); logo.ContentId = "companylogo"; htmlView.LinkedResources.Add(logo);
and instead of the full path: <img src='http://oldengaard.dk/usercontrols/NyhedsTilmelding/images/logo.jpg' /> something like <img src='cid:companylogo' />
Here is a method I'm using if I want to add an absolute path to my emails:
/// <summary>
/// Add an absolute path to all the img tags in the html of an e-mail.
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public string AddImgAbsolutePath(string html, string mainDomain)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
if (doc.DocumentNode.SelectNodes("//img[@src]") != null)
{
foreach (HtmlNode img in doc.DocumentNode.SelectNodes("//img[@src]"))
{
HtmlAttribute att = img.Attributes["src"];
if (att.Value.StartsWith("/"))
{
att.Value = mainDomain + att.Value;
}
}
}
return doc.DocumentNode.InnerHtml;
}
Well, it seems to be the absolute path too and would have the same effect as the <img src="http://oldengaard.dk/usercontrols/NyhedsTilmelding/images/logo.jpg" />
and the LinkedResource is used to add the image to the AlternateView .. and then instead of the absolute path to the image it should be something like: <img src="cid:logo" />
Embed image in email
I have the following code to send an email, when a user signup for the newsletter:
This works, but the link is the absolute path to the image.. and I've seen some examples, where the image is embed instead and use these lines:
and instead of the full path: <img src='http://oldengaard.dk/usercontrols/NyhedsTilmelding/images/logo.jpg' /> something like <img src='cid:companylogo' />
Am I missing something in the code?
- Bjarne
Hi There,
In this situation i'd use the base href tag.It works for me.
Hope this helps.
Sean
http://www.w3schools.com/TAGS/tag_base.asp
Here is a method I'm using if I want to add an absolute path to my emails:
This is done with the HTML Agility Pack.
Jeroen
Well, it seems to be the absolute path too and would have the same effect as the <img src="http://oldengaard.dk/usercontrols/NyhedsTilmelding/images/logo.jpg" />
I was wondering how to embed the image when you send the email, something like this: http://www.codeproject.com/KB/aspnet/inkrajesh.aspx
and the LinkedResource is used to add the image to the AlternateView .. and then instead of the absolute path to the image it should be something like: <img src="cid:logo" />
Bjarne
is working on a reply...