Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
[HttpPost]
       public ActionResult Index(MailModel objModelMail, HttpPostedFileBase fileUploader)
       {
           try
           {
               if (objModelMail.To!=null)
               {
                   Thread email = new Thread(delegate()
                   {
                       string from = "muthujuna@gmail.com"; //example:- sourabh9303@gmail.com
                       using (MailMessage mail = new MailMessage())
                       {
                           string[] ToAddress = { objModelMail.To };
                           mail.From = new MailAddress(from);
                           mail.Subject = objModelMail.Subject;
                           mail.Body = objModelMail.Body;

                           foreach (string address in ToAddress)
                           {
                               mail.To.Add(address);
                           }


                           if (fileUploader != null)
                           {
                               string fileName = Path.GetFileName(fileUploader.FileName);
                               mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                           }
                           mail.IsBodyHtml = false;
                           SmtpClient smtp = new SmtpClient();
                           smtp.Host = "smtp.gmail.com";
                           smtp.EnableSsl = true;
                           NetworkCredential networkCredential = new NetworkCredential(from, "XXXXXX");
                           smtp.UseDefaultCredentials = true;
                           smtp.Credentials = networkCredential;
                           smtp.Timeout = int.MaxValue;
                           smtp.Port = 587;
                           smtp.Send(mail);
                       }
                   });
                   email.IsBackground = true;
                   email.Start();
                   var To = objModelMail.To;
                   ParsingTemplate(To);
                   ViewBag.Success = "hi, your Mail Send SuccessFully";
               }
               else
               {
                   ViewBag.Error = "Error occured while processing your request";
               }
           }
           catch (Exception e)
           {
               ViewBag.Error = ("Could not send the e-mail - error: " + e.Message);
           }
           return View("Index", objModelMail);//I need that successful message here itself
       }

       private void ParsingTemplate(string To)
       {

           string templateFilePath = System.Web.HttpContext.Current.Server.MapPath("~/EmailTemplate/EmailTemplateTest.html");
           var templateAsString = System.IO.File.ReadAllText(templateFilePath);

           var myViewModel = new TestEmailTemplateViewModel
           {
               UserFullName = "Mohamed Nady",
               UserName = "NADY",
               SiteUrl = "http://www.mohnady.com",
               RegDate = DateTime.Now.ToString()
           };



i AM GETTING ERROR THIS LINE:
           string body = RazorEngine.Engine.Razor.RunCompile(templateAsString, "templateKey", typeof(TestEmailTemplateViewModel), myViewModel);
        

           SendEmail("muthujuna@gmail.com", To, "testEmail", body);


       }

       private void SendEmail(string from, string To, string subject, string body)
       {
           MailMessage mail = new MailMessage();
           mail.To.Add(new MailAddress(To));
           mail.From = new MailAddress(from);
           mail.Subject = subject;
           string Body = body;
           mail.Body = Body;
           mail.IsBodyHtml = true;
           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.Port = 587;
           smtp.UseDefaultCredentials = true;
           smtp.Credentials = new System.Net.NetworkCredential
           ("muthujuna@gmail.com", "XXXXX");// Enter seders User name and password
           smtp.EnableSsl = true;
           smtp.Send(mail);
       }


What I have tried:

hi,

I am Getting Error This line ,
string body = RazorEngine.Engine.Razor.RunCompile(templateAsString, "templateKey", typeof(TestEmailTemplateViewModel), myViewModel);



Actually i am trying to send the mail with one EmailTemplate but one Error has occured what my problem is

1.first mail has send when i enter ToAddress that muthujuna@gmail.com
2.if suppose i enter someother To Address Means am getting an Error which i put it Top
"The specified string is not in the form required for an e-mail"



Please help me if konow this
Posted
Updated 6-May-16 1:38am
Comments
ZurdoDev 6-May-16 7:32am    
The error is very clear. Whatever you are sending as an email address is not a valid format.
Member 11183217 6-May-16 7:36am    
Thank u thank u so much Ryan now i Cleared that Error

1 solution

As mentioned in the comments, you need to fix the code so that the email you are sending is in a valid format.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900