I Write code to send e-mail from an ASP.NET application..Is this correct for that or need some changes..any changes in the code will be happilly acceptable..

MailMessage mailMess = new MailMessage ();
mailMess.From = "abc@gmail.com";
mailMess.To = "xyz@gmail.com";
mailMess.Subject = "Test email";
mailMess.Body = "Hi This is a test mail.";
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send (mailMess);

I am beginner in this field recently join Online Dot Net training course so I want to clear my doubt

Dani AI

Generated

Good start, — and good tips from and . A few practical, modern notes to take this from “works in a lab” to “robust and secure in production”:

  • For new .NET apps prefer a modern library instead of relying on old SmtpClient patterns; Microsoft now points developers to libraries such as MailKit for better protocol and auth support. (learn.microsoft.com)
  • If you plan to use Gmail: Google removed the old “less secure apps” behavior (May 30, 2022). That means simple username+password logins will often be blocked — use a Google App Password (or OAuth2) for SMTP access. ()
  • Never hard-code credentials. Use local secret tools in dev and a vault (Key Vault / Secrets Manager / environment variables) in production.

A compact, production-friendly approach is MailKit + MimeKit (NuGet). Example (replace host/user/secret with your provider values):

// requires MailKit and MimeKit packages
var msg = new MimeKit.MimeMessage();
// set From / To / Subject / Body here...
using var smtp = new MailKit.Net.Smtp.SmtpClient();
await smtp.ConnectAsync("smtp.example.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync("username", "app-or-api-password");
await smtp.SendAsync(msg);
await smtp.DisconnectAsync(true);

MailKit supports async APIs, modern auth mechanisms and proper lifecycle management — see the project docs. (github.com)

If sending fails: capture the exact exception and protocol log, test connectivity with a local fake SMTP server (smtp4dev or Papercut) and with simple network checks (telnet / openssl) to rule out firewall/host or port blocking, then verify provider-specific auth (app password vs OAuth). Use a transactional provider (SendGrid/Mailgun) or proper SPF/DKIM/DMARC on your domain for best deliverability in production. For local testing, smtp4dev and Papercut are handy. (swimburger.net)

Short, practical checklist: remove plain passwords from code, switch to MailKit for modern .NET apps, use provider-recommended auth for Gmail, and test locally before deploying.

Recommended Answers

All 3 Replies

Your code is close. Try these two minor fixes:

mailMess.From.Add("abc@gmail.com");
mailMess.To.Add("xyz@gmail.com");

If you still have an issue, or if you just want to use some other email host server (i.e. post-office) add this code:

SmtpMail.Credentials = New System.Net.NetworkCredential("Your post Office Username", "and Password";
SmtpMail.Host = "your mail server (ie. smtp.mailhost.net)";

optionally if your server supports these:

SmtpMail.Port=587;
SmtpMail.EnableSSL=True;
SmtpMail.Timeout=15000; (15 seconds)

Also, optionally if you want to send HTML in the message:

mailMess.IsBodyHtml = True;

That covers how I user it over and over and over!
HTH,
Sean

Thank you

public static void SendEmail(string ToEmail)
    {
            var MailSubject = "Test email";
            var MailBody = "Hi This is a test mail.";
            string FromMail = "abc@gmail.com";
            string Password = "********";
            SmtpClient client = new SmtpClient();
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(FromMail, Password);
            MailMessage mailMessage = new MailMessage(FromMail, ToEmail, MailSubject, MailBody);
            mailMessage.BodyEncoding = UTF8Encoding.UTF8;
            mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            client.Send(mailMessage);
    }
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.