ASP.NET - auto generated email smtp code with gmail using and c#

Asked By Zikhona Ntinganti on 12-Aug-13 11:07 AM
Dear all

I am developing an application for B&Bs as a class project. I am having trouble finding relevent code that could help me when I need to send an email to a customer regarding a booking, for example if the room is available, reserved, under maintainace or unavailable. The business rule states that the email must be auto generated, that is the application must be able to send a response quickly or as soon as the submit button is clicked on the booking form.

many thanks.
Zikhona Ntinganti 
Robbe Morris replied to Zikhona Ntinganti on 12-Aug-13 12:28 PM

You'll need an outgoing SMTP server set up by your hosting company or some other provider.  Not sure if gmail perhaps smtp outgoing relays.

using System.Net.Mail;

 var smtpClient = new SmtpClient();
 var message = new MailMessage();

 smtpClient.Host = "your smtp mail server name goes here";
 smtpClient.Port = 25;

 message.From = new MailAddress("youremailaddressgoeshere", "John Smith"); 

 message.To.Add(new MailAddress("to email address"));
 message.Subject = "subject line goes here";
 message.IsBodyHtml = true;
 message.Body = "html of the email";
 smtpClient.Timeout = 15000;
 smtpClient.EnableSsl = false;