Adding logo image to email message

Donald Symmons 3,066 Reputation points
2022-06-10T22:02:59.5+00:00

How do I add my logo at the top of my email message?
I need a simple way to do this. Thank you

Here is my code to send email to user; I want to place the logo before string body ="Hello" + txtname.Text.Trim() +",";

 using (MailMessage mm = new MailMessage("******@mywebsite.com", mailtxtbx.Text))  
        {  
            mm.Subject = "Activate Account";  
            string body = "Hello " + txtname.Text.Trim() + ",";  
            body += "<br /><br />Successful Registration";  
            body += "<br /><br />Please click the button to activate your account";  
            body += "<br /><br /><a style='display: block; width: 200px; height: 30px; background: #32CD32;padding: 15px;font-family: Nunito; text-align:center; border-radius: 5px;color: white;font-weight: 700;text-decoration: none;' href = '"  
            + Request.Url.AbsoluteUri.Replace("signup.aspx", "activation.aspx?ActCode=" + actCode) + "'>Activate your new account</a>";  
            body += "<br /><br />Thanks";  
            mm.Body = body;  
            mm.IsBodyHtml = true;  
  
            SmtpClient SMTP = new SmtpClient("relay-hosting.secureserver.net", 25);  
            SMTP.UseDefaultCredentials = false;  
            SMTP.Credentials = new NetworkCredential()  
            {  
                UserName = "******@mywebsite.com",  
                Password = "xxxxxxxxxx"  
            };  
            SMTP.EnableSsl = false;  
            SMTP.Send(mm);  
        }  
Developer technologies | ASP.NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Yijing Sun-MSFT 7,106 Reputation points
    2022-06-13T05:21:25.007+00:00

    Hi @Donald Symmons ,
    I suggest you could use LinkedResource. You could do like this:

    private AlternateView Mail_Body()    
        {    
            string path = Server.MapPath(@"Images/photo.jpg");    
            LinkedResource Img = new LinkedResource(path, MediaTypeNames.Image.Jpeg);    
            Img.ContentId = "MyImage";    
            string str = @"    
                <table>    
                    <tr>    
                        <td>    
                          <img src=cid:MyImage  id='img' alt='' width='100px' height='100px'/>     
                        </td>    
                    </tr>  
                    <tr>  
                         <td>  
                              'hello'   
                         </td>  
                     </tr>  
                </table>    
                ";    
            AlternateView AV =     
            AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);    
            AV.LinkedResources.Add(Img);    
            return AV;    
        }    
      
      
    using (MailMessage mm = new MailMessage("******@mywebsite.com", mailtxtbx.Text))  
             {  
                 mm.Subject = "Activate Account";  
                mm.AlternateViews.Add(Mail_Body());  
                 mm.IsBodyHtml = true;  
          
                 SmtpClient SMTP = new SmtpClient("relay-hosting.secureserver.net", 25);  
                 SMTP.UseDefaultCredentials = false;  
                 SMTP.Credentials = new NetworkCredential()  
                 {  
                     UserName = "******@mywebsite.com",  
                     Password = "xxxxxxxxxx"  
                 };  
                 SMTP.EnableSsl = false;  
                 SMTP.Send(mm);  
             }  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Daniel Kåven 696 Reputation points
    2022-06-10T23:39:12.717+00:00

    You can solve this by hosting an image anywhere and link it with an <img> tag.
    I recommend to create the whole signature as a HTML and when you are satisfied include it within your code.

    ​----------
    If this was helpful, please Upvote and Accept Answer.
    Else, please Comment with further information or related questions.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2022-06-10T23:52:11.103+00:00

    See the following Code sample

    Focus on imageIdentifier and LinkedResource are the key.

    var imageIdentifier = "Miata";  
      
    var htmlMessage = AlternateView.CreateAlternateViewFromString(  
     $"<p>This is what I'm purchasing in <b>2019</b> to go along with my 2016 model.</p><img src=cid:{imageIdentifier}><p>Karen</p>",  
     null, "text/html");  
      
    var fileName = $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images1")}\\2017Miata.jpg";  
    var miataImage = new LinkedResource(fileName, "image/jpeg") {ContentId = imageIdentifier };  
    mail.AlternateViews.Add(plainMessage);  
    mail.AlternateViews.Add(htmlMessage);  
    htmlMessage.LinkedResources.Add(miataImage);  
    
    
     
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.