@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

June 13, 2010 comment ,

Send E-Mail from .NET application

.NET 2.0 has included System.Net.Mail code namespace for supporting email programming with .NET

Following is the code snippets for how to send mail from .NET application.

MailMessage mail = new MailMessage();
mail.To.Add("email1@email1.com");
mail.To.Add("email2@email2.com");
// you can add even add more to address in the same way.
 
mail.From = new MailAddress("from email address");
// enter the email address. this address will show in the from field of recipient.
 
mail.Subject = "Email using .NET";
string Body = "Hi, this mail is to test sending mail";
mail.Body = Body;
mail.IsBodyHtml = true;
 
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtpserveraddress"; //Or Your SMTP Server Address
// enter you smtp server name.
 
smtp.Credentials = new System.Net.NetworkCredential("smtp username", "smtp password");
//Or your Smtp Email ID and Password
 
smtp.EnableSsl = true;
// if required 
 
smtp.Send(mail);

June 8, 2010 comment

Bind an Enumeration in C#

The GetValues method of Enum returns an array of objects. We can use this method to convert an enum to an array and bind to a ComboBox in Windows Forms using DataSource property.

The following code snippet binds ButtonState enum available in Windows Forms to a ComboBox.

comboBox1.DataSource = System.Enum.GetValues(typeof(ButtonState));

Now, once we have an enum bound to a ComboBox, we need to the selected item from ComboBox and convert back to the enum. Unfortunately, ComboBox selected item gives us a string. Now we will have to convert back to the string value to an enum value.
For that, we can use Enum.Parse method and cast it to the enumeration.

The following code snippet takes the selected value in a ComboBox and converts it back to the enum value.

MessageBox.Show(comboBox1.Items[comboBox1.SelectedIndex].ToString()); 
ButtonState currentState = (ButtonState)Enum.Parse(typeof(ButtonState), comboBox1.Items[comboBox1.SelectedIndex].ToString());

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading