Interview Question
Qus: How do I set up an email server in ASP.NET using C#?
Answers (2)
2. Set the properties for the SmtpClient and MailMessage instances (such as the mail server, sender address, recipient address, message subject, and so on).
3. Call the Send() method of the SmtpClient instance to send the message.
Example –
using System.IO;
using System.Net;
using System.Net.Mail;
public class SendEmail
{
public void Email()
{
string to = "toaddress@gmail.com"; //To address
string from = "fromaddress@gmail.com"; //From address
MailMessage message = new MailMessage(from, to);
message.Subject = "Sending Email";
message.Body = "Sample Email";
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail SMTP
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("yourmail id", "Password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
}