C# Sending email from app

Posted on 2007-10-20
Last Modified: 2013-12-17
Hi there,

I need to send notification email from my C# windows application.  I am not familiar with SNMP server.  This is for a home project and I don't have SMTP or anything setup in my PC.  How would I set up SMTP server and then how can I send a email, say, to my hotmail account from the application.

Question by:ambuli
    LVL 13

    Expert Comment

    I use the following to send e-mail from a C# application.  I haven't done anything special to set up SMTP and it seems to work just fine.

    The method doesn't need to be static, it just happens to be static in this application.

    Do you need to send attachments, or is a simple e-mail sufficient?
    using System.Net.Mail;

          public static void SendMail(string server, string address,string title,string message) {
             SmtpClient client = new SmtpClient(server);

    Author Comment

    Hi thank you.

    When you call this function, what do you give for the server?  Is it just the ipaddress?


    Author Comment

    As I read from the web, it seems that I need a SMTP server.  I am not familiar with it and I am not sure if sending an email is allowed from my ISP.  My application needs to send this mail  once a month.  So, please let me know how to do this.


    Author Comment


    I did this:

    Going to Start->Run

    and typed telnet localhost 25

    220 ambuli Microsoft ESMTP MAIL Service, Version: 6.0.2600.2180 ready at  Sun, 21
     Oct 2007 02:25:49 -0400
    250 ambuli Hello []
    MAIL FROM:<me@localhost>
    250 2.1.0 me@localhost....Sender OK
    RCPT TO:<>
    550 5.7.1 Unable to relay for

    I am lost here.  I have IIS running in my PC(XP).  So, it seems like there is a SMTP server also running. Why would I get
    550 5.7.1 Unable to relay for"


    Author Comment

    I resolved the issue with 550 5.7.1 Unable to relay for and send a mail.  The message I got is something like, the mail is queued for delivery.   But, I haven't got the mail in gmail. was helpful in resolving the error.  

    LVL 12

    Accepted Solution

    If you have gmail account, you can easily use gmail smtp and send emails..

    Private Sub SendMail()
            Dim msg As New MailMessage
            msg.From = New MailAddress(txtSendFrom.Text, txtName.Text, System.Text.Encoding.UTF8)
            msg.Subject = txtSubject.Text
            msg.SubjectEncoding = System.Text.Encoding.UTF8
            msg.Body = txtMessage.Text
            msg.BodyEncoding = System.Text.Encoding.UTF8
            msg.IsBodyHtml = False
            msg.Priority = MailPriority.High

            '//Add the Creddentials
            Dim client As New SmtpClient()
            client.Credentials = New System.Net.NetworkCredential("", "yourpassport")
            client.Port = 587 '//or use 587            
            client.Host = ""
            client.EnableSsl = True
            'client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);

            Dim userstate As Object = msg

                '//you can also call client.Send(msg)
                'client.SendAsync(msg, userstate)
                MessageBox.Show("E-Mail sent...")
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Send Mail Error")
            End Try

        End Sub
    LVL 12

    Expert Comment

    sorry for VB.NET code, but i hope it is not hard to convert to C#
    LVL 13

    Expert Comment

    >>When you call this function, what do you give for the server?  Is it just the ipaddress?
    I'm on Comcast, so I use "".  Your ISP should also provide an SMTP server.  Look in your Outlook settings (if that's what you're using) at Tools | E-mail accounts | View or change existing e-mail accounts.  My Outlook shows my e-mail server is "" -- I send to "".

    I send to a normal email address and the message is just a string.

    This link is a pretty FAQ on System.Net.Mail.
    LVL 4

    Expert Comment

    Here is the code to send a mail ...
    private void button1_Click(object sender, EventArgs e)     {
           SmtpClient client = new SmtpClient("", 25);
            //client.Send("From", "To", "Subject", "BodyText");
            client.Send("", "", "TestMail", "Hello");
    } catch (Exception ex)   {  MessageBox.Show(ex.Message);   }
    LVL 4

    Expert Comment

    Configuring your SMTP Client:

    a) Open Microsoft Outlook and Express
    a) Open the Tools menu and select accounts
    b) Double click on your account name and open it
    c) Select the servers tab
    d) In the "Outgoing mail (SMTP)" field put the host or IP address of your SMTP server.

    Hope this helps you. Please let me know for further assistance.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    There was an incident about the POP3 issue for the double read receipts and delivery receipts in Exchange 2013.  There was huge research been done and found solution for the duplicate mails. Especially when the user gets  duplicate mails.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now