在 C# 中的邮件正文中附加图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2317012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:22:39  来源:igfitidea点击:

Attaching Image in the body of mail in C#

c#

提问by priyanka.bangalore

How can I attach an image in the body content . I have written the below code

如何在正文内容中附加图像。我写了下面的代码

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
string UserName = "[email protected]";
string Password = "my password";
message.To.Add(new System.Net.Mail.MailAddress("[email protected]"));
message.From = new  System.Net.Mail.MailAddress("[email protected]");              
message.Subject = "test subject";
message.Body = "<img src=@'C:\Sunset.jpg'/>";                
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
 smtpClient.Host = "hostname";
 smtpClient.Port = 25;
 smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
 smtpClient.Send(message);

The code is fine as I am receiving the message also but the image is coming as [X] inside the body and not as the image. How to solve this? The path is correct?

代码很好,因为我也收到了消息,但图像以 [X] 的形式出现在正文中,而不是图像。如何解决这个问题?路径是否正确?

采纳答案by Asad

    string attachmentPath = Environment.CurrentDirectory + @"\test.png";
    Attachment inline = new Attachment(attachmentPath);
    inline.ContentDisposition.Inline = true;
    inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
    inline.ContentId = contentID;
    inline.ContentType.MediaType = "image/png";
    inline.ContentType.Name = Path.GetFileName(attachmentPath);

    message.Attachments.Add(inline);


reference: Send an Email in C# with Inline attachments

参考:在 C# 中发送带有内联附件的电子邮件

回答by Arnis Lapsa

Use so called LinkedResource. Hereyou can find how-to. Have done that successfully.

使用所谓的LinkedResource. 在这里您可以找到操作方法。已经成功地做到了。

If tutorial does not help, don't be shy and ask for clarification. :)

如果教程没有帮助,请不要害羞并要求澄清。:)

回答by user7118831

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Net.Mime;
using System.Net.Mail;


namespace ItsTrulyFree
{
    public partial class demo_mail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    enter code here
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {


                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtUsername.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtTo.Text);
                Msg.Subject = txtSubject.Text;
                // File Upload path
                String FileName = fileUpload1.PostedFile.FileName; 


                string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";

            //LinkedResource LinkedImage = new LinkedResource(FileName);
                     //HttpContext.Current.Server.MapPath("/UploadedFiles");
            LinkedResource LinkedImage = new LinkedResource(Server.MapPath("~//" + FileName), "image/jpg");
                LinkedImage.ContentId = "MyPic";
                //Added the patch for Thunderbird as suggested by Jorge
                LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);

                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailbody+
                  " <img src=cid:MyPic>",
                  null, "text/html");

                htmlView.LinkedResources.Add(LinkedImage);
                Msg.AlternateViews.Add(htmlView);


                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                Msg = null;
                Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
            }
            //catch (Exception ex)
            //{
            //    Console.WriteLine("{0} Exception caught.", ex);
            //}
        }

}