ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2017 www.aspforums.com. All rights reserved.Get image from Entity Framework database and send to email in ASP NET MVC<p>Here I have created sample that will help you out.</p> <p><span style="text-decoration: underline;">Index.aspx(View)</span></p> <pre class="brush: html">&lt;table&gt; &lt;% foreach (var item in Model) { %&gt; &lt;tr&gt; &lt;td&gt; &lt;img src=&#39;&lt;%: String.Format(&#34;data:{0};base64,{1}&#34;,item.ContentType,item.Base64String) %&gt;&#39; alt=&#34;&#34; style=&#34;width: 100px; height: 100px;&#34; /&gt; &lt;%: Html.Hidden(&#34;Id&#34;,item.Id)%&gt; &lt;%: Html.Hidden(&#34;ContentType&#34;, item.ContentType)%&gt; &lt;br /&gt; &lt;%: Html.ActionLink(&#34;Send Email&#34;, &#34;Contact&#34;, new { id=item.Id })%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% } %&gt; &lt;/table&gt;</pre> <p><span style="text-decoration: underline;">ImageController(Contoller)</span></p> <pre class="brush: csharp">[HttpGet] public ActionResult Index() { List&lt;Image&gt; images = new List&lt;Image&gt;(); using (ImageContext context = new ImageContext()) { foreach (Images item in context.Images) { images.Add(new Image(item)); } } return View(images); } [HttpGet] public ActionResult Contact(int id) { Images image = new Images(); using (ImageContext context = new ImageContext()) { image = context.Images.Single(x =&gt; x.Id == id); } EmailFormModel email = new EmailFormModel { Base64String = Convert.ToBase64String(image.Data), ContentType = image.ContentType, Name = image.Name, }; return View(email); } [HttpPost] public ActionResult Contact(EmailFormModel model) { if (ModelState.IsValid) { var body = &#34;&lt;p&gt;Email From: {0} ({1})&lt;/p&gt;&lt;p&gt;Message:&lt;/p&gt;&lt;p&gt;{2}&lt;/p&gt;&#34;; var message = new MailMessage(); message.To.Add(model.FromEmail); message.Subject = &#34;Your email subject&#34;; message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message); message.IsBodyHtml = true; string base64 = model.Base64String; byte[] imagebytes = Convert.FromBase64String(base64); message.Attachments.Add(new Attachment(new MemoryStream(imagebytes), model.Name)); using (var smtp = new SmtpClient()) { smtp.SendAsync(message, null); return RedirectToAction(&#34;Sent&#34;); } } return View(model); }</pre> <p><span style="text-decoration: underline;">Contact.aspx(View)</span></p> <pre class="brush: html">&lt;% using (Html.BeginForm()) &nbsp;&nbsp;&nbsp; {%&gt; &lt;%: Html.ValidationSummary(true) %&gt; &lt;fieldset&gt; &nbsp;&nbsp;&nbsp; &lt;legend&gt;Fields&lt;/legend&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-label"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.LabelFor(model =&gt; model.FromEmail) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-field"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.TextBoxFor(model =&gt; model.FromEmail) %&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.ValidationMessageFor(model =&gt; model.FromEmail) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-label"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.LabelFor(model =&gt; model.Message) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-field"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.TextBoxFor(model =&gt; model.Message) %&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.ValidationMessageFor(model =&gt; model.Message) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-label"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.LabelFor(model =&gt; model.FromName) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-field"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.TextBoxFor(model =&gt; model.FromName) %&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.ValidationMessageFor(model =&gt; model.FromName) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-label"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.LabelFor(model =&gt; model.Base64String) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div class="editor-field"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.TextBoxFor(model =&gt; model.Base64String) %&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%: Html.ValidationMessageFor(model =&gt; model.Base64String) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;div&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%:Html.HiddenFor(model=&gt;model.Name) %&gt; &nbsp;&nbsp;&nbsp; &lt;/div&gt; &nbsp;&nbsp;&nbsp; &lt;p&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type="submit" value="Send" /&gt; &nbsp;&nbsp;&nbsp; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt;</pre> <p><span style="text-decoration: underline;">Image Class(Model)</span>(MetaData For Your Entity Class)</p> <pre class="brush: csharp">[MetadataType(typeof(FetchImageUsingEntity.Models.Images))] public partial class Image { public Image() { } public Image(Images baseImages) { if (baseImages != null) { this.Id = baseImages.Id; this.Name = baseImages.Name; this.ContentType = baseImages.ContentType; this.Data = baseImages.Data; this.Base64String = Convert.ToBase64String(baseImages.Data); } } public int Id { get; set; } public string Name { get; set; } public string ContentType { get; set; } public byte[] Data { get; set; } public string Base64String { get; set; } }</pre> <p>&nbsp;EmailFromModel(Model)</p> <pre class="brush: csharp">public class EmailFormModel { [Required] public string FromEmail { get; set; } [Required] public string Message { get; set; } [Required] public string FromName { get; set; } public string Base64String { get; set; } public string Name { get; set; } public string ContentType { get; set; } }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><span style="text-decoration: underline;"><img src="http://i988.photobucket.com/albums/af3/mudassarkhan/SendEmailGallery.gif" alt="" width="461" height="287" /></span></p> <p>&nbsp;</p>http://www.aspforums.net/Threads/594197/Get-image-from-Entity-Framework-database-and-send-to-email-in-ASP-NET-MVC/http://www.aspforums.net/Threads/594197/Get-image-from-Entity-Framework-database-and-send-to-email-in-ASP-NET-MVC/Tue, 15 Mar 2016 09:42:50 GMTGet image from Entity Framework database and send to email in ASP NET MVC<p>Hi,</p> <p>FetchImageUsingEntity.Models.Images is partial entity class</p>http://www.aspforums.net/Threads/594197/Get-image-from-Entity-Framework-database-and-send-to-email-in-ASP-NET-MVC/http://www.aspforums.net/Threads/594197/Get-image-from-Entity-Framework-database-and-send-to-email-in-ASP-NET-MVC/Wed, 16 Mar 2016 02:32:08 GMTGet image from Entity Framework database and send to email in ASP NET MVC<p>Hi,</p> <p>Refer below screenshot,here Images is Entity</p> <p><img src="http://i988.photobucket.com/albums/af3/mudassarkhan/2016-03-16_140237.png" alt="" width="249" height="233" /></p>http://www.aspforums.net/Threads/594197/Get-image-from-Entity-Framework-database-and-send-to-email-in-ASP-NET-MVC/http://www.aspforums.net/Threads/594197/Get-image-from-Entity-Framework-database-and-send-to-email-in-ASP-NET-MVC/Wed, 16 Mar 2016 02:41:04 GMT