How to send an Email using Gmail SMTP server.
Hi, here is simple way to send an email using Gmail
SMTP server. Create a form in visual studio with below fields in image.
Add a button to rename it “btnSend”
Now add the following code in btnsend_Click event.
private void
btnSend_Click(object sender, EventArgs e)
{
try
{
//Create object of System.Net.Mail.MailMessage
MailMessage email = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
//Sender Address
email.From = new MailAddress(txtFrom.Text);
//Receiver Address
email.To.Add(TxtTo.Text);
email.Subject = TxtSubject.Text;
email.Body = txtMessage.Text;
//Email Attachment
if (linkLabel1.Text.Trim() != "")
{
System.Net.Mail.Attachment
attachment;
attachment = new System.Net.Mail.Attachment(linkLabel1.Text);
email.Attachments.Add(attachment);
}
SmtpServer.Port = 587; // Gmail SMTP Port
//Sender User/Password.
SmtpServer.Credentials = new System.Net.NetworkCredential(txtUser.Text.Trim(),
txtPassword.Text.Trim());
SmtpServer.EnableSsl = true;
SmtpServer.Send(email);
linkLabel1.Text = "";
MessageBox.Show("send Successfully.");
}
catch (Exception
ex)
{
MessageBox.Show(ex.ToString());
}
}
Test the application and enjoy!!!!
No comments:
Post a Comment