Send Email via C# through Google Apps account

后端 未结 5 921
挽巷
挽巷 2020-11-28 01:12

I have a standard Google Apps account. I have setup a custom domain through Google Apps. I am able to send and receive emails successfully through Google Apps when I use the

相关标签:
5条回答
  • 2020-11-28 01:44

    There is not need to do anything. Just login in your current account first time and follow instructions.

    Your problem will resolve. It occur because you had created the account in google apps but did not login. Just login and follow the instructions and try.

    0 讨论(0)
  • 2020-11-28 01:48

    change the port to 465

    0 讨论(0)
  • 2020-11-28 01:53

    There is no need to hardcode all smtp settings in your code. Put them in web.config instead. This way you can encrypt these settings if needed and change them on the fly without recompiling your application.

    <configuration>
      <system.net>
        <mailSettings>
          <smtp from="example@domain.com" deliveryMethod="Network">
              <network host="smtp.gmail.com" port="587"
                  userName="example@domain.com" password="password"/>
          </smtp>
        </mailSettings>
      </system.net>
    </configuration>
    

    End when you send email just enable SSL on your SmtpClient:

    var message = new MailMessage("navin@php.net");
    // here is an important part:
    message.From = new MailAddress("example@domain.com", "Mailer");
    // it's superfluous part here since from address is defined in .config file
    // in my example. But since you don't use .config file, you will need it.
    
    var client = new SmtpClient();
    client.EnableSsl = true;
    client.Send(message);
    

    Make sure that you're sending email from the same email address with which you're trying to authenticate at Gmail.

    Note: Starting with .NET 4.0 you can insert enableSsl="true" into web.config as opposed to setting it in code.

    0 讨论(0)
  • 2020-11-28 01:53

    This is what I use in WPF 4

    SmtpClient client = new SmtpClient();
    client.Credentials = new NetworkCredential("sender_email@domain.tld", "P@$$w0rD");
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    
    try 
    {
        MailAddress maFrom = new MailAddress("sender_email@domain.tld", "Sender's Name", Encoding.UTF8),
        MailAddress maTo = new MailAddress("recipient_email@domain2.tld", "Recipient's Name", Encoding.UTF8);
        MailMessage mmsg = new MailMessage(maFrom, maTo);
        mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>";
        mmsg.BodyEncoding = Encoding.UTF8;
        mmsg.IsBodyHtml = true;
        mmsg.Subject = "Some Other Text as Subject";
        mmsg.SubjectEncoding = Encoding.UTF8;
    
        client.Send(mmsg);
        MessageBox.Show("Done");
    } 
    catch (Exception ex) 
    {
        MessageBox.Show(ex.ToString(), ex.Message);
        //throw;
    }
    

    Watch for Firewalls and Anti-Viruses. These creepy things tend to block applications sending email. I use McAfee Enterprise and I have to add the executable name (like Bazar.* for both Bazar.exe and Bazar.vshost.exe) to be able to send emails...

    0 讨论(0)
  • 2020-11-28 01:55

    My code is connecting to smtp.google.com using TLS on Port=587 (SSL should be port 465) but still needs EnableSsl=true

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    NetworkCredential credentials = new NetworkCredential();
    credentials.UserName = "INSERT EMAIL";
    credentials.Password = "INSERT PASSWORD";
    smtp.Credentials = credentials;
    
    MailAddress addressFrom = new MailAddress(credentials.UserName);
    MailAddress addressTo = new MailAddress("INSERT RECIPIENT");
    MailMessage msg = new MailMessage(addressFrom, addressTo);
    msg.Subject = "SUBJECT"
    msg.Body = "BODY";
    
    smtp.Send(msg);
    

    Notice these important prerequisites on your G SUITE account

    • Ensure that the username you use has cleared the CAPTCHA word verification test that appears when you first sign in.
    • Ensure that the account has a secure password - https://support.google.com/accounts/answer/32040
    • Make sure that Less secure apps is enabled for the desired account- https://support.google.com/a/answer/6260879
    0 讨论(0)
提交回复
热议问题