Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[HttpPost]
       public ActionResult Index(MailModel objModelMail, HttpPostedFileBase fileUploader)
       {
           if (ModelState.IsValid)
           {
               string from = "muthujuna@gmail.com"; //example:- sourabh9303@gmail.com
               using (MailMessage mail = new MailMessage(from, objModelMail.To))
               {
                   mail.Subject = objModelMail.Subject;
                   mail.Body = objModelMail.Body;
                   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, "XXXX");
                   smtp.UseDefaultCredentials = true;
                   smtp.Credentials = networkCredential;
                   smtp.Port = 587;
                   ParsingTemplate();
                   smtp.Send(mail);
                   ViewBag.Message = "Sent";
                   return View("Index", objModelMail);
               }
           }
           else
           {
               return View();
           }
       }


What I have tried:

hi frnds,


I am trying to send one mail with Attachment using Mvc4.but it Takes time too long to send.more than 3 minutes its loaded finally am getting Timeout Error but small size of files no issues .now i want to send asyncronously Email has to be send...please if some one know this help me or share some links how to do that please help me ,
Posted
Updated 26-Apr-16 4:11am
Comments
Sinisa Hajnal 20-Apr-16 6:13am    
Even if moved out of the thread it will timeout if you don't resolve the cause. You can use Async keyword and Task class or use BackgroundWorker to delegate sending to another thread...but you have to discover why you get slow response (in general, check proxies, firewalls and other such barriers between you and smtp server).
Member 11183217 20-Apr-16 7:14am    
hi haznal i am using thread now problem solve it didn't took the time but my Attchment file is not send after two minutes error redirect to this line smtp.send(mail) ---------------->and main thing is my file size 19.56MB we can send maximum 25Mb
Member 11183217 20-Apr-16 7:17am    
[HttpPost]
public ActionResult Index(MailModel objModelMail, HttpPostedFileBase fileUploader)
{
if (ModelState.IsValid)
{

Thread email = new Thread(delegate()
{
string from = "muthujuna@gmail.com"; //example:- sourabh9303@gmail.com
using (MailMessage mail = new MailMessage(from, objModelMail.To))
{
mail.Subject = objModelMail.Subject;
mail.Body = objModelMail.Body;
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, "jeyalakshmi1");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.Send(mail);
ViewBag.Message = "Sent";
}
});
email.IsBackground = true;
email.Start();
ParsingTemplate();
return View("Index", objModelMail);
}
else
{
return Error("");
}
}
Member 11183217 20-Apr-16 7:18am    
i have given my code which i used to send that Attachment mail
F-ES Sitecore 20-Apr-16 7:09am    
As suggested use BAckgroundWorder, however the real root of your issues is that you're sending email through gmail and you shouldn't be.

http://forums.asp.net/post/5825785.aspx

1 solution

Hello,

Hangfire, which helps you send mails on the background like a job.

Try the below link:
Sending Mail in Background with ASP.NET MVC — Hangfire 1.5 documentation[^]

Also, Quartz Enterprise Scheduler .NET | Quartz.NET Documentation[^]

You can also use SendEmailAsync()
c# - .SendMailAsync() use in MVC[^]

Thanks
 
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