Example of asp. net sending emails

  • 2020-12-10 00:40:41
  • OfStack


protected void SendMail()
{
try
{
string CreaterName = "";
string examiner = "";
List<string> mailList = GetMailList(ref CreaterName, ref examiner);
MailEntity me = new MailEntity();
MailEntity me_1 = new MailEntity();
me.AddresserMail = ConfigurationManager.AppSettings["AddresserMail"].ToString();
me.AddresserName = ConfigurationManager.AppSettings["AddresserName"].ToString();
me.AddresserPwd = ConfigurationManager.AppSettings["AddresserPwd"].ToString();
me_1.AddresserMail = ConfigurationManager.AppSettings["AddresserMail_1"].ToString();
me_1.AddresserName = ConfigurationManager.AppSettings["AddresserName_1"].ToString();
me_1.AddresserPwd = ConfigurationManager.AppSettings["AddresserPwd_1"].ToString();
string strPostfix = ConfigurationManager.AppSettings["Postfix"].ToString();
string isSend = ConfigurationManager.AppSettings["isSend"].ToString();
StringBuilder ss = new StringBuilder();
if (isSend == "1")
{
if (mailList.Count > 0)
{
foreach (string mail in mailList)
{
int count = mail.IndexOf("@");
string postfix = mail.Substring(count + 1);
string[] pf = strPostfix.Split(new char[] { '|' });
int pCount = 0;
foreach (string p in pf)
{
if (postfix == p)
{
pCount++;
break;
}
}
if (pCount > 0)
{
me.AddresseeMail = mail.ToString();
}
else
{
me_1.AddresseeMail = mail.ToString();
}
string Body = "";
try
{
using (StreamReader sr = new StreamReader(HttpRuntime.BinDirectory + "/../tpl/tpl_Debt.htm", System.Text.Encoding.Default))
{
Body = sr.ReadToEnd();
}
}
catch (Exception ex)
{
lbMessage.Text = "***  Error reading template (reading template failed)!" + ex.Message;
lbMessage.ForeColor = System.Drawing.Color.Red;
return;
}
// --------------------------------------------------------------------------
me.ClientHost = ConfigurationManager.AppSettings["clientHost"].ToString();
me.MailEncoding = System.Text.Encoding.GetEncoding("GB2312");
me.MailPriority = System.Net.Mail.MailPriority.Normal;
me.IsBodyHtml = true;
me.Subject = "[" + CreaterName + "] A debit note created ";
//--------------------------------------------------------------------------
me_1.ClientHost = ConfigurationManager.AppSettings["clientHost_1"].ToString();
me_1.MailEncoding = System.Text.Encoding.GetEncoding("GB2312");
me_1.MailPriority = System.Net.Mail.MailPriority.Normal;
me_1.IsBodyHtml = true;
me_1.Subject = "[" + CreaterName + "] A debit note created ";
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_ExpenseName", CreaterName);// The borrower 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Type", ddlDebtType_Add.SelectedItem.Text);// Borrowing type 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Money", txbMoney_Add.Text);// The amount of 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Mender", txtMender.Text);// The reviewer 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Time", tbxDebtTime_Add.Text);// Borrowing time 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Advance", tbAdvance.Text);// generation / Advances the unit 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_DebtPathWay", ddlDebtPathWay.SelectedItem.Text);// borrowing 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Remark", txbMemo_Add.Text);// Borrowing reason 
// Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Mend", "");// Audit opinion 
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_Memo", "");// note 
string url = HttpContext.Current.Request.Url.AbsoluteUri;
url = url.Replace("http://", "");
int index = url.IndexOf("/");
url = "http://" + url.Substring(0, index + 1);
Body = Body.Replace("DTTPL_COMM_MODULE_NAME_LINK", url + "Affair/SelectWorkEvent.aspx?typeid=249");

me.Body = Body;
me_1.Body = Body;
if (me.AddresseeMail == null && me.CopyToAddresseeMail == null && me_1.AddresseeMail == null && me_1.CopyToAddresseeMail == null)
{
lbMessage.Text = "***  The debit note was saved successfully !  But those responsible / The following person has no email address in the database, and the email has not been sent! ";
lbMessage.ForeColor = System.Drawing.Color.Red;
return;
}
if (me.AddresseeMail != null || me.CopyToAddresseeMail != null)
{
bool temp = false;
for (int y = 0; y < 3; y++)
{
try
{
if (MailServer.SendMail(me))
{
temp = true;
break;
}
}
catch (Exception ex)
{
continue;
}
}
if (!temp)
{
if (me.AddresseeMail != null)
{
ss.Append(me.AddresseeMail.Trim());
}
if (me.CopyToAddresseeMail != null && me.CopyToAddresseeMail.Trim() != "")
{
ss.Append(",");
ss.Append(me.CopyToAddresseeMail.Trim());
}
}
me.AddresseeMail = null;
}
if (me_1.AddresseeMail != null || me_1.CopyToAddresseeMail != null)
{
bool temp = false;
for (int y = 0; y < 3; y++)
{
try
{
if (MailServer.SendMail(me_1))
{
temp = true;
break;
}
}
catch (Exception ex)
{
continue;
}
}
if (!temp)
{
if (ss.ToString().Trim() != "")
{
ss.Append(",");
if (me_1.AddresseeMail != null)
ss.Append(me_1.AddresseeMail.Trim());
if (me_1.CopyToAddresseeMail != null && me_1.CopyToAddresseeMail.Trim() != "")
{
ss.Append(",");
ss.Append(me_1.CopyToAddresseeMail.Trim());
}
}
else
{
if (me_1.AddresseeMail != null)
ss.Append(me_1.AddresseeMail.Trim());
if (me_1.CopyToAddresseeMail != null && me_1.CopyToAddresseeMail.Trim() != "")
{
ss.Append(",");
ss.Append(me_1.CopyToAddresseeMail.Trim());
}
}
}
me_1.AddresseeMail = null;
}
}
}
}
}
catch (Exception ex)
{
lbMessage.Text = "***  Send failure (the email is not sent)!" + ex.ToString();
lbMessage.ForeColor = System.Drawing.Color.Red;
}
}


Related articles: