locked
Sending Image in Emailbody RRS feed

  • Question

  • User210249645 posted

    how to send image in email body in c#

    Wednesday, April 14, 2021 12:45 PM

All replies

  • User1535942433 posted

    Hi JyotiSR,

    As far as I think,you could try this:

    MailMessage mailWithImg = GetMailWithImg();
    MySMTPClient.Send(mailWithImg); //* Set up your SMTPClient before!
    
    private MailMessage GetMailWithImg() {
        MailMessage mail = new MailMessage();
        mail.IsBodyHtml = true;
        mail.AlternateViews.Add(GetEmbeddedImage("c:/image.png"));
        mail.From = new MailAddress("yourAddress@yourDomain");
        mail.To.Add("recipient@hisDomain");
        mail.Subject = "yourSubject";
        return mail;
    }
    
    private AlternateView GetEmbeddedImage(String filePath) {
        LinkedResource res = new LinkedResource(filePath);
        res.ContentId = Guid.NewGuid().ToString();
        string htmlBody = @"<img src='cid:" + res.ContentId + @"'/>";
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        return alternateView;
    }

    More details,you could refer to below article:

    https://stackoverflow.com/questions/18358534/send-inline-image-in-email

    Best regards,

    Yijing Sun

    Thursday, April 15, 2021 5:27 AM
  • User-703669545 posted

    You can send Email with HTML contents and set

    Msg.IsBodyHtml = true;

    Complete code to send HTML email in C#

    MailMessage Msg = new MailMessage();
          Msg.From = new MailAddress("senderEmailId@gmail.com","Sender Name");// replace with valid value
          Msg.Subject = "Contact";
          Msg.To.Add("Emailto@gmail.com"); //replace with correct values
          Msg.Body = "Message";
          Msg.IsBodyHtml = true;
          Msg.Priority = MailPriority.High;
          SmtpClient smtp = new SmtpClient();
          smtp.Host = "smtp.gmail.com";
          smtp.Port = 587;
          smtp.Credentials = new System.Net.NetworkCredential("senderEmailId@gmail.com", "password");// replace with valid value
          smtp.EnableSsl = true;
          smtp.Timeout = 20000;
    
          smtp.Send(Msg);

    Source: Send Email in ASP.NET MVC C# (With attachments)

    So considering you want to send text with image, you can have code like below

     MailMessage Msg = new MailMessage();
          Msg.From = new MailAddress("senderEmailId@gmail.com","Sender Name");// replace with valid value
          Msg.Subject = "Contact";
          Msg.To.Add("Emailto@gmail.com"); //replace with correct values
          Msg.Body = "You HTML Text Here <img src='https://urlhere' /><br/>. Yes this will work."; // your HTML message here
          Msg.IsBodyHtml = true; // yes body text contains HTML
          Msg.Priority = MailPriority.High;
          SmtpClient smtp = new SmtpClient();
          smtp.Host = "smtp.gmail.com";
          smtp.Port = 587;
          smtp.Credentials = new System.Net.NetworkCredential("senderEmailId@gmail.com", "password");// replace with valid value
          smtp.EnableSsl = true;
          smtp.Timeout = 20000;
    
          smtp.Send(Msg);

    One of the things to remember here, when you send email with image url, image url must be complete URL like https://sample.com/image-url.png not just "/image-url.png", basically provide complete url.

    Friday, April 16, 2021 12:39 PM