使用 C# 将图像添加到电子邮件正文

Posted

技术标签:

【中文标题】使用 C# 将图像添加到电子邮件正文【英文标题】:Add Image to Email Body using C# 【发布时间】:2017-10-25 11:39:00 【问题描述】:

我想使用 C# 将图片框中的图像添加到我的电子邮件正文。这就是我所做的。任何人都可以帮助我吗?

var fromAddress = new MailAddress("aaa@gmail.com", "aaa");
        var toAddress = new MailAddress("bbb@gmail.com", "bbb");
        const string fromPassword = "mypassword";
        const string subject = "Tes Program";
        const string body = "Bersama ini kami kirimkan QR Code sebagai sarana validasi pengiriman rekening koran Anda. Harap simpan dan tunjukkan QR Code ini saat kurir kami datang untuk mengantar rekening koran. Atas perhatiannya kami sampaikan terima kasih. Salam";

        var smtp = new SmtpClient
        
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        ;

        using (var message = new MailMessage(fromAddress, toAddress)
        

            Subject = subject,
            Body = body


        

        )
        
            smtp.Send(message);
        

我有picturebox1,想将picturebox1中显示的图片添加到我的电子邮件正文中

【问题讨论】:

您在哪里将图像加载到您的应用程序中? 图片是我在文本框中输入的内容生成的二维码,然后它将显示在图片框中 Send inline image in email的可能重复 【参考方案1】:

您需要将其添加为电子邮件的嵌入式资源并为其创建 html 视图,这是一个入门示例:

    private static void AddImageToEmail(MailMessage mail, Image image)
    
        var imageStream = GetImageStream(image);

        var imageResource = new LinkedResource(imageStream, "image/png")  ContentId = "added-image-id" ;
        var alternateView = AlternateView.CreateAlternateViewFromString(mail.Body, mail.BodyEncoding, MediaTypeNames.Text.Html);

        alternateView.LinkedResources.Add(imageResource);
        mail.AlternateViews.Add(alternateView);
    

    private static Stream GetImageStream(Image image)
    
        // Conver the image to a memory stream and return.
        var imageConverter = new ImageConverter();
        var imgaBytes = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
        var memoryStream = new MemoryStream(imgaBytes);

        return memoryStream;
    

【讨论】:

【参考方案2】:

您需要调整代码以包含内嵌图像。

请参阅以下已回答此问题的问题:

Link 1

Link 2

代码 sn-p(您需要添加类似的内容才能获得相同的结果),取自链接 1;

protected void Page_Load(object sender, EventArgs e)

    string Themessage = @"<html>
                      <body>
                        <table 100%"">
                        <tr>
                            <td style=""font-style:arial; color:maroon; font-weight:bold"">
                           Hi! <br>
                            <img src=cid:myImageID>
                            </td>
                        </tr>
                        </table>
                        </body>
                        </html>";
    sendHtmlEmail("from@gmail.com", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);


protected void sendHtmlEmail(string from_Email, string to_Email, string body, string           from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)

    //create an instance of new mail message
    MailMessage mail = new MailMessage();

    //set the HTML format to true
    mail.IsBodyHtml = true;

    //create Alrternative HTML view
    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

    //Add Image
    LinkedResource theEmailImage = new LinkedResource("E:\\IMG_3332.jpg");
    theEmailImage.ContentId = "myImageID";

    //Add the Image to the Alternate view
    htmlView.LinkedResources.Add(theEmailImage);

    //Add view to the Email Message
    mail.AlternateViews.Add(htmlView);

    //set the "from email" address and specify a friendly 'from' name
    mail.From = new MailAddress(from_Email, from_Name);

    //set the "to" email address
    mail.To.Add(to_Email);

    //set the Email subject
    mail.Subject = Subject;

    //set the SMTP info
    System.Net.NetworkCredential cred = new System.Net.NetworkCredential("fromEmail@gmail.com", "fromEmail password");
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = cred;
    //send the email
    smtp.Send(mail);

【讨论】:

以上是关于使用 C# 将图像添加到电子邮件正文的主要内容,如果未能解决你的问题,请参考以下文章

使用 Python Win32 发送电子邮件。将图像添加到电子邮件正文不起作用

将页眉和页脚作为内联 C# 添加到电子邮件

联系表格 7 在电子邮件正文中添加图像

如何使用 MFMailComposeViewController 在电子邮件正文中添加图像

Python将内联图像添加到多部分/替代电子邮件

如何删除图像作为附件但显示在电子邮件正文中