Sending emails from dotnet8 web application

Daniel Giles 0 Reputation points
2025-03-29T22:57:05.87+00:00

Hi, I have a dotnet8 web application which I am using to send emails from ******@mydomain.com (which is a google workspace account) via a smtp request using the following configuration:
Domain = smtp.gmail.com
Port = 465
A generated App Password stored in my google admin console.

When running the dotnet8 web application on my personal windows machine, it is able to establish a connection using the above configuration and therefore successfully send the email. However, when I deploy my dotnet8 web application to IONOS it is no longer able to establish a connection and thus unable to send the emails. IONOS has ensured me there is nothing on their system that would simply block the request to "smtp.gmail.com", however they also suggested that I may be missing some additional configuration whether it be in my google admin console or there some DNS settings that I need to include. With that in mind, I would greatly appreciate it if you could help me to shed shed some light on this because I have tried several configurations but nothing happening. I have created an SPF record, DKIM, DMARC, verified my domain using google postmaster tools and enabled "less secure app access".

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. AgaveJoe 30,206 Reputation points
    2025-03-30T10:58:15.2166667+00:00

    For a more robust and secure email solution, modernizing your application and migrating to Google's Gmail API is highly recommended. Utilizing a library like MailKit can simplify this process.

    https://developers.google.com/workspace/gmail/api/guides

    To effectively troubleshoot the current connection problem, the error message is crucial. If you don't have it, adding logging and exception handling will help us trace the application's behavior. Additionally, the use of SSL port 465 is problematic as SSL is outdated and vulnerable. Try switching to port 587, which uses the modern and secure TLS protocol. Finally, Google's 'less secure apps access' is being deprecated; App Passwords are the recommended alternative for secure access but this might not be effective for an app to app connection.

    https://support.google.com/mail/answer/185833?hl=en#zippy=%2Cwhy-you-may-need-an-app-password

    0 comments No comments

  2. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-03-31T16:42:28.2033333+00:00

    gmail support of basic authentication ended 3/14/2025:

    https://workspaceupdates.googleblog.com/2023/09/winding-down-google-sync-and-less-secure-apps-support.html

    thus the SmtpClient will no longer work with gmail. currently there are no planned updates to SmtpClient (as its legacy and the update would not be transparent). as suggested you must replace with a library like MailKit that supports modern authentication.

    0 comments No comments

  3. Danny Nguyen (WICLOUD CORPORATION) 800 Reputation points Microsoft External Staff
    2025-07-08T08:04:40.4033333+00:00

    Hi Daniel,

    You could try the following troubleshooting steps:

    1. Switch to Port 587 with STARTTLS

    Port 465 uses implicit SSL, which is outdated and can be problematic in some hosting environments. Port 587 with STARTTLS is the modern, secure standard. MailKit example:

    using MailKit.Net.Smtp;
    using MimeKit;
    
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Your Name", "******@mydomain.com"));
    message.To.Add(new MailboxAddress("Recipient", "recipient@example.com"));
    message.Subject = "Test Email";
    message.Body = new TextPart("plain") { Text = "Hello from .NET!" };
    
    using var client = new SmtpClient();
    client.Connect("smtp.gmail.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
    client.Authenticate("******@mydomain.com", "your-app-password");
    client.Send(message);
    client.Disconnect(true);
    
    1. Enable Detailed Logging

    Add exception handling and logging to capture the exact error message when the app fails on IONOS. This will help pinpoint whether it’s a network issue, authentication failure, or TLS handshake problem.

    1. Test SMTP Connectivity from IONOS

    Run a command like this from your IONOS server:

    openssl s_client -connect smtp.gmail.com:587 -starttls smtp
    

    If this fails, it’s likely a network or firewall issue despite IONOS’s assurances.

    1. Google Workspace Configuration
    • Ensure the App Password is valid and not revoked.
    • Check if SMTP relay is enabled in your Google Admin Console.
    • Consider whitelisting your IONOS server IP under:
      • Apps > Google Workspace > Gmail > Advanced settings > SMTP relay service
    1. DNS Records

    SPF, DKIM, and DMARC are great for email deliverability but don’t affect SMTP authentication. Still, make sure your SPF record includes:

    v=spf1 include:_spf.google.com ~all
    

    Also as suggested my AgaveJoe, using the Gmail API is a more secure and scalable approach, especially for production apps.

    Benefits:

    • Uses OAuth2, eliminating the need for app passwords.
    • Avoids SMTP limitations and firewall issues.
    • Provides access to advanced Gmail features (labels, threads, attachments).

    Getting Started:

    • Register your app in Google Cloud Console.
    • Use libraries like Google.Apis.Gmail.v1 or MailKit with OAuth2.
    • Follow the Gmail API Quickstart for .NET

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.