一尘不染

使用asp.net发送邮件以及嵌入式图像

c#

使用asp.net发送邮件以及嵌入式图像

我已经使用过以下功能,但无法使用

Dim EM As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text)
        Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text)
        Dim RGen As Random = New Random()
        A.ContentId = RGen.Next(100000, 9999999).ToString()
        EM.Attachments.Add(A)
        EM.Subject = txtSubject.Text
        EM.Body = "<body>" + txtBody.Text + "<br><img src='cid:" + A.ContentId +"'></body>"
        EM.IsBodyHtml = True
        Dim SC As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(txtSMTPServer.Text)
        SC.Send(EM)

阅读 271

收藏
2020-05-19

共1个答案

一尘不染

如果您使用的是.NET 2或更高版本,则可以使用AlternateView和LinkedResource类,如下所示:

string html = @"<html><body><img src=""cid:YourPictureId""></body></html>";
AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);

LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
yourPictureRes.ContentId = "YourPictureId";
altView.LinkedResources.Add(yourPictureRes);

MailMessage mail = new MailMessage();
mail.AlternateViews.Add(altView);

希望您可以推断出VB等效项。

2020-05-19