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
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
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());
}
//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 :)