我有单选按钮,从中可以选择数据源的类型来检索收件人的电子邮件地址。
if (RadioButton1.Checked)
{
sql.Open();
string s = "select * from address";
SqlCommand t = new SqlCommand(s, sql);
t.ExecuteNonQuery();
sql.Close();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT address FROM address1";
cmd.Connection = sql;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
sql.Open();
da.Fill(dt);
sql.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
else if (RadioButton2.Checked)
{
string connectionString = "";
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload1.SaveAs(fileLocation);
if (fileExtension == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT address FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dt);
con.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
}
else if (RadioButton3.Checked)
{
if (FileUpload2.HasFile)
{
string fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload2.SaveAs(fileLocation);
StreamReader sr = new StreamReader(fileLocation);
String line = sr.ReadToEnd();
string[] toAddressArray;
toAddressArray = line.Split(new char[] { ' ' });
foreach (string a in toAddressArray)
{
Msg.To.Add(a);
}
}
}
现在,我想要的是当收件人单击作为邮件正文发送的html页面中的链接时,我想从三个单选按钮数据源选项之一中检索收件人的邮件ID。
如何获取邮件ID?
这是完整的代码:
SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["mystring"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
getFiles();
if (!IsPostBack)
{
var list = getFiles();
dd1.DataSource = list;
dd1.DataTextField = "Key";
dd1.DataValueField = "Value";
dd1.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
{
SendHTMLMail();
}
void SendHTMLMail()
{
StreamReader reader = new StreamReader(dd1.SelectedItem.Value);
string readFile = reader.ReadToEnd();
Regex regx = new Regex("(?<!src=\")http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*([a-zA-Z0-9\\?\\#\\=\\/]){1})?", RegexOptions.IgnoreCase);
string output = regx.ToString();
string count = 0.ToString();
output = readFile;
string username = Server.UrlEncode(this.txtUsername.Text);
output = regx.Replace(output, new MatchEvaluator((match) =>
{
var url = Uri.EscapeDataString(match.Value.ToString());
return $"http://localhost:61187/two?sender={username}&link={url}&count={count}";
}));
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(txtUsername.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = output.ToString();
Msg.IsBodyHtml = true;
if (fuAttachment.HasFile)
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
Msg.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
if (RadioButton1.Checked)
{
sql.Open();
string s = "select * from address";
SqlCommand t = new SqlCommand(s, sql);
t.ExecuteNonQuery();
sql.Close();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT address FROM address1";
cmd.Connection = sql;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
sql.Open();
da.Fill(dt);
sql.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
else if (RadioButton2.Checked)
{
string connectionString = "";
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload1.SaveAs(fileLocation);
if (fileExtension == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT address FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dt);
con.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
}
else if (RadioButton3.Checked)
{
if (FileUpload2.HasFile)
{
string fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload2.SaveAs(fileLocation);
StreamReader sr = new StreamReader(fileLocation);
String line = sr.ReadToEnd();
string[] toAddressArray;
toAddressArray = line.Split(new char[] { ' ' });
foreach (string a in toAddressArray)
{
Msg.To.Add(a);
}
}
}
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);
}
}
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;
}