Tuesday, September 3, 2013

send email with file attachement and image as body content

      


//Change this as per your Emai configuration


const string mailHost = "info@dreamit-sanjay.blogspot.com";

        const int mailPort = 25;

        const string mailUserName = "info@dreamit-sanjay.blogspot.com";


        const string mailPwd = "sdsd";

        const string supportMail = "info@dreamit-sanjay.blogspot.com";

        const string dataMail = "info@dreamit-sanjay.blogspot.com";

 

//Main Function

        public static bool Send(string From, string ToAddr, string cc, string bcc, string Body, string Subject, string Attachments, bool AddHeaderLogo, string LogoPath, bool Requiredlog, string ReportType, bool forceToSend, string senderId, bool DefMailSendToEmailData, bool ReplyTo, string ReplyToMail)

        {

            bool emailSuccess;

            emailSuccess = true;

            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

            try

            {

                //mail.Headers.Add("MIME-Version", "1.0");

                //mail.Headers.Add("Content-Type", "text/html; charset=ISO-8859-1");

                mail.To.Add(ToAddr.Replace(";", ","));

                mail.From = new System.Net.Mail.MailAddress(string.IsNullOrEmpty(From) ? supportMail : From);

                bcc = bcc.Replace(";", ",");

 

 

                if (!string.IsNullOrEmpty(bcc))

                {

                    bcc = (DefMailSendToEmailData == true) ? dataMail + "," + bcc : bcc;

                }

                else

                {

                    bcc = (DefMailSendToEmailData == true) ? dataMail : "";

                }

 

                if (!string.IsNullOrEmpty(bcc))

                {

                    mail.Bcc.Add(bcc);

                }

 

                cc = cc.Replace(";", ",");

                if (!string.IsNullOrEmpty(cc))

                {

                    mail.CC.Add(cc);

                }

                if (ReplyTo == true)

                {

                    MailAddress ma = new MailAddress(ReplyToMail);

                    mail.ReplyTo = ma;

                }

                mail.Subject = Subject;

                if (AddHeaderLogo == true)

                {

                    //Body = "<html><head></head><body><table align='left' cellpadding='0' cellspacing='0' style='width:8.27in; height:11.69in;border:solid 2px gray; padding-left:2px; padding-right:2px;'><tr><td><img src=\"cid:Pic1\">" + Body + "</td></tr></table></body></html>";

                    Body = Body.Replace("[$Logo$]", "<img src=\"cid:Pic1\">");

                    System.Net.Mail.AlternateView avHtml = AlternateView.CreateAlternateViewFromString(Body, null, MediaTypeNames.Text.Html);

                    LinkedResource pic1 = new LinkedResource(LogoPath, MediaTypeNames.Image.Jpeg);

                    pic1.ContentId = "Pic1";

                    avHtml.LinkedResources.Add(pic1);

                    mail.AlternateViews.Add(avHtml);

                }

                mail.Body = Body;

                mail.IsBodyHtml = true;

                //mail.Priority = System.Net.Mail.MailPriority.High;

                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

                client.Credentials = new System.Net.NetworkCredential(mailUserName, mailPwd);

                client.Port = mailPort; // Gmail works on this port

                client.Host = mailHost;

                client.EnableSsl = false; //Gmail works on Server Secured Layer

 

 

                if (!string.IsNullOrEmpty(Attachments))

                {

                    System.Net.Mail.Attachment a;

                    string fileName = Attachments;

                    string[] x = fileName.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < x.Length; i++)

                    {

                        a = new System.Net.Mail.Attachment(x[i]);

 

                        string result = Path.GetFileName(x[i]);

                        a.Name = result;

                        mail.Attachments.Add(a);

 

                    }

                    client.Send(mail);

 

                }

                else

                {

                    client.Send(mail);

                }

                //a.Dispose();

                if (Requiredlog == true)

                {

                   

                }

                emailSuccess = true;

            }

            catch (System.Threading.ThreadAbortException ec)

            {

                //a.Dispose();

                emailSuccess = true;

            }

            catch (Exception error)

            {

                if (_Session.UserRole != (int)_Enums.UserRole.CustCare)

                {

                    //HttpContext.Current.Response.Redirect(PageBase.homePageURL + "?msg=" + HttpContext.Current.Server.UrlEncode("Problem with sending mail"));

                    if (forceToSend == false)

                    {

                        HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString() + "?msg=" + HttpContext.Current.Server.UrlEncode("Problem with sending mail"));

                        HttpContext.Current.Response.Write("Problem with sending mail");

                    }

                }

                else

                {

                    //HttpContext.Current.Response.Redirect(PageBase.QMManageQueryURL + "?msgq=" + HttpContext.Current.Server.UrlEncode("Problem with sending mail"));

                    if (forceToSend == false)

                    {

                        HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString() + "?msgq=" + HttpContext.Current.Server.UrlEncode("Problem with sending mail"));

                        HttpContext.Current.Response.Write("Problem with sending mail");

                    }

                }

                //a.Dispose();

                Common.InsertErorLog(Common.GetCurrentPageName(), ToAddr.ToString().Trim() + Subject.ToString().Trim() + senderId.ToString().Trim() + error.ToString());

                emailSuccess = true;

            }

            finally

            {

                mail.Dispose();

            }

 

            return emailSuccess;

        }

 

Call like this :

 

Send("", "sanjay@dreamit-sanjay.blogspot.com", "Test Mail" + "<br/><b>Note :</b> Task Summary of period from previous saturday to this Friday", "Weekly Task Mail " + DateTime.Now.AddDays(-5).ToString("dd-MMM-yyyy") + "-" + DateTime.Now.ToString("dd-MMM-yyyy"), "");


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment