using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Public_Uber.Models;
namespace Public_Uber.Controllers
{
public class SendMailerController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Public_Uber.Models.MailModel objModelMail, HttpPostedFileBase fileUploader)
{
if (ModelState.IsValid)
{
string from = "sthehfessor95@gmail.com";
using (MailMessage mail = new MailMessage(from, objModelMail.To))
{
ExpandedUserDTO ob = new ExpandedUserDTO();
objModelMail.Body= "Your Username is:"+ob.UserName + "& Password:" + ob.Password;
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, "Sithembiso95@");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.Send(mail);
ViewBag.Message = "Sent";
TempData["Message"] = "<script>alert('Email Successfully Sent! ');</script>";
return View("Index", objModelMail);
}
}
else
{
return View();
}
}
}
}