C# Sending email from app

Posted on 2007-10-20
Medium Priority
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
  • 4
  • 2
  • 2
  • +1
LVL 13

Expert Comment

ID: 20116585
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

ID: 20116703
Hi thank you.

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


Author Comment

ID: 20117046
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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 20117081

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:<ambuli@gmail.com>
550 5.7.1 Unable to relay for ambuli@gmail.com

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 ambuli@gmail.com"


Author Comment

ID: 20117115
I resolved the issue with 550 5.7.1 Unable to relay for ambuli@gmail.com 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.

http://blogs.technet.com/jhoward/archive/2005/10/11/412328.aspx was helpful in resolving the error.  

LVL 12

Accepted Solution

Praveen Kumar earned 2000 total points
ID: 20117122
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("youraccount@gmail.com", "yourpassport")
        client.Port = 587 '//or use 587            
        client.Host = "smtp.gmail.com"
        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

by:Praveen Kumar
ID: 20117129
sorry for VB.NET code, but i hope it is not hard to convert to C#
LVL 13

Expert Comment

ID: 20117989
>>When you call this function, what do you give for the server?  Is it just the ipaddress?
I'm on Comcast, so I use "smtp.comcast.net".  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 "mail.comcast.net" -- I send to "smtp.comcast.net".

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

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

Expert Comment

ID: 20145820
Here is the code to send a mail ...
private void button1_Click(object sender, EventArgs e)     {
       SmtpClient client = new SmtpClient("smtp.yourdomainname.com", 25);
        //client.Send("From", "To", "Subject", "BodyText");
        client.Send("aaa@v.com", "ccc@dfsd.com", "TestMail", "Hello");
} catch (Exception ex)   {  MessageBox.Show(ex.Message);   }

Expert Comment

ID: 20146184
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Pop culture is prime bait for hackers seeking to infect user’s computers and mobile devices with malicious malware. Hackers know exactly what the latest trends are online and know how to use them to their advantage.
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

580 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