Saturday 19 July 2014

Sending an E-Mail With Attachment in MVC with asp.Net





  In the previous Example we already  Explain How to Send mail in Asp.Net .but in this Example we can explain how to send  mail with Atteched file or file Attechment facility in email in mvc4.  we ca also Atteched the word file,Excel file,image File or any other type of File with an Email in asp.net with mvc.

in which Example you can send Email with an attechment using SMTP server. the System.Web.Mail Namespace Provide classes MailMessage are used for sending Email and Mailattechment class are used for manage the Attechment of the File.

we provide same facility as provided by Gmail you can easily upload any type of one or more file with mail and send to the other user in by using it's email id.

sorting data in gridview with up and down arrow sorting data with up and down arrow in gridview

Ajax Autocomplete Textbox search example autocomplete textbox search in asp.net


You have to add the Foolowing Namespace to Send mail to user like

using System.Net.Mail;

Here we Define Simple Code that How to Attach File in Email

MailMessage mail = new MailMessage();
//Attach file using FileUpload Control and put the file in memory stream
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(newAttachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }


You Have Require SMTP Email Configuration before sendind Email to other user so to configure the Setting please Click Here this post will Explain that in briefly.

to send mail first create model class in a model folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SendMailwithAttachment.Models
{
    public class MailModel
    {
        public string To { getset; }
        public string Subject { getset; }
        public string Body { getset; }
    }
}


SendMailcontroller.cs:-

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;

namespace SendMailwithAttachment.Controllers
{
    public class SendMailerController : Controller
    {
        

        public ActionResult Index()
        {
            return View();
        }

        
[HttpPost]
        public ActionResult Index(SendMailwithAttachment.Models.MailModel objModelMail, HttpPostedFileBasefileUploader)
        {
            if (ModelState.IsValid)
            {
                string from = "Your Gmail Id"; //example:- kiritpatel@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, "Your Gmail Password");
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCredential;
                    smtp.Port = 587;
                    smtp.Send(mail);
                    ViewBag.Message = "Sent";
                    return View("Index", objModelMail);
                }
            }
            else
            {
                return View();
            }
        }
    }
}


Index.cshtml
@model SendMailwithAttachment.Models.MailModel
@{
    ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script>
    $(document).ready(function () {
        if ('@ViewBag.Message' == 'Sent') {
            alert('Mail has been sent successfully');
        }
    });
</script>
<h2>Index</h2>
<fieldset>
    <legend>Send Email
    </legend> 
    @using (@Html.BeginForm("Index""SendMailer"FormMethod.Post, new { @id = "form1", @enctype ="multipart/form-data" }))
    {
        @Html.ValidationSummary()
        <input type="submit" value="Send" />
    
        <table>
            <tr>
                <td>To:
                </td>
                <td>
                    @Html.TextBoxFor(m => m.To)
                </td>
            </tr>
            <tr>
                <td>Subject:
                </td>
                <td>
                    @Html.TextBoxFor(m => m.Subject)
                </td>
            </tr>
            <tr>
                <td>Attachment
                </td>
                <td>
                    <input type="file" name="fileUploader" />
                </td>
            </tr>
            <tr>
                <td>Body:
                </td>
                <td>
                    @Html.TextAreaFor(m => m.Body)
                </td>
            </tr> 
        </table>    
    }
</fieldset>

0 comments:

Post a Comment