繁体   English   中英

无法连接到远程服务器

[英]Unable to connect to remote server

我是C#的新手,我正在尝试从我正在开发的桌面程序发送电子邮件。 这是我正在使用的代码,但我不断收到以下错误:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("thesma@gmail.com");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("ideas@gmail.com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com",578);
smtp.EnableSsl = true;
smtp.Send(message);

我似乎无法找出问题是什么......

您需要设置Gmail的凭据

var client = new SmtpClient("smtp.gmail.com", 587)
 {
    Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
    EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");

最有可能丢失凭证:

smtp.Credentials = new System.Net.NetworkCredential("ideas@gmail.com", "password");

所以:

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("ideas@gmail.com", "Password");
smtp.EnableSsl = true;
smtp.Send(message);

或者,您可以在app.config存储(几乎所有)这些内容,但我不确定您需要它的安全性,因为打开应用程序目录的任何用户都可以清楚地看到用户/通行证(并且文件)。 为了完成起见:

<system.net>
  <mailSettings>
    <smtp from="ideas@gmail.com">
      <network host="smtp.gmail.com"
               enableSsl="true"
               userName="ideas@gmail.com"
               password="password"
               port="587" />
    </smtp>
  </mailSettings>
</system.net>

网络凭据非常重要,但有时我们需要检查端口587是否被阻止。

   SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential("jorgesys@gmail.com", "whocaresdupe");
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    try
    {
        client.Send(mail);          
    } catch (Exception ex)
    {
       Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendEmail.aspx';}</script>");
    }

我的公司阻止桌面/笔记本电脑使用McAfee中的设置发送电子邮件。 检查Windows事件视图以查看是否存在阻止电子邮件的条目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM