0
Sending Mail functionality C#.Net
Posted by Rajendra Prasad Panchati
on
Monday, February 15, 2010
in
C#.Net
Required Namespaces : 1)using System.Net; 2)using System.Net.Mail;
Case 1: Mail send without Bcc, CC and without attachment
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(From Address, From Name);
objMailMessage.To.Add(To Address,To Name);
objMailMessage.Subject = Mail subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.Body = Mail Body;
SmtpClient objSmtpClient = new SmtpClient(Your SMTP Client);
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(Your SMTP User,Your SMTP Password);
objSmtpClient.Send(objMailMessage);
Case 2: Mail send with Bcc, CC and without attachment
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(From Address, From Name);
objMailMessage.To.Add(To Address,To Name);
objMailMessage.Subject = Mail subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.Body = Mail Body;
objMailMessage.Bcc.Add(Bcc Mail Id);//If Multiple Ids, ids should be devided by , (comma)
objMailMessage.CC.Add(CC Mail Id);//If Multiple Ids, ids should be devided by , (comma)
SmtpClient objSmtpClient = new SmtpClient(Your SMTP Client);
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(Your SMTP User,Your SMTP Password);
objSmtpClient.Send(objMailMessage);
Case 3: Mail send without Bcc, CC and with attachment
You may need using System.IO; namespace also for this type of mail sending if already not included.
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(From Address, From Name);
objMailMessage.To.Add(To Address,To Name);
objMailMessage.Subject = Mail subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.Body = Mail Body;
objMailMessage.Attachment.Add(new Attachment(Your attachment Path));//You can attach multiple attachments
SmtpClient objSmtpClient = new SmtpClient(Your SMTP Client);
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(Your SMTP User,Your SMTP Password);
objSmtpClient.Send(objMailMessage);
Case 1: Mail send without Bcc, CC and without attachment
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(From Address, From Name);
objMailMessage.To.Add(To Address,To Name);
objMailMessage.Subject = Mail subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.Body = Mail Body;
SmtpClient objSmtpClient = new SmtpClient(Your SMTP Client);
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(Your SMTP User,Your SMTP Password);
objSmtpClient.Send(objMailMessage);
Case 2: Mail send with Bcc, CC and without attachment
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(From Address, From Name);
objMailMessage.To.Add(To Address,To Name);
objMailMessage.Subject = Mail subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.Body = Mail Body;
objMailMessage.Bcc.Add(Bcc Mail Id);//If Multiple Ids, ids should be devided by , (comma)
objMailMessage.CC.Add(CC Mail Id);//If Multiple Ids, ids should be devided by , (comma)
SmtpClient objSmtpClient = new SmtpClient(Your SMTP Client);
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(Your SMTP User,Your SMTP Password);
objSmtpClient.Send(objMailMessage);
Case 3: Mail send without Bcc, CC and with attachment
You may need using System.IO; namespace also for this type of mail sending if already not included.
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(From Address, From Name);
objMailMessage.To.Add(To Address,To Name);
objMailMessage.Subject = Mail subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.Body = Mail Body;
objMailMessage.Attachment.Add(new Attachment(Your attachment Path));//You can attach multiple attachments
SmtpClient objSmtpClient = new SmtpClient(Your SMTP Client);
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(Your SMTP User,Your SMTP Password);
objSmtpClient.Send(objMailMessage);
More
Less