System.Net.Mail is the namespace used to send email if you are using the 2.0 (or higher) .NET Framework.
Unlike System.Web.Mail, which was introduced in the 1.0 Framework, it is not built upon the CDO/CDOSYS libraries. Instead it is written from the ground up without any interop. Thus, it is not dependant upon other COM libraries. System.Net.Mail introduces brand new classes for creating and sending email.
Although some functionality has been removed, the new System.Net.Mail namespace is much more versatile than the older CDO dependant System.Web.Mail.
Example -------
using System; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Threading; using System.ComponentModel; namespace Utils.Mail { public class MailSender { public static string mailStatus = String.Empty; public static event EventHandler NotifyCaller; protected static void OnNotifyCaller() { if (NotifyCaller != null) NotifyCaller(mailStatus, EventArgs.Empty); }
public static bool SendMailMessage(string SMTPServer, string fromAddress, string fromName, string toAddress, string toName, string msgSubject, string msgBody) { try { SmtpClient client = new SmtpClient(SMTPServer); MailAddress from = new MailAddress(fromAddress, fromName); MailAddress to = new MailAddress(toAddress, toName); MailMessage message = new MailMessage(from, to); message.Subject = msgSubject; message.Body = msgBody; client.Send(message); } catch(System.Net.Mail.SmtpException ) { throw; } catch(Exception ) { throw; } return true; }
public static void SendMailMessageAsync(string SMTPServer, MailMessage message) { try { SmtpClient client = new SmtpClient(SMTPServer); client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); client.SendAsync(message, message.To.ToString()); } catch (System.Net.Mail.SmtpException) { throw; } catch (Exception) { throw; } }
public static bool SendMailMessage(string SMTPServer, MailMessage message) { try { SmtpClient client = new SmtpClient(SMTPServer); client.Send(message); } catch (System.Net.Mail.SmtpException) { throw; } catch (Exception) { throw; } return true; }
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) { String token = (string)e.UserState; if (e.Cancelled) { mailStatus = token + ” Send canceled.”; } if (e.Error != null) { mailStatus=”Error on “+ token + “: ” +e.Error.ToString(); } else { mailStatus = token + ” mail sent.”; } OnNotifyCaller(); } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|