Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have problem while sending a mail using asp.net C#. got an error like failure sending mail.

i was tried port numbers of 25 and 587 both.
This is my code

C#
public bool sendMail(string mailID, string srvMailID, string srvMailPassword, string dispName, string mailSubject, string smtpHost, string mailDescription, string port)
    {
        bool sent = false;

        try
        {
            string frmAddress = srvMailID.ToString();
            string toAddress = mailID.ToString();


            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

            mail.To.Add(toAddress);

            mail.From = new System.Net.Mail.MailAddress(srvMailID, dispName.ToString(), System.Text.Encoding.UTF8);
            mail.Subject = mailSubject.ToString();
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = mailDescription.ToString();
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = System.Net.Mail.MailPriority.High;

            System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();

            Client.Credentials = new System.Net.NetworkCredential(frmAddress.ToString(), srvMailPassword.ToString());
            Client.Port = Convert.ToInt32(port);
            Client.Host = smtpHost.ToString();
            Client.EnableSsl = true;

            Client.Send(mail);
            sent = true;

        }
        catch (Exception ex)
        {
            sent = false;
        }
        finally
        {

        }
        return sent;
    }



the function calling is
C#
sendMail(Emaild, "softwarebuddy@gmail.com", "mypassword", "Rushi Tammisetti", "Password Recovery", "smtp.gmail.com", "Dear User Your Username Is is:" + username.ToString() + "\n Password is :" + pwd.ToString() + "", "587");



please suggest me.what the wrong is.?

Best Regards,
Rushi
Posted
Updated 20-Aug-13 16:53pm
v2
Comments
ZurdoDev 20-Aug-13 22:24pm    
What's the exact error?
Rushi Raghava 20-Aug-13 22:37pm    
failure sending mail(Exception)
Rushi Raghava 20-Aug-13 22:42pm    
The inner exception was "unable to connect remote server"
ZurdoDev 21-Aug-13 7:12am    
There's your answer. You need to get with your SMTP admin and figure out what credentials and port you need.
BulletVictim 21-Aug-13 5:05am    
I tested your code in a console application using port 25 and it sends the mail without getting any errors?
The layout of how you construct the message is not exactly how I would do it but it still works.
I have to note that the only thing I left out from your code is all the .ToString()s that you use, since you already declare it as a string.

C#
    bool SendMail (string strFromEMail, string strFromName, string strPass, string strToEMail, string strToName string strSub, string strBody)
{
            SmtpClient smtpClient = new SmtpClient( "smtp.gmail.com", 465 );
            System.Net.NetworkCredential credentials = null ;
             bool bSuccess = true ;
            
            MailMessage message = new MailMessage();
            // Try to send the message
            try
            {
                // Prepare two email addresses 
                MailAddress fromAddress = new MailAddress(strFromEMail, strFromName );
                MailAddress toAddress = new MailAddress (strToEMail, strToName);
                // Prepare the mail message
                message.From = fromAddress;
                message.To.Add(toAddress);
                message.Subject = strSub;
                message.IsBodyHtml = true;
                credentials = new System.Net.NetworkCredential( strFromEMail, strPass);
                smtpClient.Credentials = credentials;
                smtpClient.Send(message); 
            }
            catch ( Exception eX )
            {
                 // eX.Message shows  the cause of the error, if it fails. 
                 bSuccess = false ;
            }
            return bSuccess ;
}
 
Share this answer
 
Gmail works on Port 587 and it is tested at my end.

Follow my previous answer - sending email to gmail from asp.net[^].

If you still face the issue, then please check the InnerException to find the exact issue.
 
Share this answer
 

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