简体   繁体   中英

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. 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. 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. 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;

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. it works but if I read this html in the c# application I don't can send this mail. I make a breakepoint and look in my readstring but it is all right :/

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM