I am trying to write a smiple email web program. Whenever I try to get an out going email I keep getting the general message that the computer is denying me access.

"No connection could be made because the target machine actively refused it " I have tried this at different locations.

String to, body, from, subject;

            to = TextBoxTo.Text;
            from = TextBoxFrom.Text;
            subject = TextBoxSubject.Text;
            body = TextBoxBody.Text;



            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(new MailAddress(to));
            mailMessage.From = new MailAddress(from);
            mailMessage.Subject = subject;
            mailMessage.Body = body;


            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Send(mailMessage);

Web.config

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

Recommended Answers

All 3 Replies

Are you sure that the web.config settings are applied automatically?

It is also possible that there is a firewall between your server and gmail blocking access.

You may want to try replacing lines 17-18 with:

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
       ("YourUserName@gmail.com","YourGmailPassword");
smtp.EnableSsl = true;
smtp.Send(mail);

I have done it using smtp.Credential, system.net.credentials, using Appsetting in WEb.config. But at school or my house I get the error the receiving computer is blocked

//smtpClient.Port = 587;
            //smtpClient.Host = "smtp.gmail.com";
            //System.Net.NetworkCredential NC = new NetworkCredential("bob@gmail.com", "");
            //smtpClient.Host = ConfigurationManager.AppSettings["SMTP"];
            //smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["from"], ConfigurationManager.AppSettings["pwd"]);
            //smtpClient.EnableSsl = true;
            //smtpClient.UseDefaultCredentials = false;
            //smtpClient.Credentials = NC;
            //smtpClient.Credentials = new NetworkCredential("bob@gmail.com", "");



<!--<appSettings>
    <add key="smtp" value="smtp.gmail.com"/>
    <add key="to" value="bob@gmail.com"/>
    <add key="from" value="bob@gmail.com"/>
    <add key="pwd" value=""/>
    <add key="port" value="587"/>
  </appSettings>-->
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.