Click here to Skip to main content
14,664,041 members
Rate this:
Please Sign up or sign in to vote.
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()
        {
            InitializeComponent();
            
        }

        //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("smtp.gmail.com",587);
           

        private void btnEmail_Click(object sender, EventArgs e)
        {

            try
            {
                //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.To.Add(receiveraddress);
                mail.Subject = emailSubject;
                mail.Body = emailMessageText;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxx");
                client.EnableSsl = true;
                client.Send(mail);
                System.Windows.Forms.MessageBox.Show("Email Sent Successfully");
               


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

        }
    }
}
Posted
Updated 20-Apr-11 3:04am
v2
Comments
Hari Gillala 20-Apr-11 9:03am
   
The Error Message: "No connection could be made because the target machine actively refused it 209.85.143.109:587"
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?
Wannes Geysen 20-Apr-11 9:24am
   
I just copy-pasted your code and ran it with my own credentials and it worked like a charm. When I entered a wrong username/password i got the following error:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

Maybe you have exceeded the number of wrong login attempts. Can you still login at your GMail account using a webbrowser?

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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)
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100