Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following code for sending mail .When i test in localhost it work properly but when this code use in web application. my registration form submitted successfully but the email does not send and not any error also. plz give me help.
C#
string Subject = "Registration...";
        String Body = "Candidate Name : " + txtfname.Text + " " + txtsname.Text + "\n" + "Description : You Are Now Register With Us" + "\n" + "Your User Name Is :" + txtuname.Text + "And Password is :" + txtpass.Text + "\n" + "Thank You...";
        //lbldetail.Text;
        MailMessage mail = new MailMessage();

        mail.To.Add(txtmail.Text);
        mail.From = new MailAddress("vedbhavan2011@gmail.com");
        mail.Subject = Subject;
        mail.Body = Body;


        mail.IsBodyHtml = true;
        mail.Priority = System.Net.Mail.MailPriority.High;
        SmtpClient client = new SmtpClient();
        client.Credentials = new System.Net.NetworkCredential("vedbhavan2011@gmail.com", "******");//password here
        client.Port = 587; // Gmail works on this port
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true; //Gmail works on Server Secured Layer
        try
        {
            client.Send(mail);
            lblmsg.Text = "Email Sent Successfully...";
        }
        catch
        {

        }
Posted
Updated 24-Aug-12 3:26am
v2
Comments
[no name] 24-Aug-12 9:37am    
You do not know that you do not have any errors since you are swallowing all of the exceptions.
Philip Stuyck 24-Aug-12 12:09pm    
yes, the system is telling him what is wrong but he chose to ignore it.
Print out the message of the exception that is passed to you, it has valueable information usually.

You missed one important property.

Just add the line below
C#
client.UseDefaultCredentials = false;

before
C#
client.Credentials = new System.Net.NetworkCredential("vedbhavan2011@gmail.com", 


So the code will look like,
C#
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("vedbhavan2011@gmail.com", 
....


Let me know if it solves your purpose.

cheers
 
Share this answer
 
v2
Comments
rajuvpatil 25-Aug-12 4:14am    
hi use this one also bt problem remain same...plz help me....
Thank you.
Sandip.Nascar 25-Aug-12 4:25am    
Can you trap the exception and let me know if you get any error?
try
{
client.Send(mail);
//lblmsg.Text = "Email Sent Successfully...";
}
catch(Exception ex)
{
string str_ex = ex.Message;
}
Thx
Sandip
You are not displaying the error that happens

C#
try
        {
            client.Send(mail);
            lblmsg.Text = "Email Sent Successfully...";
        }
        catch
        {

        }



do this
C#
try
        {
            client.Send(mail);
            lblmsg.Text = "Email Sent Successfully...";
        }
        catch(Exception ex)
        {
            string str_ex = ex.Message;
            lblmsg.Text = str_ex;
        }


I'm taking a guess here but I bet your problem is your firewall on your production server. It is probably not allowing communication through on port 587
 
Share this answer
 
In your server have you start the smtp ?
 
Share this answer
 
Comments
rajuvpatil 24-Aug-12 9:28am    
no plz tell me how to start the smtp on the server.
Kamalkant(kk) 24-Aug-12 9:36am    
go through google
Check out the spam folder. May be mails are going and stores in spam folder and users are searching in inbox so you are not getting.

Added from comment:

C#
MailMessage mail = new MailMessage();
        mail.To = toMail;
        mail.From = "support@xyz.com";
        mail.Subject = "Welcome to xyz!!";
        mail.Body = table;
        mail.BodyFormat = MailFormat.Html;
        mail.Priority = MailPriority.High;
        SmtpMail.SmtpServer = "localhost";
        SmtpMail.Send(mail);
 
Share this answer
 
v2
Comments
rajuvpatil 24-Aug-12 9:32am    
i check the spam folder also bt ther is no any mail.
Sunil Kumar Pandab 24-Aug-12 9:57am    
TRy this one. It may help

MailMessage mail = new MailMessage();
mail.To = toMail;
mail.From = "support@xyz.com";
mail.Subject = "Welcome to xyz!!";
mail.Body = table;
mail.BodyFormat = MailFormat.Html;
mail.Priority = MailPriority.High;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(mail);

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