Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    May 30, 2011 @ 02:30
    Bjarne Fyrstenborg
    0

    Embed image in email

    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' />

    Am I missing something in the code?

    - Bjarne

  • Sean 141 posts 179 karma points
    May 31, 2011 @ 11:56
    Sean
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    May 31, 2011 @ 12:03
    Jeroen Breuer
    0

    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;
    }

    This is done with the HTML Agility Pack.

    Jeroen

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    May 31, 2011 @ 15:40
    Bjarne Fyrstenborg
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft