简体   繁体   English

下拉列表以选择要作为邮件正文发送的html页面

[英]dropdownlist to select the html page to be sent as mail body

I have made a mailer where I want to send a html page as the mail body. 我做了一个邮件发送器,我想在其中发送一个HTML页面作为邮件正文。 Here is the code: 这是代码:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    {
        SendHTMLMail();
    }

    void SendHTMLMail()
    {
        MailMessage Msg = new MailMessage();

        Msg.From = new MailAddress(txtUsername.Text);

        Msg.To.Add(txtTo.Text);
        Msg.Subject = txtSubject.Text;
        Msg.Body = myString.ToString();
        Msg.IsBodyHtml = true;

        if (fuAttachment.HasFile)
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);

            Msg.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
        smtp.EnableSsl = true;
        smtp.Send(Msg);
        Msg = null;
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }
}

I have certain html pages in my app data folder. 我的应用程序数据文件夹中有某些html页面。 Now what I want is a dropdown from which I can select the html page that I send as the mail body. 现在我想要的是一个下拉列表,从中可以选择作为邮件正文发送的html页面。 How can I populate the dropdown to select among those html pages? 如何填充下拉菜单以在这些html页面中进行选择?

As I am not sure about which platform you are trying to do that. 由于我不确定要尝试使用哪个平台。 So, I am going to write a generic approach. 因此,我将编写一种通用方法。 In order to read the files and its content from the App_Data folder you need to first get the path of the App_Data : 为了从App_Data文件夹中读取文件及其内容,您需要首先获取App_Data的路径:

For web application 对于网络应用

DirectoryInfo di = new DirectoryInfo(HostingEnvironment.MapPath("/App_Data"));

For windows application 对于Windows应用程序

string projectFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).FullName;
string folderAppData = Path.Combine(projectFolder, "App_Data");

Now to get all the files from the App_Data folder use this function which will return a List<KeyValuePair<string,string>> 现在,要从App_Data文件夹中获取所有文件,请使用此函数,该函数将返回List<KeyValuePair<string,string>>

For web application: 对于Web应用程序:

public List<KeyValuePair<string, string>> getFiles()
{            
     DirectoryInfo di = new DirectoryInfo(HostingEnvironment.MapPath("/App_Data"));
     var fi = di.EnumerateFiles("*.html", SearchOption.TopDirectoryOnly);
     var files = new List<KeyValuePair<string, string>>();
     foreach (FileInfo file in fi)
     {
       files.Add(new KeyValuePair<string, string>(file.Name, file.FullName));
     }
     return files;
}

For windows application: 对于Windows应用程序:

public List<KeyValuePair<string, string>> getFiles()
{
   string projectFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).FullName;
   string folderAppData = Path.Combine(projectFolder, "App_Data");
   var diw = new DirectoryInfo(folderAppData);
   var fiw = diw.EnumerateFiles("*.html", SearchOption.TopDirectoryOnly);
   var filesw = new List<KeyValuePair<string, string>>();
   foreach (FileInfo file in fiw)
   {
      filesw.Add(new KeyValuePair<string, string>(file.Name, file.FullName));
   }
   return filesw;
}

Now with this function, you will get data in the format of a key-value pair as: 现在,使用此功能,您将获得键值对格式的数据,如下所示:

     Key    |    value
file1.html  |  c://somefolder/App_Data/file1.html
..... other files

Now you can use this list to populate the data in the dropdown where the key object is the file name which you can use as the text field and value object is the file path which you can use as the value field. 现在,您可以使用此列表填充dropdown列表中的数据,其中key对象是可以用作text字段的文件名, value对象是可以用作value字段的文件路径。

And now to read the file contents you have to pass the file path got from the function declared above by passing the file path: 现在,要读取文件内容,您必须通过传递文件路径来传递从上面声明的函数获得的文件路径:

public string getFileContent(string path)
{
   return System.IO.File.ReadAllText(path);
}

Binding for webForms: 绑定webForms:

in the Page_load() event just add these lines 在Page_load()事件中,只需添加以下行

if (!IsPostBack)
{
    var list = getFileNames();
    ddl.DataSource = list;
    ddl.DataTextField = "Key";
    ddl.DataValueField = "Value";
    ddl.DataBind();
}

Note : for a better approach it is advisable to create a separate class to handle the files. 注意 :为了更好的方法,建议创建一个单独的类来处理文件。 Whereas in production don't store the file path on the client side (in HTML) instead of this, store the file path in the database and give the id of the file as the value for the dropdown than on your mail send just get the file path from the DB and then get the content. 而在生产环境中,不要在客户端(以HTML格式)存储文件路径,而是在客户端存储文件路径,并在文件中指定文件ID作为下拉列表的值,而不是在邮件中发送即可。从数据库获取文件路径,然后获取内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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