How to send email to gmail using SMTPclient in C#?

前端 未结 1 340
梦毁少年i
梦毁少年i 2021-01-18 03:03

I am using outloook 2003 and visual studio 2008. i want to develop an application that will send the email to any domain. but my code fails when i\'m trying to send email to

相关标签:
1条回答
  • 2021-01-18 03:34

    This is a good sample for Sending E-Mail with Gmail in C#

    string from = me@gmail.com; //Replace this with your own correct Gmail Address
    
    string to = you@gmail.com //Replace this with the Email Address to whom you want to send the mail
    
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
     mail.To.Add(to);
     mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
    mail.Subject = "This is a test mail" ;
    mail.SubjectEncoding = System.Text.Encoding.UTF8;
    mail.Body = "This is Email Body Text";
    mail.BodyEncoding = System.Text.Encoding.UTF8;
    mail.IsBodyHtml = true ;
    mail.Priority = MailPriority.High;
    
    SmtpClient client = new SmtpClient();
    //Add the Creddentials- use your own email id and password
    
     client.Credentials = new System.Net.NetworkCredential(from, "Password");
    
    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);
            }
            catch (Exception ex) 
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty; 
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }
       HttpContext.Current.Response.Write(errorMessage );
            } // end try 
    

    Are you sure

    message.From = new System.Net.Mail.MailAddress("Sumit.Dhingra@niit.com");
    

    is right? Does this method have an overload like this?

    0 讨论(0)
提交回复
热议问题