I want to use windows application to send email to a account (both gmail). I tried using following code :
private void button2_Click(object sender, EventArgs e)
{
try
{
using (SmtpClient client = new SmtpClient("smtp.gmail.com"))
{
MailMessage message = new MailMessage();
message.From = new MailAddress(textBox1.Text);
message.To.Add(textBox3.Text);
message.Body = textBox6.Text;
message.Subject = textBox5.Text;
client.UseDefaultCredentials = false;
client.Port = 587;
client.EnableSsl = true;
if (textBox4.Text != null)
{
message.Attachments.Add(new Attachment(textBox4.Text));
}
client.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
client.Send(message);
message = null;
MessageBox.Show("sent");
}
}
catch (SmtpFailedRecipientsException sfrEx)
{
// TODO: Handle exception
// When email could not be delivered to all receipients.
throw sfrEx;
}
catch (SmtpException sEx)
{
// TODO: Handle exception
// When SMTP Client cannot complete Send operation.
throw sEx;
}
catch (Exception ex)
{
// TODO: Handle exception
// Any exception that may occur during the send process.
throw ex;
}
}
private void button1_Click_1(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox4.Text= openFileDialog1.FileName.ToString();
}
}
But when I click button2(send) it does nothing, not even exception I don't know what is the problem.neither it throws error nor sends the mail and my firewall is off.
any help would be really appreciated, thanks