Hi mahesh213,
Check this example. Now please take its reference and correct your code.
Using the below article i have modified the code to send Excel as attachment.
Namespaces
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web.Mvc;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult getAll()
{
return Json(GetEmployees(), JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult GenerateReport(int eId)
{
string html = "";
Employee employee = GetEmployees().Where(x => x.EId == eId).FirstOrDefault();
// Company Logo and Name.
html += "<table>" +
"<tr><td colspan='2'><img src='" + GetUrl("Files/Logo.png") + "' height='50px' width='100px' /></td></tr>" +
"<tr><td colspan='2'> </td></tr>" +
"<tr><td colspan='2'> </td></tr>" +
"<tr><td colspan='2'> </td></tr>" +
"<tr><td colspan='2'> </td></tr>" +
"<tr><td colspan='2'> </td></tr>" +
"<tr><td>Company : </td><td><b>Excelasoft Solutions</b></td></tr>" +
"<tr><td colspan='2'> </td></tr></table>";
// Employee Data.
html += "<table border='1' cellpadding='0' cellspacing='0' width='100%'><tr>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"</tr><tr>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"</tr></table>";
byte[] bytes = Encoding.ASCII.GetBytes(html);
// Send Email with Excel as Attachment.
MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
mm.Subject = "Employee Report";
mm.Body = "Employee Report Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), employee.EName.Trim().Replace(" ", "_") + ".xls"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return new JsonResult() { Data = new { Message = "Email Sent." } };
}
private string GetUrl(string imagePath)
{
string appUrl = System.Web.HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
{
appUrl = "/" + appUrl;
}
string url = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, appUrl);
return url + imagePath;
}
private List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { EId = 1, EName = "Maria", Gender = "Female", PNo = "030-0074321", Location = "Austria", Company = "Alfreds Futterkiste" });
employees.Add(new Employee { EId = 2, EName = "Antonio Moreno", Gender = "Male", PNo = "(5) 555-3932", Location = "Brazil", Company = "Antonio Moreno Taquería" });
employees.Add(new Employee { EId = 3, EName = "Thomas Hardy ", Gender = "Male", PNo = "(5) 555-3932", Location = "Ireland", Company = "Around the Horn" });
return employees;
}
public class Employee
{
public int EId { get; set; }
public string EName { get; set; }
public string Gender { get; set; }
public string PNo { get; set; }
public string Location { get; set; }
public string Company { get; set; }
}
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', ['$scope', '$http', function ($scope, $http, $window) {
$http({
method: 'GET',
url: '/Home/getAll/'
}).success(function (data) {
$scope.items = data;
});
$scope.GenerateReport = function (id) {
$http({
method: 'POST',
url: '/Home/GenerateReport/',
params: { eId: id }
}).success(function (data) {
$window.alert(data.Message);
});
};
} ]);
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<div class="container" id="printarea">
<table class="table table-bordered">
<tr class="success">
<th>EId</th>
<th>EName</th>
<th>Gender</th>
<th>PNo</th>
<th>Location</th>
<th>Company</th>
<th>Ation</th>
</tr>
<tr ng-repeat="item in items">
<td>{{item.EId}}</td>
<td>{{item.EName}}</td>
<td>{{item.Gender}}</td>
<td>{{item.PNo}}</td>
<td>{{item.Location}}</td>
<td>{{item.Company}}</td>
<td>
<button class="btn btn-success btn-sm" ng-click="GenerateReport(item.EId)">
<span class="glyphicon glyphicon-print"></span> PDF
</button>
</td>
</tr>
</table>
</div>
</body>
</html>