简体   繁体   中英

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. 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. Now what I want is a dropdown from which I can select the html page that I send as the mail body. How can I populate the dropdown to select among those html pages?

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 :

For web application

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

For windows application

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

For web application:

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:

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.

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:

in the Page_load() event just add these lines

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.

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