代码之家  ›  专栏  ›  技术社区  ›  Vishal Parmar

部署到主机后,使用gmail通过C#发送邮件不起作用

  •  3
  • Vishal Parmar  · 技术社区  · 6 年前

    我想通过Gmail发邮件。当我在localhost上测试时,我成功地发送了邮件,但是当我将邮件上载到web主机时,这不起作用。我看到这种类型的错误:

    请求System.Net.Mail.SmtpPermission,System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089类型的权限失败。

    无论何时使用端口25,都会出现以下类型的错误:

    SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:需要5.5.1身份验证

    下面是我发送电子邮件的代码。

    MailMessage mail = new MailMessage("host@gmail.com","User@gamil.com");
    
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");              
    mail.Subject = "Any String" 
    mail.Body = mailbody;
    mail.IsBodyHtml = true;
    SmtpServer.Port = 587;
    SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
    SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Credentials = new System.Net.NetworkCredential("xyz@gmail.com","123");               
    SmtpServer.EnableSsl = true;
    
    SmtpServer.Send(mail);
    

    有什么解决办法吗?请给我建议!

    3 回复  |  直到 5 年前
        1
  •  4
  •   MindSwipe    6 年前

    编辑: OP补充了对回答这个问题至关重要的额外信息,但我保留了旧的答案,因为它可能仍然对某人有帮助

    新答案: This StackOverflow问题已回答此问题

    旧答案: 作为 this 587 而不是它的默认值( 25 )这需要提升权限,导致此错误,请更改以下内容:

    SmtpServer.Port = 587;
    

    对此:

    SmtpServer.Port = 25;
    

    注: 443

        2
  •  0
  •   Ahmet Uyar    6 年前

    您的代码添加 SmtpDeliveryFormat.SevenBit

    例子:

    using (SmtpClient smtp = new SmtpClient())
                    {
                        NetworkCredential credential = new NetworkCredential
                        {
                            UserName = WebConfigurationManager.AppSettings["UserName"],
                            Password = WebConfigurationManager.AppSettings["Password"],
                        };
                        smtp.Credentials = credential;
                        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                        smtp.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
                        smtp.Host = WebConfigurationManager.AppSettings["Host"];
                        smtp.Port = WebConfigurationManager.AppSettings["Port"].ToNcInt();
                        smtp.EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableSsl"]);
                        smtp.Send(mail);
                    }
    
        3
  •  -1
  •   sanvssan    6 年前

    试试这个

    using System;
    using System.Net;
    using System.Net.Mail;
    
    namespace AmazonSESSample
    {
        class Program
    {
        static void Main(string[] args)
        {
            // Replace sender@example.com with your "From" address. 
            // This address must be verified with Amazon SES.
            String FROM = "a@a.com";
            String FROMNAME = "ABC";
    
            // Replace recipient@example.com with a "To" address. If your account 
            // is still in the sandbox, this address must be verified.
            String TO = "a@a.com";
    
            // Replace smtp_username with your Amazon SES SMTP user name.
            String SMTP_USERNAME = "a@a.com";
    
            // Replace smtp_password with your Amazon SES SMTP user name.
            String SMTP_PASSWORD = "ASJKAJSN";
    
            // (Optional) the name of a configuration set to use for this message.
            // If you comment out this line, you also need to remove or comment out
            // the "X-SES-CONFIGURATION-SET" header below.
            String CONFIGSET = "ConfigSet";
    
            // If you're using Amazon SES in a region other than US West (Oregon), 
            // replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP  
            // endpoint in the appropriate AWS Region.
            String HOST = "smtp-relay.sendinblue.com";
    
            // The port you will connect to on the Amazon SES SMTP endpoint. We
            // are choosing port 587 because we will use STARTTLS to encrypt
            // the connection.
            int PORT = 587;
    
            // The subject line of the email
            String SUBJECT =
                "Amazon SES test (SMTP interface accessed using C#)";
    
            // The body of the email
            String BODY =
                "<h1>Amazon SES Test</h1>" +
                "<p>This email was sent through the " +
                "<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " +
                "using the .NET System.Net.Mail library.</p>";
    
            // Create and build a new MailMessage object
            MailMessage message = new MailMessage();
            message.IsBodyHtml = true;
            message.From = new MailAddress(FROM, FROMNAME);
            message.To.Add(new MailAddress(TO));
            message.Subject = SUBJECT;
            message.Body = BODY;
            // Comment or delete the next line if you are not using a configuration set
            message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);
    
            using (var client = new System.Net.Mail.SmtpClient(HOST, PORT))
            {
                // Pass SMTP credentials
                client.Credentials =
                    new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
    
                // Enable SSL encryption
                client.EnableSsl = true;
    
                // Try to send the message. Show status in console.
                try
                {
                    Console.WriteLine("Attempting to send email...");
                    client.Send(message);
                    Console.WriteLine("Email sent!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The email was not sent.");
                    Console.WriteLine("Error message: " + ex.Message);
                }
            }
        }
    }
    

    }