When, Where & Why XML used


There are lots of places we can use XML

1. Where we need a place to hold some data, can read / write / manipulate without having any database. Even we can use textfile, but XML is more flexible – say which field, what is the data type, etc., if we use text file, we need to predefine what is the sequence of each column and what dataype etc.,

2. Where we need to transfer some data from one pace to another like one layer to another, one tier to another etc., – we can serialize the xml and deserialize it in destination place..,

3. Where we need a light weight file, and flexible to handle almost like a database …..

4. XML is not languages specific, can read in any program language like .NET, Java, VB, Javascript etc.,

Advertisements

Sending Emails from C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string SmtpServer = ConfigurationManager.AppSettings[“App_Mail_Server”]; // Provide the SMTP server name
            string strFrom = “abc@domain.com”;
            string strTo = “cde@domain.com“;  // If no mail id required, give blank
            string strCC = “afc@domain.com“;  // If no mail id required, give blank
            string strBCC = “sdf@domain.com“;     // If no mail id required, give blank
            string strPeriority = “High”;
            string strAttachment = @”C:\Test.txt,D:\Test1.txt”;
            string strSubject = “Test Mail with Multiple Attachment”;
            string strBody = “Test Mail with Multiple Attachment”;
            bool IsBodyIsInHTML = false;

            try
            {
                SmtpClient smtp = new SmtpClient(SmtpServer);
                MailMessage mssg = new MailMessage();
                mssg.From = new MailAddress(strFrom);
                mssg.Subject = strSubject;
                mssg.Body = strBody;

                //Adding multiple To Addresses
                if (strTo.Trim().Length > 0)
                {
                    foreach (string sTo in strTo.Split(“,”.ToCharArray()))
                        mssg.To.Add(sTo);
                }

                //Adding multiple CC Addresses
                if (strCC.Trim().Length > 0)
                {
                    foreach (string sCC in strCC.Split(“,”.ToCharArray()))
                        mssg.CC.Add(sCC);
                }

                //Adding multiple BCC Addresses
                if (strBCC.Trim().Length > 0)
                {
                    foreach (string sBCC in strBCC.Split(“,”.ToCharArray()))
                        mssg.Bcc.Add(sBCC);
                }

                //Adding multiple BCC Addresses
                if (strAttachment.Trim().Length > 0)
                {
                    foreach (string sAttachment in strAttachment.Split(“,”.ToCharArray()))
                    {
                        Attachment attachment = new Attachment(sAttachment);
                        mssg.Attachments.Add(attachment);
                    }
                }

                MailPriority mailPeriority = MailPriority.Normal;
                switch (strPeriority)
                {
                    case “High”:
                        mailPeriority = MailPriority.High;
                        break;
                    case “Low”:
                        mailPeriority = MailPriority.Low;
                        break;
                    case “Normal”:
                        mailPeriority = MailPriority.Normal;
                        break;
                }
                mssg.Priority = mailPeriority;

                mssg.BodyEncoding = System.Text.Encoding.Default;
                mssg.SubjectEncoding = System.Text.Encoding.Default;
                mssg.IsBodyHtml = IsBodyIsInHTML;
                smtp.Send(mssg);
            }
            catch (Exception ex)
            {
                //trach the exception and write in the log. might be Main Addresses contains non mail id formats.
            }

        }
    }
}