Click here to Skip to main content
15,945,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
I wanna send email from smtp server with gmail but I get the error that I dont know what is happened
content of error:
The remote certificate is invalid according to the validation procedure.
and this is my code
C#
protected void SendMail(string from, string to, string cc, string bcc, string subject, string body)
{
    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress(from, "your name");
    mailMessage.To.Add(to);
    if (cc != null)
        mailMessage.CC.Add(cc);
    if (bcc != null)
        mailMessage.Bcc.Add(bcc);
    mailMessage.Subject = subject;
    mailMessage.IsBodyHtml = true;
    mailMessage.Body = body;
    mailMessage.Priority = MailPriority.Normal;
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    NetworkCredential info = new NetworkCredential("mymail@gmail.com", "123123");
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Credentials = info;
    client.EnableSsl = true;
    client.Send(mailMessage);
}

protected void Button1_Click(object sender, EventArgs e)
{

    SendMail("mymail@gmail.com", "mymail@yahoo.com", null, null, "new mail", "Send Mail with Gmail");




}
Posted
Updated 13-Mar-12 16:17pm
v2

C#
  protected void SendMail(object sender, EventArgs e)
  {

          System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress(@ConfigurationSettings.AppSettings["EmailFrom"]);
          smtpClient.Host = @ConfigurationSettings.AppSettings["MailServerName"];
          smtpClient.Port = Convert.ToInt32(@ConfigurationSettings.AppSettings["Port"]);
          message.From = fromAddress;
          message.To.Add(@ConfigurationSettings.AppSettings["EmailTo"]);
          message.Subject = @ConfigurationSettings.AppSettings["EmailSubject"];
          message.IsBodyHtml = false;
          message.Body = " Any Message Text.....";
          smtpClient.Send(message);
      }
      finally
      {
          sw.Close();
      }
  }

<appSettings>
<connectionStrings>

  <add key="Port" value="587"/>
  <add key="MailServerName" value="smtp.gmail.com"/>
  <add key="EmailFrom" value="emailfrom@gmail.com"/>
  <add key="EmailPassword" value="pass@123"/>
  <add key="EmailTo" value="emailto@gmail.com"/>
  <add key="EmailSubject" value="My Email Subject"/>

</appSettings>
<connectionStrings/>
 
Share this answer
 
v2
Sometimes certificate errors occur due to the invalid date time settings in the system clock. Make sure that it is the current date and time.
I dont see any problem in your code...
 
Share this answer
 
try this,


MailAddress mailfrom = new MailAddress ( "frommail@gmail.com" );
MailAddress mailto = new MailAddress ( "tomail@gmail.com" );
MailMessage newmsg = new MailMessage ( mailfrom, mailto );

newmsg.Subject = "Subject of Email";
newmsg.Body = "Body(message) of email";

////For File Attachment, more file can also be attached

Attachment att = new Attachment ( "G:\\code.txt" );
newmsg.Attachments.Add ( att );

SmtpClient smtps = new SmtpClient ( "smtp.gmail.com", 587 );
smtps.UseDefaultCredentials = false;
smtps.Credentials = new NetworkCredential ( "mail@gmail.com", "pwd" );
smtps.EnableSsl = true;
smtps.Send ( newmsg );
 
Share this answer
 
This user doesn't have a yahoo.com account (mymail@yahoo.com)
First make it or use different id.
 
Share this answer
 
Comments
behrad110 4-Mar-12 13:58pm    
it is for sample but i have a valid user for yahoo
jeetveer 4-Mar-12 14:13pm    
your cod is working and there is no error when i use different e-mail id.
and i also see that when i send a mail on "mymail@yahoo.com" that time error message occur and that is "This user doesn't have a yahoo.com account (mymail@yahoo.com)... "
how i can retrive the mail which is become an our mail account o our asp.net application usind c#
 
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