Hello,
Could you please help me in writing a code for sending an e-mail in c#...
Regards..
System.Web.Mail (the API shown earlier in this thread) is the older .NET 1.x approach; it was replaced by System.Net.Mail in .NET 2.0 and is not the right starting point for new projects. For modern development Microsoft advises alternatives to the built-in SmtpClient for richer, more reliable mail features. Sending mail in ASP.NET 2.0 and the official SmtpClient docs explain this history and guidance. (c-sharpcorner.com)
Looking at ’s sample: the configuration is inconsistent (host/port and credentials). Public SMTP services like Gmail require TLS/SSL and authentication (smtp.gmail.com with STARTTLS on 587 or SSL on 465) and will reject anonymous or DefaultNetworkCredentials; if the account uses 2‑step verification an app password (or OAuth2) is required. Also remember the common SmtpClient gotcha: set UseDefaultCredentials = false before assigning Credentials, otherwise the credentials you set may be ignored. See Google’s SMTP guidance and Google’s app‑password docs, and Microsoft’s UseDefaultCredentials notes. (developers.google.com)
For reliable, up‑to‑date sending in modern .NET, use MailKit/MimeKit rather than relying on the older SmtpClient for new features or OAuth support. Minimal MailKit example (Gmail + STARTTLS):
using MimeKit;
using MailKit.Net.Smtp;
using MailKit.Security;
var msg = new MimeMessage();
msg.From.Add(MailboxAddress.Parse("sender@example.com"));
msg.To.Add(MailboxAddress.Parse("recipient@example.com"));
msg.Subject = "Test";
msg.Body = new TextPart("html") { Text = "<p>Hello</p>" };
using var smtp = new SmtpClient();
smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
smtp.Authenticate("your@gmail.com", "app-password");
smtp.Send(msg);
smtp.Disconnect(true); MailKit is the actively maintained recommendation and has better protocol and auth support than SmtpClient. (github.com)
Quick troubleshooting checklist (from the thread problems above):
This addresses the root issues in the posted code: old API, wrong host/port, and credential/order mistakes. Use the MailKit snippet above for a modern, robust solution.
Jump to Post— stymiee 111Have you written any code yet? If not do so as we do not do work for you. We are only here to help if you have a problem. So write some code and if you have a specific question or problem post it here with your code and we …
Have you written any code yet? If not do so as we do not do work for you. We are only here to help if you have a problem. So write some code and if you have a specific question or problem post it here with your code and we will try to help you. Otherwise you will need to hire someone to do this work for you.
Sure I worked on it before, but because I am new for c# I faced many problems,
System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();
message.From="from e-mail";
message.To="to e-mail";
message.Subject="Message Subject";
message.Body="Message Body";
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(message);
for example this code it does not work,,
Try out this code.
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
//message.To.Add("xxxx@gmail.com");
message.Subject = "Rental Agreement Form";
message.From = new System.Net.Mail.MailAddress("user@msssoftware.com");
message.To.Add("xxxx@gmail.com");
//message.CC.Add("xxxx@gmail.com");
message.IsBodyHtml = true;
message.Body = result;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 25);
smtp.Host = "localhost";
smtp.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
smtp.Send(message);
Label3.Text = "Mail sent Successfully"; We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.