使用asp.net发送邮件和嵌入式图像
使用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 = "" + txtBody.Text + "
" EM.IsBodyHtml = True Dim SC As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(txtSMTPServer.Text) SC.Send(EM)
如果您使用的是.NET 2或更高版本,则可以使用AlternateView和LinkedResource类,如下所示:
string 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的等价物。
在搜索和尝试必须是四个或五个“答案”后,我觉得我必须分享我最终发现的实际工作,因为很多人似乎不知道如何做到这一点,或者有些人给出了许多其他人有问题的精心解答,加上一些,只给出一个片段答案,然后必须解释。 由于我没有博客,但我想帮助其他人,这里有一些完整的代码可以做到这一切。 非常感谢Alex Peck,因为这是他的答案。
inMy.aspx asp.net文件
testemail
inMy.aspx.cs代码在c#文件后面
protected void sendHTMLEmail(object s, EventArgs e) { /* adapted from https://stackoverflow.com/questions/1113345/sending-mail-along-with-embedded-image-using-asp-net and https://stackoverflow.com/questions/886728/generating-html-email-body-in-c-sharp */ string myTestReceivingEmail = "yourEmail@address.com"; // your Email address for testing or the person who you are sending the text to. string subject = "This is the subject line"; string firstName = "John"; string mobileNo = "07711 111111"; // Create the message. var from = new MailAddress("emailFrom@address.co.uk", "displayed from Name"); var to = new MailAddress(myTestReceivingEmail, "person emailing to's displayed Name"); var mail = new MailMessage(from, to); mail.Subject = subject; // Perform replacements on the HTML file (which you're using as a template). var reader = new StreamReader(@"c:TempHTMLfile.htm"); string body = reader.ReadToEnd().Replace("%TEMPLATE_TOKEN1%", firstName).Replace("%TEMPLATE_TOKEN2%", mobileNo); // and so on as needed... // replaced this line with imported reader so can use a templete .... //string html = body; //"Text here
- picture here
"; // Create an alternate view and add it to the email. Can implement an if statement to decide which view to add // AlternateView altView = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); // Logo 1 // string imageSource = (Server.MapPath("") + "\logo_sml.jpg"); LinkedResource PictureRes = new LinkedResource(imageSource, MediaTypeNames.Image.Jpeg); PictureRes.ContentId = "logo_sml.jpg"; altView.LinkedResources.Add(PictureRes); // Logo 2 // string imageSource2 = (Server.MapPath("") + "\booking_btn.jpg"); LinkedResource PictureRes2 = new LinkedResource(imageSource2, MediaTypeNames.Image.Jpeg); PictureRes2.ContentId = "booking_btn.jpg"; altView.LinkedResources.Add(PictureRes2); mail.AlternateViews.Add(altView); // Send the email (using Web.Config file to store email Network link, etc.) SmtpClient mySmtpClient = new SmtpClient(); mySmtpClient.Send(mail); }
HTMLfile.htm
Hi %TEMPLATE_TOKEN1% .
Your mobile no is %TEMPLATE_TOKEN2%
在您的Web.Config文件中,在
在aspx.cs代码隐藏文件的顶部,你需要的唯一其他东西是使用系统包括(如果我错过了一个你只需右键单击未知类并选择’Resolve’选项)
using System.Net.Mail; using System.Text; using System.Reflection; using System.Net.Mime; // need for mail message and text encoding using System.IO;
希望这有助于某人并非常感谢上面的海报,以提供完成工作所需的答案(以及我的代码中的其他链接)。
它有效,但我愿意改进。
干杯。
上述就是C#学习教程:使用asp.net发送邮件和嵌入式图像分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1029719.html