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

Your code is close. Try these two minor fixes:


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.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!

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;
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.