public class MailClass
{
public static void CreateMailAddresses(string sender, string reciever, out MailAddress mailfrom, out MailAddress mailto)
{
MailAddress Mailfrom = new MailAddress(sender);
MailAddress Mailto = new MailAddress(reciever);
mailfrom = Mailfrom;
mailto = Mailto;
}
public static MailMessage MailMessage(MailAddress mailfrom, MailAddress mailto, string subject, string body)
{
MailMessage newmsg = new MailMessage(mailfrom, mailto);
newmsg.Subject = subject;
newmsg.Body = body;
newmsg.IsBodyHtml = true;
return newmsg;
}
public static SmtpClient SmtpClient(string sender, string password, string host, bool SslEnabled, int port)
{
SmtpClient smtp = new SmtpClient(host, port);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(sender, password);
smtp.EnableSsl = SslEnabled;
return smtp;
}
}
private void button1_Click(object sender, EventArgs e)
{
MailAddress mailfrom;
MailAddress mailto;
MailClass.CreateMailAddresses("sender@gmail.com", "reciever@ymail.com", out mailfrom, out mailto);
MailMessage newmsg = MailClass.MailMessage(mailfrom, mailto, "Subject of Email", "Body(message) of email");
SmtpClient smtp = MailClass.SmtpClient("sender@gmail.com", "password", "smtp.gmail.com", true, 587);
smtp.Send(newmsg);
}