I have been trying to send mail through my c# website, but kinda stuck up somewhere and not able to figure out the problem. :-/
M getting SmtpException saying "Failure sending mail". Please help.:icon_confused:

MailAddress fromAddress = new MailAddress("abc@gmail.com", "n1");
                MailAddress toAddress = new MailAddress("xyz@yahoo.co.in", "n2");
                const string fromPassword = "pwd";
                const string subject = "Subject";
                const string body = "Body";
                // SMTP Configuration
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword);
                // MAIL MESSAGE CONFIGURATION
                MailMessage message = new MailMessage(fromAddress, toAddress);
                message.Subject = subject;
                message.Body = body;
                // SEND EMAIL
                smtp.Send(message);

Dani AI

Generated

The thread shows the common pattern: the C# SMTP code often works but the environment or provider blocks the send. found the real cause (SMTP outbound disabled by the OTG team). The troubleshooting steps suggested by and the sample from are good basic checks (credentials, port, TLS), but the next actions are network and provider checks followed by modern authentication checks.

Recommended troubleshooting checklist (in order):

  • Confirm the mailbox credentials by signing into the provider’s web UI and check for any security alerts.
  • Verify the mail server supports the chosen transport (Gmail supports STARTTLS/TLS and common submission ports). (developers.google.com)
  • From the web/app host, test TCP connectivity to the mail host (PowerShell Test‑NetConnection, telnet where appropriate, or openssl s_client -starttls smtp for STARTTLS) to confirm the port is reachable. (support.outboundsmtp.com)
  • Capture full exception details: log SmtpException.Message, SmtpException.InnerException, and any SmtpFailedRecipient* exceptions so the exact SMTP/transport error is visible.
  • Check network policy: many hosts, ISPs, or corporate networks block outbound SMTP (or require a relay) — coordinate with the network/OTG team as did.

Authentication and provider notes:

  • Google has tightened simple username/password access; App Passwords (requires 2‑Step Verification) or OAuth2 are the supported paths for SMTP access. Workspace/admin policies can further restrict these options, so admin settings may need adjustment. ()

Longer-term: use a supported library

  • Microsoft documentation discourages System.Net.Mail.SmtpClient for new development and recommends modern libraries such as MailKit; MailKit also has built-in support for OAuth2 and robust TLS handling. (learn.microsoft.com)

Minimal MailKit send example (replace credentials and never hard-code secrets):

using MimeKit;
using MailKit.Net.Smtp;
using MailKit.Security;

var msg = new MimeMessage();
msg.From.Add(new MailboxAddress("n1","abc@gmail.com"));
msg.To.Add(new MailboxAddress("n2","xyz@yahoo.co.in"));
msg.Subject = "Subject";
msg.Body = new TextPart("plain") { Text = "Body" };

using var client = new SmtpClient();
client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
client.Authenticate("abc@gmail.com", "APP_PASSWORD_OR_OAUTH_TOKEN");
client.Send(msg);
client.Disconnect(true);

Key takeaways: confirm network access first (the most common cause), then verify provider authentication rules (App Passwords/OAuth), and prefer MailKit for new work to avoid platform compatibility issues.

Recommended Answers

All 4 Replies

Your code looks ok, did you check those itens:
- From e-mail and from password are really ok?
- Gmail port is really 587?

If it's ok, try those things:
- Don't set EnableSsl
- Don't set UseDefaultCredentials
- Don't set DeliveryMethod

i used gmail smpt and never set those, and it worked. But i don't remeber the smpt port.

Hope it helps.

Thank you...
I found the problem. SMTP for my system was disabled by the OTG dept. Code is working fine now.

Hi,

You can do this by just write some lines of code which has given below

protected void Button1_Click(object sender, EventArgs e)
{
  MailMessage mail = new MailMessage();
  mail.To.Add("jainamit.agra@gmail.com");
  mail.To.Add("amit_jain_online@yahoo.com");
  mail.From = new MailAddress("jainamit.agra@gmail.com");
  mail.Subject = "Email using Gmail";

  string Body = "Hi, this mail is to test sending mail"+ 
                "using Gmail in ASP.NET";
  mail.Body = Body;

  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
  smtp.Credentials = new System.Net.NetworkCredential
       ("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;
  smtp.Send(mail);
}

From
Websoftcreation,jaipur

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.