Thursday, August 29, 2013

Regular Expression to strip HTML tags from String in C#

Regex.Replace(htmlString, @"<[^>]*>", String.Empty)

Send Mail using Gmail Host in asp.net

/* Code to Send Mail using Gmail Host Server Settings in asp.net   */

                    var fromAddress = new MailAddress("FromAddress@abc.com", "Display Name");
                    var toAddress = new MailAddress("ToAddress@abc.com", "Display Name");

                    var smtp = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(fromAddress.Address, "GmailPassword")
                    };

                    var message = new MailMessage(fromAddress, toAddress)
                    {
                        Subject = "MailSubject",
                        Body =  "Mail Body",
                        IsBodyHtml = true
                    };

                    smtp.Send(message);