简体   繁体   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?

How I can create a email attachment in C# and asp.net. 如何在C#和asp.net中创建电子邮件附件。 I want use a html file that describe the attachment and I want it load in a kind of a message string in my application. 我想使用描述附件的html文件,并希望它在我的应用程序中以某种消息字符串的形式加载。 Than I want do replace substrings in this message with other values that I get from a database. 比我想用我从数据库中获得的其他值替换此消息中的子字符串。 If the attachment is create I want to send it to a address. 如果附件已创建,我想将其发送到一个地址。

I use a helpclass now but I think it is not the right way :/ 我现在使用一个帮助类,但我认为这不是正确的方法:/

I don't know whether it exist in .net libary. 我不知道它是否存在于.net库中。 a kind of class or something. 一种类或某种东西。

What is the best way to do it? 最好的方法是什么?

here is the method how I make it now: namespave = using SmtpMail = EASendMail.SmtpMail; 这是我现在做的方法: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)
            {

            }
        }

UPDATE: 更新:

Now I have create a html file with images but I bind this images with base64 coding. 现在,我创建了一个带有图像的html文件,但是我使用base64编码绑定了这些图像。 it works but if I read this html in the c# application I don't can send this mail. 它可以工作,但是如果我在C#应用程序中阅读了此html,则无法发送此邮件。 I make a breakepoint and look in my readstring but it is all right :/ 我做一个断点并查看我的readstring,但没关系:/

here the code: 这里的代码:

...
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. Add the file as an embedded resource. 将文件添加为嵌入式资源。
  2. Open it and read the content 打开它并阅读内容
  3. Send the content. 发送内容。

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