Controller Code:
public ActionResult SendEmailWithAttachement(HttpPostedFileBase fileUploader)
{
var senderEmail = new MailAddress("donotreplygkce@gmail.com", "AlumniJobPortal");
var receiverEmail = new MailAddress("rupesh.konduru@gmail.com", "Receiver");
var body = "";
using (StreamReader reader = new StreamReader(Server.MapPath("~/Content/StudentJobApply.html")))
{
body = reader.ReadToEnd();
}
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("donotreplygkce@gmail.com", "AlumniJobPortal");
mail.To.Add("rupesh.konduru@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
mail.IsBodyHtml = true;
string fileName = Path.GetFileName(fileUploader.FileName);
mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("donotreplygkce@gmail.com", "password"),
};
smtp.Send(mail);
return RedirectToAction("Index");
}
View Code:
@using (@Html.BeginForm("SendEmailWithAttachement", "Student", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
{
@Html.ValidationSummary()
public ActionResult SendEmailWithAttachement(HttpPostedFileBase fileUploader)
{
var senderEmail = new MailAddress("donotreplygkce@gmail.com", "AlumniJobPortal");
var receiverEmail = new MailAddress("rupesh.konduru@gmail.com", "Receiver");
var body = "";
using (StreamReader reader = new StreamReader(Server.MapPath("~/Content/StudentJobApply.html")))
{
body = reader.ReadToEnd();
}
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("donotreplygkce@gmail.com", "AlumniJobPortal");
mail.To.Add("rupesh.konduru@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
mail.IsBodyHtml = true;
string fileName = Path.GetFileName(fileUploader.FileName);
mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("donotreplygkce@gmail.com", "password"),
};
smtp.Send(mail);
return RedirectToAction("Index");
}
View Code:
@using (@Html.BeginForm("SendEmailWithAttachement", "Student", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
{
@Html.ValidationSummary()
}
No comments:
Post a Comment