将c#图像嵌入到html电子邮件中

时间:2017-12-12 14:35:21

标签: c# image html-email

我有一个返回Image的类:

public static Image GenerateVoucher(decimal voucherAmount, string voucherCode, 
DateTime expiryDate)
 {
   ...
        Image img = Image.FromFile(path);
        using (Graphics graphics = Graphics.FromImage(img))
        {
            ...various drawing on the image...
            graphics.DrawString(voucherAmount.ToString(), new Font(handleGothic.Families[0], 113), blueBrushColour, largeVoucherAmountLocation);

        }
        return img;
    }

我希望将此回复嵌入电子邮件中:

    public static MailMessage CreateImagedEmail(string mailTo, decimal voucherAmount, string voucherCode, DateTime expiryDate)
    {
        var mail = new MailMessage();

        mail.From = new MailAddress("fromaddress.com");
        mail.To.Add(mailTo);
        mail.Subject = "Your Email with an image inline";

        var plainView = AlternateView.CreateAlternateViewFromString(
           "Your email client has detected it is unable to render html emails...",
           null, "text/plain");

        var htmlView = AlternateView.CreateAlternateViewFromString(
           "<h1>Some HTML message </h1>" +
           "<img src=cid:voucher>",
           null, "text/html");
        var voucher = GenerateVoucher(voucherAmount, voucherCode, expiryDate);
        LinkedResource voucherImg = new LinkedResource(path);

        voucherImg.ContentId = "voucher";

        htmlView.LinkedResources.Add(voucherImg);

        mail.AlternateViews.Add(plainView);
        mail.AlternateViews.Add(htmlView);

        return mail;
    }

当从文件系统返回图像时,这在上面的示例中工作正常,但是当我尝试从上面的方法添加Image时,LinkedResources抱怨它不是字符串。我很乐意将其转换为流,如果可行的话

0 个答案:

没有答案