【问题标题】:sending mail along with embedded image using asp.net使用 asp.net 发送邮件和嵌入的图像
【发布时间】:2010-11-09 22:41:07
【问题描述】:

使用 asp.net 发送邮件和嵌入图像

我已经用过following但是不能用

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)

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    感谢其他答案,我设法让它工作-唯一的区别是图像的目录-如果图像与应用程序的目录相同,则可以使用例如:Directory.GetCurrentDirectory() + @"\ClientApp\public\img\blur.jpg

        public void SendMail(string receiver, string subject, string content)
        {
            SmtpClient client = new SmtpClient(emailServiceConfig.SmtpServer);
            client.EnableSsl = true;  // Important ###
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(emailServiceConfig.Username, emailServiceConfig.Password);
    
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(emailServiceConfig.From);
            mailMessage.To.Add(receiver);
            //mailMessage.Body = content;
            mailMessage.Subject = subject;
    
    
    
        //Adding image =============================================    
        string html = @$"<html><body><img src=""cid:PageScreenshot"">{content}</body></html>";
        AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
        LinkedResource screenshotRes = new LinkedResource(Directory.GetCurrentDirectory() + @"\ClientApp\public\img\screeenshot2.jpg", MediaTypeNames.Image.Jpeg);
        screenshotRes.ContentId = "PageScreenshot";
        altView.LinkedResources.Add(screenshotRes);
        mailMessage.AlternateViews.Add(altView);
    
    
        client.Send(mailMessage);
    }
    

    【讨论】:

      【解决方案2】:

      在搜索和尝试之后必须有四五个“答案”,我觉得我必须分享我最终发现的实际工作,因为很多人似乎不知道如何做到这一点,或者有些人给出了许多其他人的详尽答案问题,加上一些问题,只给出一个 sn-p 答案,然后必须对其进行解释。因为我没有博客,但我想帮助其他人,这里有一些完整的代码来完成这一切。非常感谢 Alex Peck,因为这是他的答案扩展。

      inMy.aspx asp.net 文件

      <div>
          <asp:LinkButton ID="emailTestLnkBtn" runat="server" OnClick="sendHTMLEmail">testemail</asp:LinkButton>
      </div>
      

      c#文件后面的inMy.aspx.cs代码

      protected void sendHTMLEmail(object s, EventArgs e)
      {
          /* adapted from http://stackoverflow.com/questions/1113345/sending-mail-along-with-embedded-image-using-asp-net 
             and http://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:\Temp\HTMLfile.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; //"<html><body>Text here <br/>- picture here <br /><br /><img src=""cid:SACP_logo_sml.jpg""></body></html>";
      
          // 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

      <html>
      <body>
          <img src="cid:logo_sml.jpg">
          <br />
          Hi %TEMPLATE_TOKEN1% .
          <br />
          <br/>
          Your mobile no is %TEMPLATE_TOKEN2%
          <br />
          <br />
          <img src="cid:booking_btn.jpg">
      </body>
      </html>
      

      在您的 Web.Config 文件中,在您的 块中,您需要以下内容才能在 c:\drive 上的 TempMail 文件夹中进行测试

      <system.net>
          <mailSettings>
              <smtp deliveryMethod="SpecifiedPickupDirectory" from="madeupEmail@address.com">
                  <specifiedPickupDirectory pickupDirectoryLocation="C:\TempMail"/>
              </smtp>
          </mailSettings>
      </system.net>
      

      在您的 aspx.cs 代码隐藏文件顶部您需要的唯一其他东西是使用系统包含(如果我错过了一个,您只需右键单击未知类并选择“解决”选项)

      using System.Net.Mail;
      using System.Text;
      using System.Reflection;
      using System.Net.Mime; // need for mail message and text encoding
      using System.IO;
      

      希望这对某人有所帮助,非常感谢上面的海报给出了完成工作所需的答案(以及我代码中的其他链接)。

      它有效,但我愿意改进。

      干杯。

      【讨论】:

        【解决方案3】:

        如果您使用的是 .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 等价物。

        【讨论】:

          猜你喜欢
          • 2010-11-01
          • 2011-06-01
          • 2019-12-12
          • 2015-12-17
          • 2013-03-23
          • 2012-04-07
          • 2011-07-14
          • 2016-05-25
          相关资源
          最近更新 更多