send an email through C#

MKItani
MKItani used Ask the Experts™
on
Hy every body,
What i want to do is send an email through C# can any body send me the instructions.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
hi
try this
simple and useful
using System.Net.Mail;
public class MailingUtilities
{
    public static string BCC_MAIL
    {
        get
        {
           // blind carbon copy mail
            return "adilet@magadesign.net";
        }
    }
    public static string FROM_MAIL
    {
        get
        {
           // Gmail doesn't allow to specify other email than SMTP_USR, some other providers make you todo that
            return "xxxx@gmail.com";
        }
    }
    public static string FROM_NAME
    {
        get
        {
            // get your sign
            return "MagaDesign Mailing";
        }
    }
    public static string SMTP_USR
    {
        get
        {
           // get your smtp user
            return "xxxx@gmail.com";
        }
    }
    public static string SMTP_PWD
    {
        get
        {
            // get your smtp password
            return "xxx";
        }
    }
    public static string SMTP_HOST
    {
        get
        {
            // get your smtp provider
            return "smtp.gmail.com";
        }
    }
    public static bool SMTP_SSL
    {
        get
        {
            string _str = "true"; // some providers need some not, GMail needs
            return _str != "false";
        }
    }
    public static int SMTP_PORT
    {
        get
        {
            // hear you can specify port that is used GMail uses 587
            return 587;
        }
    }
    public static bool autoSendMailTo(string Subject, string Body, string To, bool bcc)
    {
        try
        {
            string bodyHeader = "<Html><body>";
            string bodyFooter = "</body></html>";
            SmtpClient smtp = new SmtpClient();
            smtp.Port = SMTP_PORT;
            smtp.Host = SMTP_HOST;
            smtp.EnableSsl = SMTP_SSL;
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new System.Net.NetworkCredential(SMTP_USR, SMTP_PWD);
            MailMessage mail = new MailMessage();
            mail.To.Add(new MailAddress(To));
            if (BCC_MAIL != "" && bcc)
                mail.Bcc.Add(new MailAddress(BCC_MAIL));
            mail.From = new MailAddress(FROM_MAIL, FROM_NAME);
            mail.IsBodyHtml = true;
            string body = bodyHeader + Body + bodyFooter;
            mail.Subject = Subject;
            mail.Body = body;
            smtp.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool autoSendMailTo_attach(string Subject, string Body, string To, string[] attach)
    {
        try
        {
            string bodyHeader = "<Html><body>";
            string bodyFooter = "</body></html>";
            SmtpClient smtp = new SmtpClient();
            smtp.Port = SMTP_PORT;
            smtp.Host = SMTP_HOST;
            smtp.EnableSsl = SMTP_SSL;
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new System.Net.NetworkCredential(SMTP_USR, SMTP_PWD);
            MailMessage mail = new MailMessage();
            mail.To.Add(new MailAddress(To));
            if (BCC_MAIL != "")
                mail.Bcc.Add(new MailAddress(BCC_MAIL));
            mail.From = new MailAddress(FROM_MAIL, FROM_NAME);
            mail.IsBodyHtml = true;
            string body = bodyHeader + Body + bodyFooter;
            mail.Subject = Subject;
            mail.Body = body;
            for (int i = 0; i < attach.Length; i++)
            {
                mail.Attachments.Add(new Attachment(attach[i]));
            }
            smtp.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

Open in new window

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

a nice article on sending mail through asp.net 2.0 :-

http://www.4guysfromrolla.com/articles/072606-1.aspx
to use:
MailingUtilities.autoSendMailTo("hi", "hello world", "yourmail@gmail.com",false);
Top Expert 2011

Commented:
Todd GerbertIT Consultant
Top Expert 2010

Commented:
Did you know there are over 20,000 questions (and answers) already on Experts Exchange on how to send email from C#? That little search box in the top-right corner works miracles. ;)

Here are a few links that look like they have good answers, with links and examples (I did glance over them quickly to make sure the answer in the question wasn't nonsense, but I didn't read them all in depth):

http:Q_24901587.html
http:Q_23260537.html
http:Q_20322791.html
http:Q_24739892.html
http:Q_24739438.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial