【问题标题】:Is there any way to send HTML e-mail with embedded image in UWP有没有办法在 UWP 中发送带有嵌入图像的 HTML 电子邮件
【发布时间】:2019-01-24 18:07:03
【问题描述】:

我正在开发一个 uwp 应用程序来发送带有嵌入图像的 html 电子邮件。我正在使用 EASendMail nuget pakage,过了一段时间我的应用程序显示错误之后就很好了:

连接尝试失败,因为连接方没有 一段时间后正确响应,或建立连接 失败,因为连接的主机没有响应。 (例外来自 HRESULT: 0x8007274c)

我认为试用期已过,我该怎么办?

使用 EASendMailRT; https://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=8

我找不到其他选择

try
{
    string ToAddress = MailSendPage.toAddressTxtBox;
    string Subject = MailSendPage.subjectTxtBox;
    SmtpMail oMail = new SmtpMail("TryIt");
    oMail.From = new MailAddress(username);

if(!String.IsNullOrEmpty(ToAddress)&& !String.IsNullOrEmpty(Subject))
{
    oMail.To.Add(new MailAddress(ToAddress));
    oMail.Subject = Subject;
    EASendMailRT.SmtpClient oSmtp = new EASendMailRT.SmtpClient();
    SmtpServer oServer = new SmtpServer(host);
    oServer.User = username;
    oServer.Password = password;
    oServer.Port = port;
    if (IsStackPanalHasImg() == true)
    {
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        string[] files = Directory.GetFiles(localFolder.Path + @"\ProjectImages");
        foreach (string eachfile in files)
        {
            foreach (string name in covertToHtml.ControlName)
            {
                string pattern = string.Format("{0}.jpeg", name);
                if (Regex.IsMatch(eachfile, pattern))
                {
                    Attachment oAttachment = await oMail.AddAttachmentAsync(eachfile);
                    oAttachment.ContentID = name;
                }
            }
        }
    }



    await oSmtp.SendMailAsync(oServer, oMail);
    popUpMsgs.popup(" The Mail has been sent");
}

} 捕捉(例外 ep) { popUpMsgs.popup(String.Format("发送邮件失败,出现以下错误:{0}", ep.Message)); }

【问题讨论】:

    标签: c# uwp .net-core


    【解决方案1】:

    内置电子邮件 API 仅支持以Docs 状态发送纯文本电子邮件:

    此方法仅发送纯文本消息。您不能将邮件正文设置为 HTML 格式。

    您可以在电子邮件中附加图片:

    EmailMessage mail = new EmailMessage();
    mail.Sender = new EmailRecipient("test@example.com");
    mail.To.Add(new EmailRecipient("someone@example.com"));
    mail.Subject = "Hello";
    mail.Body = "World";
    var file = await StorageFile.GetFileFromApplicationUriAsync(
          new Uri("ms-appx:///Assets/StoreLogo.png"));
    mail.Attachments.Add(new EmailAttachment(file.Name, file));
    await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(mail);
    

    此外,仅在内置 UWP Outlook 邮件客户端的情况下才能正常发送附件。经典 Outlook 很可能会完全忽略这些附件。

    如果您需要嵌入图像,则需要使用电子邮件服务。我可以推荐 SendGrid 或 MailGun。两者都有 C# API,它们可以轻而易举地工作。对于数量有限的电子邮件,它们也是免费的。

    您可以通过多种方式将图像嵌入 HTML 电子邮件中。

    最旧的是使用您在问题中使用的 CID(内容 ID)。

    第二个选项是使用 Base64 编码。您首先将图像转换为 Base64 字符串。有很多关于这方面的教程,例如this blogpost。然后您可以将图像嵌入到您的<img> 标签的src 中:

    <img src="data:image/jpeg;base64, YOURIMAGEINBASE64"/>
    

    最后,您可以嵌入托管在某处的图像。如果您需要将电子邮件发送给许多收件人,则这种方式可以实现最佳扩展,但当然需要实际将图像托管在某处。在这三种方法中,大多数客户端也支持它。

    post 详细介绍了所有三种方法。

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 2011-04-16
      • 2021-10-31
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 2015-07-29
      • 2013-11-11
      相关资源
      最近更新 更多