Monday 31 December 2012

Send Mail


WebConfig:

<add key="SMTPHost" value="127.0.0.1" />
<add key="SMTPPort" value="25" />
Code Behind :
string mailTo =”To”;
string mailFrom = “From”;
string Sub = “Subject”;
string strBody =”Body part”;
SendMail(mailTo, mailFrom, string.Empty, string.Empty, strBody, Sub, null);


public static void SendMail(string ToMail, string FromMail, string Cc, string Bcc, string Body, string Subject, ArrayList AttachmentFiles)
        {
            SmtpClient smtp = new SmtpClient();
            MailMessage mailmsg = new MailMessage();

            mailmsg.From = new MailAddress(FromMail);
            mailmsg.To.Add(ToMail);

            if (!string.IsNullOrEmpty(Cc))
                mailmsg.CC.Add(Cc);

            if (!string.IsNullOrEmpty(Bcc))
                mailmsg.Bcc.Add(Bcc);

            mailmsg.Body = Body;
            mailmsg.Subject = Subject;
            mailmsg.IsBodyHtml = true;

            mailmsg.Priority = MailPriority.High;

            smtp.Host = Convert.ToString(ConfigurationManager.AppSettings["SMTPHost"]);
            smtp.Port = Convert.ToInt32(Convert.ToString(ConfigurationManager.AppSettings["SMTPPort"]));

            if (AttachmentFiles != null)
            {
                for (int counter = 0; counter < AttachmentFiles.Count; counter++)
                {
                    if (System.IO.File.Exists(Convert.ToString(AttachmentFiles[counter])))
                    {
                        System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(Convert.ToString(AttachmentFiles[counter]));
                        mailmsg.Attachments.Add(attach);
                    }
                }
            }

            try
            {
                smtp.Send(mailmsg);
                mailmsg.Dispose();
            }
            catch { }
        }


Note: 
Must add SMTPHost Ip Address into IIS




No comments:

Post a Comment