Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net c#中使用外部html文件作为附件并将其发送到电子邮件地址?_C#_Asp.net_.net_Attachment_Email Attachments - Fatal编程技术网

如何在asp.net c#中使用外部html文件作为附件并将其发送到电子邮件地址?

如何在asp.net c#中使用外部html文件作为附件并将其发送到电子邮件地址?,c#,asp.net,.net,attachment,email-attachments,C#,Asp.net,.net,Attachment,Email Attachments,如何在C#和asp.net中创建电子邮件附件。我想使用一个html文件来描述附件,我想在我的应用程序中以一种消息字符串的形式加载它。然后我想用从数据库获得的其他值替换此消息中的子字符串。如果附件是创建的,我想把它发送到一个地址 我现在使用帮助类,但我认为这不是正确的方法:/ 我不知道它是否存在于.net图书馆中。一种阶级之类的 最好的方法是什么 下面是我现在使用的方法:namespave=使用SmtpMail=EASendMail.SmtpMail private void SendMail(s

如何在C#和asp.net中创建电子邮件附件。我想使用一个html文件来描述附件,我想在我的应用程序中以一种消息字符串的形式加载它。然后我想用从数据库获得的其他值替换此消息中的子字符串。如果附件是创建的,我想把它发送到一个地址

我现在使用帮助类,但我认为这不是正确的方法:/

我不知道它是否存在于.net图书馆中。一种阶级之类的

最好的方法是什么

下面是我现在使用的方法:namespave=使用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)
            {

            }
        }
  • 将文件添加为嵌入式资源
  • 打开它并阅读内容
  • 发送内容
  • 
    var assembly=assembly.getExecutionGassembly()

    使用(var stream=assassembly.GetManifestResourceStream(“namespace.folder.filename)) 使用(StreamReader=新StreamReader(stream)) { 字符串结果=reader.ReadToEnd(); }
    在msdn上查找httprequest类以获取网页查看我的更新:在第一种方法中,它可以工作,但我不使用外部文件;在第二种方法中,我使用外部文件,但我不发送邮件:(我没有收到任何错误…你介意给我发送html吗?我很确定问题会在那里,我想自己构建一个测试程序来检查它。
    ...
    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; 
                }
    ...
    
    using (var stream = asssembly.GetManifestResourceStream("namespace.folder.filename)) using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); }