SmtpClient with Gmail

后端 未结 6 702
忘了有多久
忘了有多久 2020-11-29 03:47

I\'m developing a mail client for a school project. I have managed to send e-mails using the SmtpClient in C#. This works perfectly with any server but it doesn

相关标签:
6条回答
  • 2020-11-29 04:31

    Gmail's SMTP server requires you to authenticate your request with a valid gmail email/password combination. You do need SSL enabled as well. Without actually being able to see a dump of all your variables being passed in the best guess I can make is that your Credentials are invalid, make sure you're using a valid GMAIL email/password combination.

    You might want to read here for a working example.

    EDIT: Okay here's something I wrote and tested just then and it worked fine for me:

    public static bool SendGmail(string subject, string content, string[] recipients, string from) {
        if (recipients == null || recipients.Length == 0)
            throw new ArgumentException("recipients");
    
        var gmailClient = new System.Net.Mail.SmtpClient {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = new System.Net.NetworkCredential("******", "*****")
        };
    
        using (var msg = new System.Net.Mail.MailMessage(from, recipients[0], subject, content)) {
            for (int i = 1; i < recipients.Length; i++)
                msg.To.Add(recipients[i]);
    
            try {
                gmailClient.Send(msg);
                return true;
            }
            catch (Exception) {
                // TODO: Handle the exception
                return false;
            }
        }
    }
    

    If you need any more info there's a similar SO article here

    0 讨论(0)
  • 2020-11-29 04:34

    I think, you need to validate the server certificate that is used to establish the SSL connections.....

    Use following code to send mail with validating server certificate.....

                this.client = new SmtpClient(_account.SmtpHost, _account.SmtpPort);
                this.client.EnableSsl = _account.SmtpUseSSL;
                this.client.Credentials = new NetworkCredential(_account.Username, _account.Password);
    
            try
            {
                // Create instance of message
                MailMessage message = new MailMessage();
    
                // Add receivers
                for (int i = 0; i < email.Receivers.Count; i++)
                    message.To.Add(email.Receivers[i]);
    
                // Set sender
                message.From = new MailAddress(email.Sender);
    
                // Set subject
                message.Subject = email.Subject;
    
                // Send e-mail in HTML
                message.IsBodyHtml = email.IsBodyHtml;
    
                // Set body of message
                message.Body = email.Message;
    
                //validate the certificate
                ServicePointManager.ServerCertificateValidationCallback =
                delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                { return true; };
    
    
                // Send the message
                this.client.Send(message);
    
                // Clean up
                message = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not send e-mail. Exception caught: " + e);
            }
    

    Import System.Security.Cryptography.X509Certificates namespace to use ServicePointManager

    0 讨论(0)
  • 2020-11-29 04:34

    I started getting this with GMail in May 2013 after working for 6 momths. The Mono project's Using Trusted Roots Respectfully document provided guidance on work around. I chose option #1:

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    

    It is too disruptive to have e-mail for my service stop working without warning.

    Update Aug 26 2016: user Chico suggested the following complete implementation of the ServerCertificateValidationCallback callback. I have not tested.

    ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
    
    bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
        bool isOk = true;
        // If there are errors in the certificate chain, look at each error to determine the cause.
        if (sslPolicyErrors != SslPolicyErrors.None) {
            for (int i=0; i<chain.ChainStatus.Length; i++) {
                if (chain.ChainStatus [i].Status != X509ChainStatusFlags.RevocationStatusUnknown) {
                    chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
                    chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
                    chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan (0, 1, 0);
                    chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
                    bool chainIsValid = chain.Build ((X509Certificate2)certificate);
                    if (!chainIsValid) {
                        isOk = false;
                    }
                }
            }
        }
        return isOk;
    
    }
    
    0 讨论(0)
  • 2020-11-29 04:36

    You need to enable 2-Step Verification in your gmail account and create an app password (https://support.google.com/accounts/answer/185833?hl=en). Once you replace your password with the new app password, it should work.

    Credentials = new System.Net.NetworkCredential("your email address", "your app password");
    
    0 讨论(0)
  • 2020-11-29 04:40

    Try running this:

    mozroots --import --ask-remove
    

    in your system (just in bash or from Mono Command Prompt if it is on Windows). And then run the code again.

    EDIT:

    I forgot you also should run

    certmgr -ssl smtps://smtp.gmail.com:465
    

    (and answer yes on questions). This works for me on Mono 2.10.8, Linux (with your example).

    0 讨论(0)
  • 2020-11-29 04:42

    This code works fine for me, try pasting this into LinqPad, edit the mail addresses and password and tell us what you see:

    var client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("me@gmail.com", "xxxxxxx");
    
    try
    {
        // Create instance of message
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    
        // Add receiver
        message.To.Add("me@gmail.com");
    
        // Set sender
        // In this case the same as the username
        message.From = new System.Net.Mail.MailAddress("me@gmail.com");
    
        // Set subject
        message.Subject = "Test";
    
        // Set body of message
        message.Body = "En test besked";
    
        // Send the message
        client.Send(message);
    
        // Clean up
        message = null;
    }
    catch (Exception e)
    {
        Console.WriteLine("Could not send e-mail. Exception caught: " + e);
    }
    
    0 讨论(0)
提交回复
热议问题