how to send a e-mail from a c# code

Last post 06-01-2009 8:17 AM by tamilcodes. 6 replies.

Sort Posts:

  • how to send a e-mail from a c# code

    06-01-2009, 4:22 AM
    • Member
      95 point Member
    • harish_ravi
    • Member since 08-13-2008, 3:00 AM
    • Hyderabad(The Kohinoor Of India)
    • Posts 69

    can some one give me code how to send a e-mail from a c# code
    i am working with a company using microsoft outlook for my mails.
    i want to send mail only to 1 person , also please help me in identifying "smtp hostname"

    Raavi
    Software Developer
    India


    Please remember to click “Mark as Answer” on the post(s) which helps you !
  • Re: how to send a e-mail from a c# code

    06-01-2009, 5:39 AM
    Answer
    • All-Star
      29,705 point All-Star
    • HeartattacK
    • Member since 01-08-2007, 5:53 PM
    • Dhaka, Bangladesh
    • Posts 3,234
    • Moderator
      TrustedFriends-MVPs
  • Re: how to send a e-mail from a c# code

    06-01-2009, 5:49 AM
  • Re: how to send a e-mail from a c# code

    06-01-2009, 6:17 AM
    • Contributor
      5,942 point Contributor
    • sreejukg
    • Member since 10-31-2004, 7:33 AM
    • Bahrain
    • Posts 1,002
    The real voyage of discovery consists not in making new landscapes, but in having new eyes
  • Re: how to send a e-mail from a c# code

    06-01-2009, 6:23 AM
    • Participant
      1,635 point Participant
    • sangam100
    • Member since 07-22-2008, 10:44 AM
    • Kathmandu,Nepal
    • Posts 313

    Hi harish_ravi ,

    Visiting all above listed links you must have realised that sending email in asp.net is quite fun and useful too. In fact, a lot of customizations exist depending on your requirement. My suggestion is to do the basic email sending first. After that you can play with the classes and objects to perform more actions. Let me show you the basic email sending using c#.

    1    using System.Net.Mail;
    2   
    3    public void SendBasicMail()
    4        {
    5            //create new mailmessage object
    6            MailMessage message = new MailMessage(new MailAddress("sangam@test.com"), new MailAddress("anup@best.com"));
    7            message.Body = "Hi. This is test message.";
    8            message.Subject = "Test Message!";
    9            message.IsBodyHtml = false;
    10  
    11           //create smtp client and send mail
    12           SmtpClient client = new SmtpClient("mail.mysmtpserver.com", 25);
    13           try
    14           {
    15               client.Send(message);
    16               lblError.Text = "Email sent successfully!";
    17               lblError.ForeColor = Color.Green;
    18           }
    19           catch (Exception ex)
    20           {
    21               lblError.Text = "Email could not be sent. " + ex.Message;
    22               lblError.ForeColor = Color.Red;
    23           }
    24       }

    Hope this helps. Please feel free to ask if any problem persists or if further help is required. Thank you,

    Lets always remember to mark the post that helps us as "Mark as Answer".
    Very useful visual studio shortcuts and more tips and tricks
    5 Different ways to open new window in asp.net
  • Re: how to send a e-mail from a c# code

    06-01-2009, 7:02 AM
    • Star
      10,560 point Star
    • getchinna_sv
    • Member since 09-10-2008, 8:29 PM
    • Hyderabad
    • Posts 1,783

    use the following method.....  this will use gmail smtp details to send an email.....

    protected void sendMail()
        {
            string from = "me@gmail.com"; //Replace this with your own correct Gmail Address
    
            string to = "you@gmail.com"; //Replace this with the Email Address to whom you want to send the mail
    
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress(from, "One Ghost", System.Text.Encoding.UTF8);
            mail.Subject = "This is a test mail";
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = "This is Email Body Text";
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
    
            SmtpClient client = new SmtpClient();
            //Add the Creddentials- use your own email id and password
    
            client.Credentials = new System.Net.NetworkCredential(from, "Password");
    
            client.Port = 587; // Gmail works on this port
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            try
            {
                client.Send(mail);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty;
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }
                HttpContext.Current.Response.Write(errorMessage);
            } // end try 
    }
            client.Credentials = new System.Net.NetworkCredential(from, "Password"); in this statement... write your gmail password.... from address should be a gmail address.... 
    Chinna_sv...
  • Re: how to send a e-mail from a c# code

    06-01-2009, 8:17 AM
    • Participant
      1,874 point Participant
    • tamilcodes
    • Member since 04-30-2009, 1:28 PM
    • Posts 400
Page 1 of 1 (7 items)