Monday, January 30, 2012

Best Extension Methods: Send Email

If your application need functionality to send email at any time may be send a value of string or may be some exception then this extension method will help you.


    /// 
    /// Send an email using the supplied string.
    /// 
    /// String that will be used i the body of the email.
    /// Subject of the email.
    /// The email address from which the message was sent.
    /// The receiver of the email. 
    /// The server from which the email will be sent.  
    /// A boolean value indicating the success of the email send.
    public static bool Email(this string body, string subject, string sender, string recipient, string server)
    {
        try
        {
            // To
            //MailMessage mailMsg = new MailMessage();
            //mailMsg.To.Add(recipient);

            //// From
            //MailAddress mailAddress = new MailAddress(sender);
            //mailMsg.From = mailAddress;

            //// Subject and Body
            //mailMsg.Subject = subject;
            //mailMsg.Body = body;

            //// Init SmtpClient and send
            //SmtpClient smtpClient = new SmtpClient(server);
            //System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
            //smtpClient.Credentials = credentials;

            //smtpClient.Send(mailMsg);
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates