Some good video websites to catch with Microsoft technology
Although all the videos are from last year , but they are still good and if you missed the teched you can catch up there.
http://www.microsoft.com/emea/msdn/spotlight/
Although all the videos are from last year , but they are still good and if you missed the teched you can catch up there.
http://www.microsoft.com/emea/msdn/spotlight/
Namespaces
The following Namespaces need to be imported in order to send emails
using System.Net;
using System.Net.Mail;
MailMessage Class Properties
Following are the required properties of the mail message class.
From – Sender’s email address
To – Recipient(s) Email Address
CC – Carbon Copies
BCC – Blind Carbon Copies
Subject – Subject of the Email
Body – Body of the Email
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments if Any.
ReplyTo – ReplyTo Email address.
Creating a MailMessage Object
MailMessage mm = new MailMessage();
mm.From = new MailAddress(“username@gmail.com”);
mm.Subject = “Hello”;
mm.Body = “<p>Body</p>”;
mm.IsBodyHtml = true;
SMTP Class Properties
Following are the properties of the SMTP class.
Host – Your SMTP Server
EnableSsl – Specify whether you host accepts SSL Connections.
UseDefaultCredentials – Set to True inorder to allow authentication based on the Credentials of the Account used to send emails
Credentials – Valid login credentials for the SMTP server
Port – Port Number of the SMTP server
Below code describes how the above properties are applied
smtp.Host = “smtp.gmail.com”;
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = “username@gmail.com”;
NetworkCred.Password = “xxxxxxxxx”;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
Send Email
The method Smtp.Send(..) is used to send the email. See the example below
smtp.Send(mm);
MessageBox.Show(“Message sent successfully.”,”Success”, MessageBoxButtons.OK,MessageBoxIcon.Information);
Send Email Asynchronously
//Create event handler
void sc_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
Console.WriteLine(“Message cancelled”);
else if (e.Error != null)
Console.WriteLine(“Error: ” + e.Error.ToString());
else
Console.WriteLine(“Message sent”);
}
//Add event handler
smtp.SendCompleted += new SendCompletedEventHandler(sc_SendCompleted);
smtp.SendAsync(mm , null);
//Or Cancel the transmtion
smtp.SendAsyncCancel();
Send Email using Background Thread
Sending Email requires some amount of time. So many times user has to wait for the page to load while the mail is being sent. And if the server is not reachable the code will try to send email until timeout.
To avoid that the email can be sent using a background thread and the user can be displayed a message immediately without delay.
Import the following Namespace
using System.Threading;
Send email using a Background thread
Thread threadSendMails;
threadSendMails = new Thread(delegate()
{
sendemail(“username@gmail.com”, “receiver@abc.com”, “Hello”, “<p>Body</p>”, “C:\\MyDoc.txt”, true);
});
threadSendMails.IsBackground = true;
threadSendMails.Start();