In this article we are going to see how to send mail using C# application in windows forms, instead of login into the gmail account, just from the windows itself open the form and send the message to them .
For do this two class are used in c# , SmtpClient client which hold the host information,port number and credentials using this client we send message with the help of MailMessage Class.
For do this two class are used in c# , SmtpClient client which hold the host information,port number and credentials using this client we send message with the help of MailMessage Class.
protected string
SendEmail(string username, string password, string
toAddress, string subject, string body)
{
string result = "Message
Sent Successfully..!!";
string senderID = username;
string senderPassword = password;
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
MailMessage message = new MailMessage(senderID,
toAddress, subject, body);
smtp.Send(message);
}
catch (Exception
ex)
{
result = "Error sending email.!!!";
}
return result;
}
private void Button_Click(object sender, RoutedEventArgs
e)
{
if (String.IsNullOrEmpty(username.Text))
{
MessageBox.Show("Please fill the user id ");
return;
}
if (string.IsNullOrEmpty(password.Password))
{
MessageBox.Show("Please fill the password");
return;
}
if (string.IsNullOrEmpty(Sender.Text))
{
MessageBox.Show("Please specify the Sender address");
return;
}
try
{
MessageBox.Show(SendEmail(username.Text, password.Password, Sender.Text, subject.Text,
textBox5.Text));
}
catch (Exception
ex)
{
MessageBox.Show(ex.Message);
}
}
Output :
I Hope this article will help for some of the basics how to send ,mail using c# application.
No comments:
Post a Comment