Click here to Skip to main content
15,868,006 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi,
I am trying to send mail from gmail and it's sent successfully in localhost
but when publish website on server didn't send
any help here?
it's my code
C#
MailMessage mail = new MailMessage();
            mail.Subject = strSubject;
            mail.From = new MailAddress("xyz@gmail.com");
            mail.To.Add(ToEmail);
            mail.Bcc.Add("abc@gmail.com");
            mail.Subject = strSubject;
            mail.Body = strBody;
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 25);
            smtp.EnableSsl = true;
            NetworkCredential netCre = new NetworkCredential("xyz@gmail.com", "myPassword");
            smtp.Credentials = netCre;
            try
            {
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    string exInner = ex.InnerException.ToString();
                }
                msg = false;
            }
Posted
Updated 10-Aug-21 1:57am
Comments
Sergey Alexandrovich Kryukov 2-Feb-16 14:55pm    
It will ping successfully, if the machine is connected to Internet? Why pinging it again?
—SA
Sergey Alexandrovich Kryukov 2-Feb-16 14:57pm    
One unrelated problem: catching an exception. First, you block exception propagation, silently. Second, your assignment to InnerException might not be a bad idea, but it does nothing; you have to re-throw an exception. Third, it's generally pretty bad to handle exceptions too locally.

By the way, why not using a SMTP server of the hosting provider? This is the most usual thing.

—SA
Michael_Davies 2-Feb-16 15:18pm    
Gmail normally uses port 465 for SMTP, have you tried that instead of 25.

If it is working on local and now on server, then there may not be code issue, you need to check other things like firewall settings.
What error you got exactly ?
try out with port number 587, see below snippet
C#
MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
   smtp.Send(mail);                
  }
  catch (Exception ex)
  {               
  }
 
Share this answer
 
The solution is to not use gmail, use the smtp server provided by your webhost.

Things you shouldn't spend time doing | The ASP.NET Forums[^]
 
Share this answer
 
below the attached solution work over local machine and server.

string fromEmail = "from@gmail.com";
MailMessage mailMessage = new MailMessage(fromEmail, "to@gmail.com", "Subject",body);
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, frompassword);
smtpClient.Send(mailMessage);
 
Share this answer
 
Comments
CHill60 15-Jun-20 12:57pm    
What does this add that hadn't already been said in Solution 1? Nothing.

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