Click here to Skip to main content
14,599,597 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear sir, I have written this code for send a mail while a new user register. But it has not worked. plz give mw the proper solution.

public void sendMail()

           var sc = new SmtpClient


               Host = "",

               Port = 465,

               EnableSsl = true,

               Credentials = new System.Net.NetworkCredential("", "-------")


           var from = new MailAddress("", "HELLO EMAIL SYSTEM");

           var to = new MailAddress("");

           var m = new MailMessage(from, to) { Subject = txtComplaint.Text, Body = txtName.Text + txtContactNo.Text + txtMailId.Text };

           m.IsBodyHtml = false;



       catch (Exception ex)

phil.o 21-Mar-15 23:41pm
Please define "has not worked".
bigyan sahoo 21-Mar-15 23:57pm
mail has not sent to
bigyan sahoo 21-Mar-15 23:58pm
I want to sent a mail to when a new user register.
Vishal Bhadouria 11-Jul-18 15:24pm
Your code is fine. just change port number to port=587. it will work.

1 solution

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

Solution 1

Well the causes can be numerous:
- you did not reach the smtp server
- you reached it but it refused the connection
- it accepted the connection but the final recipient does not exist, or the server does not accept relaying to the domain
- the recipient exist but the mail has been further filtered by the antispam engine

First, it is never a good idea to catch a general Exception. Better catch specific exception types which you know what to do with.
According to SmtpClient.Send Method (MailMessage)[^], the different types of exception that can be thrown are ArgumentNullException, InvalidOperationException, ObjectDisposedException, SmtpException and SmtpFailedRecipientsException.

As you did not state whether you get an exception or not, hard to say.

Second, try to do a telnet session to this server on port 465 with these credentials, and try to send the mail. Does it work? Which SMTP codes are returned by the server?

Third, watch for the logs on the destination server; it could give you a clue.

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