发送带有正文图像的电子邮件

时间:2021-01-20 18:26:14

标签: c# asp.net smtp

我想发送一封正文顶部带有徽标的电子邮件。这是我的方法:

public bool SendEmail(string toAddress)
        {
            SmtpClient smtpServer = new SmtpClient("the server");

            try
            {
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<image src=cid:logo  style=\"height: 50px;\"><br>Hello World", null, MediaTypeNames.Text.Html);
                
                LinkedResource imageResource = new LinkedResource(Server.MapPath(@"..\..\images\logo.png"));
                imageResource.ContentId = "logo";
                htmlView.LinkedResources.Add(imageResource);

                smtpServer.Port = port;
                smtpServer.Credentials = new NetworkCredential("user", "pass");
                smtpServer.EnableSsl = false;

                MailMessage message = new MailMessage();
                message.To.Add(toAddress);
                message.From = new MailAddress("the address");
                message.Subject = "Some subject";
                message.AlternateViews.Add(htmlView);
                message.IsBodyHtml = true;

                smtpServer.Send(message);
                smtpServer.Dispose();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

该方法正在发送电子邮件,但图像作为附件发送。我试过这样做:

LinkedResource imageResource = new LinkedResource(Server.MapPath(@"..\..\images\logo.png"), MediaTypeNames.Image.Jpeg);

但是当我在这一行中添加“MediaTypeNames.Image.Jpeg”时,发送的电子邮件根本没有图像。

有人可以帮助我吗?提前致谢!

3 个答案:

答案 0 :(得分:0)

您可以将图像转换为 base64 并将其添加到 src 中,或者您可以只提供图像所在的页面。

答案 1 :(得分:0)

您可以试试图书馆MailKit。使用此库可以很好地发送嵌入的图像。

var stream = new MemoryStream();
image.Save(stream, ImageFormat.Png);
stream.Position = 0;

var resource = builder.LinkedResources.Add(contentId, stream);
resource.ContentId = contentId;

// in the email message, there is img with src="cid:contentId"

嵌入图像的另一个示例是 here

答案 2 :(得分:0)

你可以使用它:

 public bool SendEmail(string toAddress)
    {
        SmtpClient smtpServer = new SmtpClient("the server");

        try
        {
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<image src=cid:logo  style=\"height: 50px;\"><br>Hello World", null, MediaTypeNames.Text.Html);

            LinkedResource imageResource = new LinkedResource(Server.MapPath(@"..\..\images\logo.png"));
            imageResource.ContentId = "logo";
            htmlView.LinkedResources.Add(imageResource);

            smtpServer.Port = 0; //add port here
            smtpServer.Credentials = new NetworkCredential("user", "pass");
            smtpServer.EnableSsl = false;

            MailMessage message = new MailMessage();
            message.To.Add(toAddress);
            message.From = new MailAddress("the address");
            message.Subject = "Some subject";

            //add file here
            Attachment attachment = new Attachment("c://image");
            attachment.ContentDisposition.Inline = true;
            message.Attachments.Add(attachment);

            message.AlternateViews.Add(htmlView);
            message.IsBodyHtml = true;


            smtpServer.Send(message);
            smtpServer.Dispose();

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
相关问题