如何使用GMail帐户发送电子邮件?

时间:2014-01-14 16:35:33

标签: c# asp.net

我使用以下代码从我的GMail帐户发送电子邮件。但是发生了以下错误。 Unable to connect to the remote server 我的代码出了什么问题?

        string from = "xxxx@gmail.com";
        string to = TxtTo.Text;
        string cc = "yyyy@gmail.com";
        string subject = TxtSubject.Text;
        string body = EdtContext.Content;
        MailMessage EMail = new MailMessage();
        MailAddress Sender = new MailAddress(from);
        EMail.From = Sender;
        EMail.To.Add(to);
        EMail.CC.Add(cc);
        EMail.Body = body;
        EMail.IsBodyHtml = true;
        EMail.Subject = subject;
        EMail.BodyEncoding = Encoding.UTF8;
        SmtpClient Local = new SmtpClient("smtp.gmail.com");//--- smtp
         Local.Port = 465;
         Local.EnableSsl = true;
         Local.UseDefaultCredentials = false;           
        Local.Credentials = new System.Net.NetworkCredential("xxxx@gmail.com", "*****");//------ email user and pass in sobhan mail
        Local.Send(EMail);

***************
**Port 587 solved problem**

1 个答案:

答案 0 :(得分:0)

确保您为Gmail设置了正确的凭据

另外,我认为用于向Gmail发送邮件的端口是465用于SSL或587用于TLS / STARTTLS。

此链接上讨论过类似的问题

  

Sending email in .NET through Gmail