Click here to Skip to main content
15,563,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to send email using the following piece of code in Winforms app.
Username and Password are correct and real- but have masked in the code.

I don't know wheather my local machine has the capacity to send emails or not?

But please advise me , What I need to look at my machine Configuration(in IIS)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;

namespace EmailFunctionlaity
    public partial class Email : Form
        public Email()

        //All Variable Declarations
        String senderAddress = string.Empty;
        String receiveraddress = string.Empty;
        String emailSubject = string.Empty;
        String emailMessageText = string.Empty;
        MailMessage mail = new MailMessage();
        SmtpClient client = new SmtpClient("",587);

        private void btnEmail_Click(object sender, EventArgs e)

                //Get the Values of Controls

                senderAddress = txtSenderEmail.Text;
                receiveraddress = txtReceiverEmail.Text;
                emailSubject = txtSubject.Text;
                emailMessageText = txtMessageBody.Text;

                //Pass the Values 
                mail.From = new MailAddress(senderAddress, "xxxxxxxxxx", System.Text.Encoding.UTF8);
                mail.Subject = emailSubject;
                mail.Body = emailMessageText;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential("", "xxxxxx");
                client.EnableSsl = true;
                System.Windows.Forms.MessageBox.Show("Email Sent Successfully");

            catch (Exception mailException)
                System.Windows.Forms.MessageBox.Show("Message Not Sent ,Error:  " + mailException.Message);

Updated 20-Apr-11 4:04am
Hari Gillala 20-Apr-11 9:03am    
The Error Message: "No connection could be made because the target machine actively refused it"
Wannes Geysen 20-Apr-11 9:08am    
Does Gmail allow you to use their SMTP server by third party applications?
Groulien 20-Apr-11 9:20am    
SMTP can't detect what app is sending, as long as you supply the proper credentials you should be fine.
BobJanova 20-Apr-11 9:38am    
This error message means that you didn't manage to connect to the server (though the DNS lookup succeeded so you are online). Check your firewall settings, you will almost certainly have to unblock your application to let it use 587 as outgoing SMTP calls are something that are usually not allowed.
Hari Gillala 20-Apr-11 9:16am    
Hello Wannes, I seriously don't know, I don't know how to test it? Could you tell me how can I check wheather gmail allow me to use thier SMTP server by third Party applications?

This tip should help you out: Sending an Email in C# with or without attachments: generic routine.[^]

Gmail SMTP can be used by thrid party application if correct credentials are used.
Share this answer
Simplest way to check if you have an SMTP server running is to open a command prompt and do this:

telnet localhost 25

If you get an error, then you don't have one running (not on the default port anyway)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900