Sakthi
Sakthi

Reputation: 45

Send e-mail asp.net C# Windows7 32 bit

I am trying to send an e-mail from asp.net in Windows7. After hours of googling I found that SMTP server is not available in Windows 7. I have tried the following piece of codes and landed up with one prime error "No connection could be made because the target machine actively refused it 74.125.25.109:587".

Code no:1

 private void sendmail()
    {
        string email = "[email protected]";
        string password = "mygmail_password";

        var loginInfo = new NetworkCredential(email, password);
        var msg = new System.Net.Mail.MailMessage();
        var smtpClient = new SmtpClient("smtp.gmail.com", 587);

        msg.From = new MailAddress(email);
        msg.To.Add(new MailAddress("[email protected]"));
        msg.Subject = "my mail subject";
        msg.Body = "my mail body";
        msg.IsBodyHtml = true;

        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = loginInfo;
        smtpClient.Send(msg);
    }

Code no:2

private void sendthemail()
    {
        System.Net.Mail.MailMessage mailObj = new System.Net.Mail.MailMessage(
         txtFrom.Text,txtEmail.Text,txtSubject.Text, txtBody.Text);
        SmtpClient SMTPServer = new SmtpClient("localhost");
        try
        {
            SMTPServer.Send(mailObj);

            SMTPServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;

            SMTPServer.PickupDirectoryLocation = "C:\\inetpub\\mailroot\\Pickup";

            SMTPServer.Host = "127.0.0.1"; // localhost

            SMTPServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            Label1.Text = "Mail sent";
        }
        catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }

My aspx looks like:

My IIS looks like this:

Stack trace: My questions are :

-Do I need a SMTP server to send across a mail through ASP.Net using gmail?

-Is there anything that I have to download?

-Is there anything I should add to my code?

-How do I detect the SMTP server that is running in my system?

I have downloaded a lot of Applications such as hMailServer,Smtp4dev,Windows SMTP server and don't know what to do with it.Please help!!

Upvotes: 0

Views: 774

Answers (2)

Ronnie Togger
Ronnie Togger

Reputation: 136

Have you explicitly enabled smtp on your gmail account. As far as I know this needs to be enabled before you can use it. I’d suggest you check the settings in gmail first because your code looks just fine.

I’d also try removing port number here. It’s not needed because you’re using EnableSSL property.

var smtpClient = new SmtpClient("smtp.gmail.com");

-Do I need a SMTP server to send across a mail through ASP.Net using gmail? – You do need a SMTP server but you don’t need to have it installed on your local machine.

-Is there anything that I have to download? – No.

-Is there anything I should add to my code? – Try changing the line above.

-How do I detect the SMTP server that is running in my system? – check the services running on your system and see if there is SMTP server there.

Upvotes: 1

Nazish Ali Rizvi
Nazish Ali Rizvi

Reputation: 185

No because you are using System.Net.Mail class and you are providing the gmail.smpt server in your code. No Download required You can check in your service( services.msc) smtp server services is running .

please send you error when you try to send email it will help to find out the real issue.

Upvotes: 1

Related Questions