简体   繁体   English

在C#中通过SMTP发送电子邮件时出错,这在2017年仍然有效吗?

[英]Error sending email via SMTP in C#, Does this still work in 2017?

Having researched the issue extensively especially applying recommendations from similar issues on stackoverflow, the below code still returns error ""System.Net.Mail.SmtpException: Syntax error, command unrecognized. 在对问题进行了广泛研究之后,尤其是在stackoverflow上应用了类似问题的建议之后,下面的代码仍然返回错误“” System.Net.Mail.SmtpException:语法错误,命令无法识别。 The server response was: connection rejected at...."" 服务器响应是:在...处拒绝连接。“”

        try
        {
            var mail = new MailMessage();
            mail.From = new MailAddress("demo77377@gmail.com");
            mail.To.Add(new MailAddress("xxxxxxx@gmail.com"));
            mail.Subject = "TEST";
            mail.Body = "This is a test mail from C# program";

            using (var smtp = new SmtpClient())
            {
                smtp.Credentials = new System.Net.NetworkCredential("demo77377@gmail.com", "AABBCCDDEE1!","gmail.com");
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Timeout = 10000;                    
                //
                smtp.Send(mail);
                Console.WriteLine("Message sent successfully");
                Console.ReadLine();
            }

        }
        catch (Exception e)

I have done everything possible 我已尽一切可能
On Client> I have alternated smtp properties (permutation) etc 在客户端上>我有交替的smtp属性(排列)等
On Server> I have made gmail account less secure, I have disabled captcha etc 在服务器上>我使Gmail帐户的安全性降低,我禁用了验证码等

I observed that similar issues on stackoverflow were mostly dated over 3years ago and thus, is it possible that gmail no longer supports this SMTP method via C#, likewise has it been deprecated in favor of gmail API 我观察到关于stackoverflow的类似问题主要是在3年前,因此gmail是否有可能不再通过C#支持此SMTP方法,同样,不赞成使用gmail API

Also, please find provided in code, original password supplied for the gmail account, in order to confirm if this issue is general or isolated to this gmail account 另外,请在代码中找到该gmail帐户提供的原始密码,以确认此问题是该gmail帐户的一般性问题还是孤立的

Thanks 谢谢

This code is verified to work fine: 验证此代码可以正常工作:

var fromAddress = new MailAddress("YOUREMAIL@gmail.com", "YOUREMAIL@gmail.com");
var toAddress = new MailAddress("YOUREMAIL@gmail.com", "YOUREMAIL@gmail.com");
const string fromPassword = "YOURPASSWORD";
const string subject = "YOUREMAIL@gmail.com";
const string body = "YOUREMAIL@gmail.com";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

I'm not comfortable including your personal details even though you have so I have removed them. 即使您有个人信息,我也不太满意,因此我已将其删除。 Obviously replace the email and password with the correct details. 显然用正确的详细信息替换电子邮件和密码。

从我酒店的无线网络切换到办公网络后,终于可以正常工作了。网络连接的类型在C#中通过SMTP发送电子邮件中起着一定作用。

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

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