Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can send mail from any mail server to any mail server by using c#3.5 windows application
Posted
Comments
Nijel Sabra 22-Mar-13 7:13am    
public bool SendEmail(string pTo, List<string> ccList, string pSubject, string pBody, string pAttachmentPath, string name)
{
try
{
string mailFrom = System.Web.Configuration.WebConfigurationManager.AppSettings["Email"];
string mailPassword = System.Web.Configuration.WebConfigurationManager.AppSettings["Password"];
string smptpServer = System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpServer"];
string smptpServerPort = System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpServerPort"];
NetworkCredential cred = new NetworkCredential(mailFrom, mailPassword, smptpServer);
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress(mailFrom, "hai");
myMail.To.Add(new MailAddress(pTo, name));
myMail.IsBodyHtml = true;
myMail.Subject = pSubject;
myMail.Body = pBody;
myMail.BodyEncoding = System.Text.Encoding.UTF8;
myMail.Priority = MailPriority.High;
if (ccList != null)
{
if (ccList.Count > 0)
{
string ccEmailIdList = "";
foreach (string ccEmailId in ccList)
{
if (ccEmailIdList != "")
{
ccEmailIdList += ";";
}
ccEmailIdList += ccEmailId;
}
myMail.CC.Add(ccEmailIdList);
}
}
if (pAttachmentPath.Trim() != "")
{

myMail.Attachments.Add(new Attachment(pAttachmentPath));
}
SmtpClient client = new SmtpClient(smptpServer, int.Parse(smptpServerPort));
client.Credentials = cred;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(myMail);
return true;
}
catch (Exception ex)
{
_logger.Error("Inside MailSender::SendEmail" + ex.Message);
throw;
}
}

Use your appropriate variables and apply this,
Have a nice day coder.....

1 solution

See[^] what a simple search can find for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900