Send email using System.Net.Mail through gmail

2019-01-04 23:13发布

I want to send a email through gmail server. I have put the following code but it is getting stuck while sending. Any idea please....

MailMessage mail = new MailMessage();

        mail.From = new System.Net.Mail.MailAddress("apps@xxxx.com");

        //create instance of smtpclient
        SmtpClient smtp = new SmtpClient();
        smtp.Port = 465;
        smtp.UseDefaultCredentials = true;

        smtp.Host = "smtp.gmail.com";            

        smtp.EnableSsl = true;

        //recipient address
        mail.To.Add(new MailAddress("yyyy@xxxx.com"));

        //Formatted mail body
        mail.IsBodyHtml = true;
        string st = "Test";

        mail.Body = st;
        smtp.Send(mail);

The xxxx.com is a mail domain in Google apps. Thanks...

标签: c# .net email
7条回答
三岁会撩人
2楼-- · 2019-01-04 23:53

Set smtp.UseDefaultCredentials = false and use smtp.Credentials = new NetworkCredential(gMailAccount, password);

查看更多
登录 后发表回答