Wednesday, September 15, 2010

C# Tutorial: Mail Sender [Send e-mails using C#]

Here is the code

///////////////// Button Send /////////////////
MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
// SmtpServer = smtp.company.com; Ex: Gmail - smtp.gmail.com | Yahoo - smtp.yahoo.com
SmtpClient client = new SmtpClient(smtp.Text);
// Port 587 is the preferred port for mail submission. Port 25 is widely abused by malware to spread worms and spam.
//  As a result, many ISPs are restricting its use. I tried to use it but it didn't work!
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
// Port 587 allows you to use a SSL Connection.
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);

Or download the solution

2 comments: