Add multiple images in the email body (inline)using c# windows application

后端 未结 2 1242
说谎
说谎 2020-12-02 02:21

I searched for this several times and found solutions,but all supports only one image.Finally I used this code. But the problem is,if the html contain more than one image o

相关标签:
2条回答
  • Try this code. This will generate attachment for excel , csv , pdf and also inline attachment i.e. in the body of email for png format of image. For png format multiple image will get embedded inline one after other by GetEmbeddedImage method. You can customize it based on your requirement.

    SmtpClient smtpServer = new SmtpClient(smtpServerName);
    smtpServer.Port = 25;
    smtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
    //smtpServer.EnableSsl = true;
    MailMessage smtpEmail = new MailMessage();
    string messageBodyImage = @"<img width=1200 id=""MyContent"" src=""cid:{0}"">";
     toAddressList = toAddress.Split(';');
    foreach (string toEmail in toAddressList)
        smtpEmail.To.Add(toEmail);
    
    smtpEmail.From = new MailAddress(fromAddress);
    smtpEmail.Body = messageBody;
    smtpEmail.Subject = subject;
    foreach (string format in fileExtension)
     {    
        switch (format)
         {       
        case "PNG": 
        smtpEmail.IsBodyHtml = true;
        smtpEmail.AlternateViews.Add(GetEmbeddedImage(reportByteStream, messageBodyImage, format)); 
        break;
        case "CSV":      
        smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, "text/csv"));  
        break;
        case "XLS": 
        smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, "application/vnd.ms-excel"));
        break;
        default: // For PDF
        smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, MediaTypeNames.Application.Pdf));
        break;
        }
    }
    

    Method to embed multiple image inline.

        private AlternateView GetEmbeddedImage(Dictionary<string, Byte[][]> streamAttachment, string msgTemplate, string fileFormat)
        {
            LinkedResource imageFile = null;
            AlternateView alternateView = null;
            string msgBody = string.Empty;
            try
            {
            List<LinkedResource> imageFiles = new List<LinkedResource>();
            for (int page = 0; page < streamAttachment[fileFormat].Length; page++)
            {   
                    imageFile = new LinkedResource(new MemoryStream(streamAttachment[fileFormat][page]));
                    imageFile.ContentId = Guid.NewGuid().ToString();
                    msgBody = msgBody + "<BR/>" + string.Format(msgTemplate, imageFile.ContentId);
                    imageFiles.Add(imageFile); 
            }
    
            alternateView = AlternateView.CreateAlternateViewFromString(msgBody, null, MediaTypeNames.Text.Html);
            imageFiles.ForEach(img => alternateView.LinkedResources.Add(img));
            }
            catch (Exception Ex)
            {
    
            }
            return alternateView;
        } 
    
    0 讨论(0)
  • 2020-12-02 03:15

    You can attach images to mail and then put img tag and useContentId of attachment as src this way:

    private void denMailButton_Click(object sender, EventArgs e)
    {
        string subject = "Subject";
        string body = @"Image 1: <img src=""$CONTENTID1$""/> <br/> Image 2: <img src=""$CONTENTID2$""/> <br/> Some Other Content";
    
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("from@example.com");
        mail.To.Add(new MailAddress("to@example.com"));
        mail.Subject = subject;
        mail.Body = body;
        mail.Priority = MailPriority.Normal;
    
        string contentID1 = Guid.NewGuid().ToString().Replace("-", "");
        string contentID2 = Guid.NewGuid().ToString().Replace("-", "");
    
        body = body.Replace("$CONTENTID1$", "cid:" + contentID1);
        body = body.Replace("$CONTENTID2$", "cid:" + contentID2);
    
        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    
        //path of image or stream
        LinkedResource imagelink1 = new LinkedResource(@"D:\1.png", "image/png");
        imagelink1.ContentId = contentID1;
        imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
        htmlView.LinkedResources.Add(imagelink1);
    
        LinkedResource imagelink2 = new LinkedResource(@"D:\2.png", "image/png");
        imagelink2.ContentId = contentID2;
        imagelink2.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
        htmlView.LinkedResources.Add(imagelink2);
    
        mail.AlternateViews.Add(htmlView);
    
        SmtpClient client = new SmtpClient();
        client.Host = "mail.example.com";
        client.Credentials = new NetworkCredential("from@example.com", "password");
        client.Send(mail);
    }
    

    And here is the screenshot:

    0 讨论(0)
提交回复
热议问题