我有一个邮件收发器,我想在其中将邮件发送给多个收件人。收件人可以来自数据库或excel工作表。这是代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
GetAllRecipient();
Msg = null;
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
public void SendHTMLMail()
{
StreamReader reader = new StreamReader(Server.MapPath("~/one.html"));
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();
output = readFile;
Msg.Body = output.ToString();
Msg.IsBodyHtml = true;
int i = 0;
string username = Server.UrlEncode(this.txtUsername.Text);
output = regx.Replace(output, new MatchEvaluator((match) =>
{
var url = Uri.EscapeDataString(match.Value.ToString());
url = url.Replace("%3F", "&").Replace("%3D", "=");
return $"http://localhost4654/two?sender={username}&link={url}&mailer_id={i}";
}));
Msg.From = new MailAddress(txtUsername.Text);
Msg.Subject = null;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
smtp.EnableSsl = true;
}
public void GetAllRecipient()
{
if (RadioButton1.Checked)
{
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();
for (int a = 0; a < dt.Rows.Count; a++)
{
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
SendHTMLMail();
}
}
}
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();
for (int a = 0; a < dt.Rows.Count; a++)
{
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
SendHTMLMail();
smtp.Send(Msg);
}
}
}
}
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页面中,有一些链接。当收件人单击任何链接时,我将在“ rep.text”文本框中检索其邮件ID。因此,即使单击链接的人是第4位或第5位,我也会从收件人列表中获得姓氏。如果我使用密件抄送,也会发生同样的情况。
答案 0 :(得分:1)
使用
Msg.Bcc.Add(sample@sample.com);
代替
Msg.To.Add(sample@sample.com);
答案 1 :(得分:0)
LinkedHashMap<String, List<List<String>>> hashMap_searchresult = new LinkedHashMap<>();
List<List<String>> list = new ArrayList<>();
list.add(new ArrayList<String>(Arrays.asList("a", "b", "c", "d")));
list.add(new ArrayList<String>(Arrays.asList("e", "f", "g", "h")));
hashMap_searchresult.put("a1", list);
for (Map.Entry<String, List<List<String>>> entry : hashMap_searchresult.entrySet()) {
String key = entry.getKey();
List<List<String>> value2222 = entry.getValue();
for (List<String> lst : value2222) {
String[] arr = new String[list.size()];
arr = lst.toArray(arr);
System.out.println("checkstatus" + " key " + key + " value : " + Arrays.toString(arr));
}
}
答案 2 :(得分:0)
我认为我为您编写代码不会帮助您学习。同样,您遇到的问题似乎与编程语言无关,而与程序达到目标所需的逻辑步骤有关。
在开始编写代码之前,最好写下这些步骤。
将这个任务分解成小段代码会更好。
从我的代码中可以看出,用户将其详细信息填写在表格上,以便向收件人列表发送电子邮件。
我将步骤分为不同的方法,希望对您有所帮助。填写每种方法所需要做的事情,仅此而已。在继续进行下一步之前,请确保每个步骤均有效。我认为您拥有执行此操作的大部分代码,只需要对其进行重组即可。
protected void btnSubmit_Click(object sender, EventArgs e)
{
string user = GetUserDetails();
List<string> recipients = GetAllRecipient();
string html = GetHtmlTemplate();
foreach (var recipient in recipients)
{
html = html.Replace("<<SOME TAG>>", recipient);
SendEmail(recipient, html);
}
}
public string GetUserDetails()
{
return txtUsername.Text;
}
public List<string> GetAllRecipient()
{
//TODO: Get all your recipient from database or excel
//e,g,
return new List<string>() {"someemail@address.com", "anotheremail@address.com"};
}
public void SendEmail(string recipient, string html)
{
//TODO: Send the email
}
我可以确定的步骤是: 用户-点击按钮