6/17/2012
Seding Email using C#
public void SendMail(string strUserName, string strMailFrom, string strMailTo,string strMailCC, string strSubject,string strBody)
{
try
{
// splitting by the cama in the To mail address and CC address
char ch = ',';
string [] strArrayMailTo=strMailTo.Split(ch);//(new char {','});
string[] strArryMailCC = strMailCC.Split(ch);//(new char{','});
string strServer = ConfigurationManager.AppSettings["ServerIP"].ToString();
int port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"].ToString());
SmtpClient ObjSmtpClient = new SmtpClient(strServer, port);
MailMessage objMailMessage = new MailMessage();
MailAddress objFromMailAddress = new MailAddress(strMailFrom);
objMailMessage.From = objFromMailAddress;
//Removing Duplicate mail Id's in the To Address and sorts the address
string[] UniqstrArrayMailTo = RemoveDups(strArrayMailTo, true);
//Adding ToMail
foreach (string strrMailTo in UniqstrArrayMailTo)
{
objMailMessage.To.Add(strrMailTo);
}
//Removing Duplicate mail Id's in the CC Address and sorts the address
string[] UniqstrArryMailCC = RemoveDups(strArryMailCC, true);
//Adding CCMail
foreach (string strrMailCC in UniqstrArryMailCC)
{
objMailMessage.CC.Add(strrMailCC);
}
objMailMessage.Body = MailBody(strUserName,strBody);
objMailMessage.IsBodyHtml = true;
objMailMessage.Subject = strSubject;
ObjSmtpClient.Send(objMailMessage);
}
catch (Exception Ex)
{
//throw new Exception(Ex.Message.ToString());
}
}
private string MailBody(string strUserName,string strBody)
{
StringBuilder sb = new StringBuilder();
sb.Append("<HTML><table><body><tr><td>");
sb.Append("Dear Sir/Madam");
sb.AppendLine();
sb.Append("</td></tr>");
sb.Append("<BR><BR><BR><BR>");
sb.Append("<tr><td>");
sb.Append(strBody);
sb.Append("</td></tr>");
sb.Append("<BR><BR><BR><BR>");
sb.Append(strUserName);
sb.Append("<BR><BR><BR>");
sb.Append("</body></table></HTMl>");
return sb.ToString();
}
private string[] RemoveDups(string[] items, bool sort)
{
ArrayList noDups = new ArrayList();
for (int i = 0; i<items.Length; i++)
{
if (!noDups.Contains(items[i].Trim()))
{
noDups.Add(items[i].Trim());
}
}
if (sort) noDups.Sort(); //sorts list alphabetically
string[] uniqueItems = new String[noDups.Count];
noDups.CopyTo(uniqueItems);
return uniqueItems;
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment