Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
public void sendMail()
   {
       try
       {

           var sc = new SmtpClient

           {

               Host = "smtp.zoho.com",

               Port = 465,

               EnableSsl = true,

               Credentials = new System.Net.NetworkCredential("admin@varshaenterprises.in", "-------")

           };

           var from = new MailAddress("admin@varshaenterprises.in", "HELLO EMAIL SYSTEM");

           var to = new MailAddress("bijay@varshaenterprises.in");

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

           m.IsBodyHtml = false;

           sc.Send(m);

       }

       catch (Exception ex)
       {
           ex.Message.ToString();


       }
   }
Posted
Comments
phil.o 21-Mar-15 23:41pm    
Please define "has not worked".
bigyan sahoo 21-Mar-15 23:57pm    
mail has not sent to bijay@varshaenterprises.in
bigyan sahoo 21-Mar-15 23:58pm    
I want to sent a mail to bijay@varshaenterprises.in 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

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 varshaentreprises.in 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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900