Monday, June 30, 2008

ASCII Table

ASCII table is needed for processing character based operations in any programming language.

ASCII - American Standard Code for Information Interchange is a character encoding based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that work with text.  

You can find in the below screen shots the decimal values and its equivalent hexadecimal values with character codes.

1) Non Printable ASCII Characters
2) Printable ASCII Characters
3) Extended ASCII Characters
 

























































































Wednesday, June 4, 2008

Send Email using C#.NET (Visual Studio 2005)

Sending email is easy and simple by using the various classes in .NET Framework. But if you miss the right combinations, it will give you some headaches as well. Here I will just list the statements which will help you atleast test sending an email, then you can explore various option and combinations by yourself.

Here I will explan with sample code on how to send email using C# in Windows application (VS 2005).

If we are using the SMTP Server of Gmail for example, use the following code. But if you keep on sending emails continuously using smtp.gmail.com, chances are that your login will be restricted with some picture validation, which will result in failure of sending email using program. If you intent to use email feature of C# for commercial applications, it is always better to use a valid/paid email service.

The namespaces used

  • using System.Net;

  • using System.Net.Mail;

  • using System.Net.Mime;

The classes involved

SmtpClient - Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP)
MailMessage- Represents an email message that can be sent using the System.Net.Mail.SmtpClient class
MailAddress- Represents the address of an electronic mail sender or recipient
NetworkCredential- Provides credentials for password-based authentication schemes

Example Code:
Case 1:
If you use gmail or similar kind of smtp servers
//Code Statements//replace xyz@xyz.com,zzz@xyz.com & xxxxx with actual values.

try
{
//The From address (Email ID)
string str_from_address = "xyz@xyz.com";

//The Display Name
string str_name= "Test Mail";

//The To address (Email ID)
string str_to_address = "zzz@xyz.com";

//Create MailMessage Object
MailMessage email_msg = new MailMessage();

//Specifying From,Sender & Reply to address
email_msg.From = new MailAddress(str_from_address, str_name);
email_msg.Sender = new MailAddress(str_from_address, str_name);
email_msg.ReplyTo = new MailAddress(str_from_address, str_name);

//The To Email id
email_msg.To.Add(str_to_address);

email_msg.Subject = "My Subject";//Subject of email

email_msg.Body = "This is the body of this message";

//Create an object for SmtpClient class
SmtpClient mail_client = new SmtpClient();

//Providing Credentials (Username & password)
NetworkCredential network_cdr = new NetworkCredential();
network_cdr.UserName = str_from_address;
network_cdr.Password = "xxxxx";

mail_client.Credentials = network_cdr;

//Specify the SMTP Port
mail_client.Port = 587;

//Specify the name/IP address of Host
mail_client.Host = "smtp.gmail.com";

//Uses Secure Sockets Layer(SSL) to encrypt the connection
mail_client.EnableSsl = true;

//Now Send the message
mail_client.Send(email_msg);

MessageBox.Show("Email Sent Successfully");
}
catch (Exception ex)
{
//Some error occured
MessageBox.Show(ex.Message.ToString());
}


Case 2
If we are using some business email/ paid emails, we need to use only basic authentication, providing the username and password will be enough to send the email. This block of statements will do the task.



//Code Statements
//replace xyz@xyz.com,zzz@xyz.com & xxxxx with actual values.
//smtp.yyyy.com should be replaced with smtp host of your email service
//UseDefaultCredentials is set to false
//Also EnableSsl property is skipped(If your smtp server supports, you can include it)

try
{
string str_from_address = "xyz@xyz.com"; //The From address (Email ID)
string str_name= "Test Mail"; //The Display Name
string str_to_address = "zzz@xyz.com"; //The To address (Email ID)

//Create MailMessage Object
MailMessage email_msg = new MailMessage();

//Specifying From,Sender & Reply to address
email_msg.From = new MailAddress(str_from_address, str_name);
email_msg.Sender = new MailAddress(str_from_address, str_name);
email_msg.ReplyTo = new MailAddress(str_from_address, str_name);

//The To Email id
email_msg.To.Add(str_to_address);

email_msg.Subject = "My Subject";//Subject of email
email_msg.Body = "This is the body of this message";
email_msg.Priority = MailPriority.Normal;

//Create an object for SmtpClient class
SmtpClient mail_client = new SmtpClient();

//Providing Credentials (Username & password)
NetworkCredential network_cdr = new NetworkCredential();
network_cdr.UserName = str_from_address;
network_cdr.Password = "xxxxx";

mail_client.Host = "smtp.yyyy.com"; //SMTP host
mail_client.UseDefaultCredentials = false;
mail_client.Credentials = network_cdr;

//Now Send the message
mail_client.Send(email_msg);

MessageBox.Show("Email Sent Successfully");

}
catch (Exception ex)
{
//Some error occured
MessageBox.Show(ex.Message.ToString());
}

//Happy Sending email :)