![](/img/trans.png)
[英]Having a mail in previsualization from an ASP.NET page with HTML body
[英]dropdownlist to select the html page to be sent as mail body
我做了一個郵件發送器,我想在其中發送一個HTML頁面作為郵件正文。 這是代碼:
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);
}
}
我的應用程序數據文件夾中有某些html頁面。 現在我想要的是一個下拉列表,從中可以選擇作為郵件正文發送的html頁面。 如何填充下拉菜單以在這些html頁面中進行選擇?
由於我不確定要嘗試使用哪個平台。 因此,我將編寫一種通用方法。 為了從App_Data
文件夾中讀取文件及其內容,您需要首先獲取App_Data
的路徑:
對於網絡應用
DirectoryInfo di = new DirectoryInfo(HostingEnvironment.MapPath("/App_Data"));
對於Windows應用程序
string projectFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).FullName;
string folderAppData = Path.Combine(projectFolder, "App_Data");
現在,要從App_Data
文件夾中獲取所有文件,請使用此函數,該函數將返回List<KeyValuePair<string,string>>
對於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;
}
對於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;
}
現在,使用此功能,您將獲得鍵值對格式的數據,如下所示:
Key | value
file1.html | c://somefolder/App_Data/file1.html
..... other files
現在,您可以使用此列表填充dropdown
列表中的數據,其中key
對象是可以用作text
字段的文件名, value
對象是可以用作value
字段的文件路徑。
現在,要讀取文件內容,您必須通過傳遞文件路徑來傳遞從上面聲明的函數獲得的文件路徑:
public string getFileContent(string path)
{
return System.IO.File.ReadAllText(path);
}
綁定webForms:
在Page_load()事件中,只需添加以下行
if (!IsPostBack)
{
var list = getFileNames();
ddl.DataSource = list;
ddl.DataTextField = "Key";
ddl.DataValueField = "Value";
ddl.DataBind();
}
注意 :為了更好的方法,建議創建一個單獨的類來處理文件。 而在生產環境中,不要在客戶端(以HTML格式)存儲文件路徑,而是在客戶端存儲文件路徑,並在文件中指定文件ID作為下拉列表的值,而不是在郵件中發送即可。從數據庫獲取文件路徑,然后獲取內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.