简体   繁体   English

尝试通过CS程序发送电子邮件时,为什么我的连接超时?

[英]Why am I getting a connection timed out when trying to send an email through a CS program?

Firewall: Turned OFF. 防火墙:已关闭。 I am getting a connection timed out error. 我收到连接超时错误。 The email and password are verified. 电子邮件和密码已验证。

The exception is: System.Net.Mail.SmtpException : The operation has timed out. 例外是:System.Net.Mail.SmtpException:操作已超时。

My code 我的密码

 string filename = @"C:\emailsample.htm";

        string mailbody = System.IO.File.ReadAllText(filename);

        mailbody = mailbody.Replace("##NAME##", firstname.Text);

        string to = emailid.Text;

        string from = "xxx.abc45@gmail.com";

        MailMessage message = new MailMessage(from, to);

        message.Subject = "Auto Generated Mail";

        message.Body = mailbody;

        message.BodyEncoding = Encoding.UTF8;

        message.IsBodyHtml = true;

        SmtpClient client = new SmtpClient("smtp.gmail.com", 465);

        System.Net.NetworkCredential basic = new System.Net.NetworkCredential(from, "Password");

        client.EnableSsl = true;

        client.Timeout = 20000;

        client.DeliveryMethod = SmtpDeliveryMethod.Network;

        client.UseDefaultCredentials = false;

        client.Credentials = basic;

        try
        {
            client.Send(message);

        }

        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.ToString());
            return;
        }

Try modifying the Port to 587. 尝试将端口修改为587。

Also verify if the From email is correct. 还要验证发件人电子邮件是否正确。

 SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("user@gmail.com","password");

MailMessage mm = new MailMessage("donotreply@domain.com", "sendtomyemail@domain.co.uk", "test", "test");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

client.Send(mm);

暂无
暂无

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

相关问题 当我尝试通过SmtpClient.Send方法发送电子邮件时,为什么会抛出异常? - Why it is throwing an exception when I am trying to send an email by SmtpClient.Send method? 通过代理时连接超时 - Connection timed out when going through proxy 为什么我收到错误 CS0246:尝试使用 OpenHtmlToPdf 时找不到类型或命名空间名称? - Why am I getting error CS0246: The type or namespace name could not be found when trying to use OpenHtmlToPdf? 通过Postgresql中的函数删除记录时,为什么会出现“ bigint超出范围”错误? - Why am I getting an “out of range for bigint” error when deleting records through a function in Postgresql? 为什么我收到错误“您的请求已超时。 请重试请求”一次? - Why am I getting the error “your request is timed out. please retry the request” once in a while? Unity C#:为什么我会收到错误 CS1022:Typenamespace definition,或 end-of-file expected,试图按索引从列表中选择一个随机游戏对象? - Unity C#: why am I getting error CS1022: Typenamespace definition, or end-of-file expected, trying to pick a random gameobject out of a list by index? 当我尝试与我的数据库建立连接时出现错误 - Getting an error when I am trying to make a connection with my database 尝试从 TextBox 中获取 integer 时出现错误 - I am getting error when trying to get integer out of TextBox 实施事件时,为什么会出现无连接错误? - Why am I Getting a no connection Error when I Implement an Event? 为什么在没有网络连接的情况下这个 webclient post code 没有超时? - why is this webclient post code not being timed out when there is no network connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM