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()
   {
       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

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 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.
   
v2

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