簡體   English   中英

我如何在ex.html c#中使用extern html文件作為附件並將其發送到電子郵件地址?

[英]How I can use a extern html file for a attachment in asp.net c# and send it to a email address?

如何在C#和asp.net中創建電子郵件附件。 我想使用描述附件的html文件,並希望它在我的應用程序中以某種消息字符串的形式加載。 比我想用我從數據庫中獲得的其他值替換此消息中的子字符串。 如果附件已創建,我想將其發送到一個地址。

我現在使用一個幫助類,但我認為這不是正確的方法:/

我不知道它是否存在於.net庫中。 一種類或某種東西。

最好的方法是什么?

這是我現在做的方法:namespave = using SmtpMail = EASendMail.SmtpMail;

private void SendMail(string vorname, string nachname, string anrede, string firma, string benutzername, string passwort, string von, string bis, string email)
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            oMail.From = email;

            oMail.To = email;

            oMail.Subject = "Company (" + nachname + ", " + vorname + ")";
            SmtpServer oServer = new SmtpServer(SMTPSERVER);

            try
            {
                Attachment header = oMail.AddAttachment(Properties.Settings.Default.ATT_header);
                Attachment footer = oMail.AddAttachment(Properties.Settings.Default.ATT_footer);
                Attachment left = oMail.AddAttachment(Properties.Settings.Default.ATT_left);
                Attachment right = oMail.AddAttachment(Properties.Settings.Default.ATT_right);
                Attachment world = oMail.AddAttachment(Properties.Settings.Default.ATT_world);
                Attachment company = oMail.AddAttachment(Properties.Settings.Default.ATT_company);
                Attachment weltkarte_header = oMail.AddAttachment(Properties.Settings.Default.ATT_weltkarte);

                string contentID_header = "header";
                header.ContentID = contentID_header;

                string contentID_footer = "footer";
                footer.ContentID = contentID_footer;

                string ContentID_left = "left";
                left.ContentID = ContentID_left;

                string ContentID_right = "right";
                right.ContentID = ContentID_right;

                string ContentID_world = "world";
                world.ContentID = ContentID_world;

                string ContentID_company = "company";
                company.ContentID = ContentID_company;

                string ContentID_weltkarte_header = "weltkarte_header";
                weltkarte_header.ContentID = ContentID_weltkarte_header;

                string htmltext = "<html><body><table width='1000px' border='0' cellpadding='0' cellspacing='0'>" +
                                    "<tr><img src=\"cid:" + contentID_header + "\"></tr>" +
                                    "<tr><img src=\"cid:" + ContentID_weltkarte_header + "\"></tr>" +
                                    "<tr><table border='0' cellpadding='0' cellspacing='0'>" +
                                         "<tr>" +
                                             "<td><img src=\"cid:" + ContentID_left + "\"></td>" +
                                             "<td width='880' style='background-color:#efefef;'>" +
                                                    "<p align='center'>Sie haben einen Gastzugang für [Anrede] [Vorname] [Nachname],[Firma] eingerichtet.</p>" +
                                                    "<p align='center'>Im folgenden finden Sie die Zugangsdaten,</br>" +
                                                    "die für die Anmeldung am Netzwerk benötigt werden.Weitere Informationen stehen auf der Anmeldeseite zur Verfügung.</p>" +
                                                    "<p align='center'><b>Benutzername: [Benutzername]</b><br/><b>Kennwort: [Passwort]</b></p>" +
                                                    "<p align='center'>Der Zugang wird vom [ZeitVon] bis [ZeitBis] freigeschaltet sein.</p>" +
                                             "</td>" +
                                             "<td><img src=\"cid:" + ContentID_right + "\"></td>" +
                                         "</tr>" +
                                    "</table></tr>" +
                                    "<tr><img src=\"cid:" + ContentID_company + "\"></tr>" +
                                    "<tr><img src=\"cid:" + contentID_footer + "\"></tr>" +
                                "</table></body></html>";

                htmltext = htmltext.Replace("[Anrede]", anrede).Replace("[Vorname]", vorname).Replace("[Firma]", firma).Replace("[Nachname]", nachname);
                htmltext = htmltext.Replace("[Benutzername]", benutzername).Replace("[Passwort]", passwort);
                htmltext = htmltext.Replace("[ZeitVon]", von).Replace("[ZeitBis]", bis);

                oMail.HtmlBody = htmltext;

                oSmtp.SendMail(oServer, oMail);

            }
            catch (Exception)
            {

            }
        }

更新:

現在,我創建了一個帶有圖像的html文件,但是我使用base64編碼綁定了這些圖像。 它可以工作,但是如果我在C#應用程序中閱讀了此html,則無法發送此郵件。 我做一個斷點並查看我的readstring,但沒關系:/

這里的代碼:

...
SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            oMail.From = mail;

            oMail.To = mail;

            oMail.Subject = "company (" + lastname + ", " + firstname + ")";
            SmtpServer oServer = new SmtpServer(SMTPSERVER);

            try
            {


                using (StreamReader reader = new StreamReader(Server.MapPath("~/App_Data/zugangsmail.html"), System.Text.Encoding.Default))
                {
                    string message = reader.ReadToEnd();

                    message = message.Replace("[Anrede]", title).Replace("[Vorname]", firstname).Replace("[Firma]", company).Replace("[Nachname]", lastname);
                    message = message.Replace("[Benutzername]", username).Replace("[Passwort]", password);
                    message = message.Replace("[ZeitVon]", from).Replace("[ZeitBis]", to);

                    oMail.HtmlBody = message;
                    oSmtp.SendMail(oServer, oMail);
                }

            }
            catch (Exception ex)
            {
                error.Visible = true;
                lblErrorMessage.Text = ex.Message; 
            }
...
  1. 將文件添加為嵌入式資源。
  2. 打開它並閱讀內容
  3. 發送內容。

var assembly = Assembly.GetExecutingAssembly();

using (var stream = asssembly.GetManifestResourceStream("namespace.folder.filename))
using (StreamReader reader = new StreamReader(stream))
{
     string result = reader.ReadToEnd();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM