如何使用 asp.net 4.0 和 c# 发送包含多个嵌入图像的电子邮件

how to send email with multiple embedded images with asp.net 4.0 with c#

我只想发送在 html 正文中包含多张图片的电子邮件。在这里我想将每个图像文件附加到已经在我的网络应用程序文件夹中的 html 正文。

喜欢:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>    
<title>Happy New Year - 4</title></head>
<body>  
<table cellspacing="0" cellpadding="0" style="width: 100%;">  
<tbody>
<tr>   
<td style="text-align: center; vertical-align: top; background-color: #ffffff;"><table cellspacing="0" cellpadding="0" style="width: 688px; background-color: #ffffff; text-align: center; margin-left: auto; margin-right: auto;">     <tbody><tr>      <td style="text-align: left; vertical-align: top; background-color: #ffffff;">       <table cellspacing="0" cellpadding="0" style="width: 100%;">        <tbody><tr>         <td style="text-align: left; vertical-align: bottom; background-color: #ffffff;"><div><img src=""cid:30012015_152347.jpg" alt="" /></div></td>        </tr>        <tr>         <td style="text-align: left; vertical-align: top;">          <table cellspacing="0" cellpadding="0" style="width: 100%;">           <tbody><tr>            <td style="width: 269px; text-align: left; vertical-align: top;"><div><img src=""cid:30012015_152350.jpg" alt="" /></div></td>            <td style="width: 357px; text-align: left; vertical-align: top; background-color: #a90100;">             <table cellspacing="0" cellpadding="0" style="width: 100%;">              <tbody><tr>               <td style="text-align: left; vertical-align: top; background-color: #a90100;">               <div style="margin-left: 5px; margin-right: 5px;">                <div style="font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">                 <div style="margin-top: 0px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">Dear <b>##FirstName## ##LastName##,</b></div>                 <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">Standing on the threshold of a New Year, We send you our warmest wishes. May this year shower you with everything you desire.</div>                 <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">We wish you all the best for the year ahead.</div>                 <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">Lots of love,</div>                 <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">From your friends at:</div>                 <div style="width: 100%; font-family: trebuchet ms,verdana,"ms sans serif"; font-size: 14px; color: #ffffff; margin-top: 5px; white-space: normal;"><b>##UserOrganisation##</b></div>                 </div>                                  </div>               </td>              </tr>             </tbody></table>            </td>            <td style="width: 62px; text-align: left; vertical-align: top;"><div><img src=""cid:30012015_152352.jpg" alt="" /></div></td>           </tr>          </tbody></table>         </td>        </tr>        <tr>         <td style="text-align: left; vertical-align: top; background-color: #ffffff;"><div><img src=""cid:30012015_152354.jpg" alt="" /></div></td>        </tr>                </tbody></table>      </td>     </tr>     <tr>      <td style="text-align: center; vertical-align: top; padding-top: 10px;">      <table cellspacing="0" cellpadding="0" style="width: 688px;">       <tbody><tr>        <td style="text-align: left; vertical-align: top; border-top: 1px solid #a7a7a7; border-bottom: 1px solid #a7a7a7; padding-top: 5px; padding-bottom: 5px;">        <table width="100%" cellspacing="0" cellpadding="0" style="width: 100%;">         <tbody><tr>         <td width="50%" style="border-right: 1px solid #a7a7a7; width: 50%; text-align: left;">          <div style="margin: 5px 5px 5px 8px;"><span style="font-size: 16px; color: #e92f08; font-family: trebuchet ms,verdana,"ms sans serif"; font-weight: bold;">##UserOrganisation## or<br />Logo</span></div>         </td>         <td width="50%" style="width: 50%; text-align: left;">          <div style="margin: 5px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Address : </b><br />##UserAddress1##, ##UserAddress2##,<br /></span><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px;">##UserCity## - ##UserPostalCode##.</span></div>          <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Phone : </b><img width="12" height="1" src=""cid:" alt="" style="width: 12px; height: 1px;" />##UserPhone##</span></div>          <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>FAX : </b><img width="26" height="1" src=""cid:" alt="" style="width: 26px; height: 1px;" />##UserFax##</span></div>          <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Email : </b><img width="18" height="1" src=""cid:" alt="" style="width: 18px; height: 1px;" /><a target="_blank" href="mailto:##UserEmailAddress##" style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px;">##UserEmailAddress##</a></span></div>          <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Visit us : </b><img width="7" height="1" src=""cid:" alt="" style="width: 7px; height: 1px;" />&lt;Website Address&gt;</span></div>         </td>        </tr>       </tbody></table>        </td>       </tr>      </tbody></table>      </td>     </tr>    </tbody></table>   </td>  </tr> </tbody></table></body></html>

现在检查此电子邮件正文有多个 src="cid:DDMMYYYY_HHMMSS.gif" 这里所有文件都存储在我的本地 Web 应用程序文件夹中,名称唯一。我想将每个文件附加到电子邮件正文中。 (不是电子邮件附件。电子邮件图像嵌入)。

如何处理多张图片...

请帮帮我..

你可以使用类似下面的东西...

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("your_email_address@gmail.com");
            mail.To.Add("to_address");
            mail.Subject = "Test Mail - 1";

            mail.IsBodyHtml = true;
            string htmlBody;

            htmlBody = "Write some HTML code here";

            mail.Body = htmlBody;

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            MessageBox.Show("mail Send");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
class Program
{
    static void Main(string[] args)
    {
        SmtpMail oMail = new SmtpMail("TryIt");
        SmtpClient oSmtp = new SmtpClient();

        // Set sender email address, please change it to yours
        oMail.From = "test@emailarchitect.net";

        // Set recipient email address, please change it to yours
        oMail.To = "support@emailarchitect.net";

        // Set email subject
        oMail.Subject = "test html email with attachment";

        // Your SMTP server address
        SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

        // User and password for ESMTP authentication, if your server doesn't require
        // User authentication, please remove the following codes.            
        oServer.User = "test@emailarchitect.net";
        oServer.Password = "testpassword";

        // If your SMTP server requires SSL connection, please add this line
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        try
        {
           // Import html body and also import linked image as embedded images.
           oMail.ImportHtml( "<html><body>test <img src=\"test.gif\"> importhtml</body></html>",
                "c:\my picture", //test.gif is in c:\my picture
                ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCss );

            Console.WriteLine("start to send email with embedded image...");
            oSmtp.SendMail(oServer, oMail);
            Console.WriteLine("email was sent successfully!");
        }
        catch (Exception ep)
        {
            Console.WriteLine("failed to send email with the following error:");
            Console.WriteLine(ep.Message);
        }
    }
}

}

下面的代码可以在邮件中发送多张图片

        string subject = "Subject";
        string body = @"Image 1: <img src='src1'/> <br/> Image 2: <img src='src2'/> <br/> Some Other Content";

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("<FromAddress@mail.com>");
        mail.To.Add(new MailAddress("<ToAddress@mail.com>"));
        mail.Subject = subject;
        mail.Body = body;
        mail.Priority = MailPriority.Normal;

        string contentID1 = Guid.NewGuid().ToString().Replace("-", "");
        string contentID2 = Guid.NewGuid().ToString().Replace("-", "");

        body = body.Replace("src1", "cid:" + contentID1);
        body = body.Replace("src2", "cid:" + contentID2);

        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

        //path of image or stream
        LinkedResource imagelink1 = new LinkedResource(@"F:\Images0817045614.png", "image/png");
        imagelink1.ContentId = contentID1;
        imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
        htmlView.LinkedResources.Add(imagelink1);

        LinkedResource imagelink2 = new LinkedResource(@"F:\Images0817045837.png", "image/png");
        imagelink2.ContentId = contentID2;
        imagelink2.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
        htmlView.LinkedResources.Add(imagelink2);

        mail.AlternateViews.Add(htmlView);

        using (SmtpClient smtpClient = new SmtpClient("<SMTPHostAddress>"))
        {
            smtpClient.Send(mail);
        }

希望这有效!!