create own application to send mail

Satyadevanand
Posted by Satyadevanand under Windows Forms category on | Points: 40 | Views : 1591
<h1>Email Application </h1>


using System.Net;
using System.Net.Mail;

private void btnSend_Click(object sender, EventArgs e)
{
string fromId=txtFrom.Text;
string frompwd=txtPwd.Text;
string to = txtTo.Text;
string sub = txtSub.Text;
string body = txtBody.Text;
try
{
MailMessage msg = new MailMessage(fromId,to);
msg.Subject = sub;
msg.Body = body;
NetworkCredential NetCrd = new NetworkCredential();
NetCrd.UserName = fromId;
NetCrd.Password = fromPwd;
SmtpClient mailClient = new SmtpClient();
mailClient.Host = "smtp.gmail.com";
mailClient.Port = 587;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
MessageBox.Show("Mail has been sent successfully","Info",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


<b>3 Steps to send mail</b>
First Step

MailMessage msg = new MailMessage(fromId,to);
msg.Subject = sub;
msg.Body = body;

<i>Second Step</i>

NetworkCredential NetCrd = new NetworkCredential();
NetCrd.UserName = fromId;
NetCrd.Password = fromPwd;

<i>Third Step</i>

SmtpClient mailClient = new SmtpClient();
mailClient.Host = "smtp.gmail.com";
mailClient.Port = 587;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);

Comments or Responses

Login to post response