3

i am using using System.Net.Mail;

and following code to send mail

MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient();

        // Set the sender's address
        message.From = new MailAddress("fromAddress");

     // Allow multiple "To" addresses to be separated by a semi-colon
        if (toAddress.Trim().Length > 0)
        {
            foreach (string addr in toAddress.Split(';'))
            {
                message.To.Add(new MailAddress(addr));
            }
        }
      // Allow multiple "Cc" addresses to be separated by a semi-colon
        if (ccAddress.Trim().Length > 0)
        {
            foreach (string addr in ccAddress.Split(';'))
            {
                message.CC.Add(new MailAddress(addr));
            }
        }
        // Set the subject and message body text
        message.Subject = subject;
        message.Body = messageBody;

        // Set the SMTP server to be used to send the message
        client.Host = "YourMailServer";

        // Send the e-mail message
        client.Send(message);

for Host i am providing client.Host = "localhost";

for this its falling with error

No connection could be made because the target machine actively refused it some_ip_address_here

and when i use client.Host = "smtp.gmail.com";

i get following error

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

i am not able to send mail through localhost. Please help me out, i am new to c# please correct me in code where i am going wrong..?

svick
  • 236,525
  • 50
  • 385
  • 514
Shri Harry
  • 103
  • 2
  • 2
  • 6
  • 2
    You need an SMTP server that is willing to send mail for you. (and a login, and the correct port & SSL settings) – SLaks Mar 17 '13 at 16:10
  • how can i send mails from my machine, i am not deploying code. I am working on localhost.When i use host "smtp.gmail.com"; its faling..i don't know how to correct port and SSL settings. – Shri Harry Mar 17 '13 at 16:12

5 Answers5

5

Here is some code that works for sending mail via gmail (code from somewhere here on stackoverflow. It is similar to the code here: Gmail: How to send an email programmatically):

using (var client = new SmtpClient("smtp.gmail.com", 587)
{
  Credentials = new NetworkCredential("yourmail@gmail.com", "yourpassword"),
  EnableSsl = true
})
{
  client.Send("frommail@gmail.com", "tomail@gmail.com", "subject", message);
}
Community
  • 1
  • 1
tomsv
  • 7,207
  • 6
  • 55
  • 88
  • If you found the code somewhere, could you link to the original? – svick Mar 17 '13 at 17:13
  • Yes. The problem is that I found it somewhere here some time ago and then have been using it for a while and now I do not remember the exact source. – tomsv Mar 17 '13 at 17:30
2

For sending mail from client.Host = "localhost" you need set up local SMTP server.

For sending mail via Google (or via any other SMTP server, including your own local SMTP) you must set username, password, ssl settings - all as required by SMTP server chosen, and you need to read their help for this.

For example Google says that you need SSL, port 465 or 587, server smtp.gmail.com and your username and password.

You can assign all this values in .config file.

<system.net>
  <mailSettings>
    <smtp>
      <network host="smtp.gmail.com" enableSsl="true" port="587" userName="yourname@gmail.com" password="password" />
    </smtp>
  </mailSettings>
</system.net>

Or set to SmtpClient in code before every use:

client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSSL = true;
client.Credentials = new NetworkCredential("yourname@gmail.com", "password");
svick
  • 236,525
  • 50
  • 385
  • 514
Dmitry
  • 16,110
  • 4
  • 61
  • 73
1

Place this code inside a <configuration> </configuration> in web.config file

<system.net>
<mailSettings>
  <smtp>
    <network host="smtp.gmail.com" enableSsl="true" port="587" userName="youremail@gmail.com" password="yourpassword" />
  </smtp>
</mailSettings>
</system.net>

then backend code

MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("email@gmail.com");
message.To.Add(new MailAddress(TextBoxEadd.Text));
message.CC.Add(new MailAddress("email@gmail.com"));
message.Subject = "New User Registration ! ";
message.Body =  "HELLO";
sr.Close();
SmtpClient client = new SmtpClient();
client.Send(message);

I hope this code help you! :)

svick
  • 236,525
  • 50
  • 385
  • 514
Carlo Adap
  • 157
  • 1
  • 2
  • 9
  • This worked for me! but can you tell me why you need to define message.From = new MailAddress("email@gmail.com") when you specify your credentials in the web config ie userName="youremail@gmail.com" – Harry Jan 09 '15 at 21:17
0

use this Line..Dont Use Port And HostName

LocalClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

0

I just wanted to add that Gmail now requires App Password in order to use it from other applications. Check this link. I had to find it the hard way. After I created an App Password and then I changed the NetworkCredentials to use it to send emails.

ilke444
  • 2,641
  • 1
  • 17
  • 31
Xequtor
  • 125
  • 1
  • 12