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

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.