Get image from Entity Framework database and send to email in ASP NET MVC

Last Reply on Mar 16, 2016 02:41 AM By Shashikant

Posted on Mar 14, 2016 10:04 AM

Can you help me how to get the image from database and send to email using asp net mvc. I have two models now, one for Images and one for EmailForm. How to connect them in one view, and what do i need to have in controller? 

Posted on Mar 15, 2016 09:42 AM

Here I have created sample that will help you out.

Index.aspx(View)

<table>
    <% foreach (var item in Model)
        { %>
    <tr>
        <td>
            <img src='<%: String.Format("data:{0};base64,{1}",item.ContentType,item.Base64String)  %>'
                alt="" style="width: 100px; height: 100px;" />
            <%: Html.Hidden("Id",item.Id)%>
            <%: Html.Hidden("ContentType", item.ContentType)%>
            <br />
            <%: Html.ActionLink("Send Email", "Contact", new { id=item.Id })%>
        </td>
    </tr>
    <% } %>
</table>

ImageController(Contoller)

[HttpGet]
public ActionResult Index()
{
    List<Image> images = new List<Image>();
    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 => 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 = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
        var message = new MailMessage();
        message.To.Add(model.FromEmail);
        message.Subject = "Your email subject";
        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("Sent");
        }
    }
    return View(model);
}

Contact.aspx(View)

<% using (Html.BeginForm())
    {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>Fields</legend>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.FromEmail) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.FromEmail) %>
        <%: Html.ValidationMessageFor(model => model.FromEmail) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Message) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Message) %>
        <%: Html.ValidationMessageFor(model => model.Message) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.FromName) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.FromName) %>
        <%: Html.ValidationMessageFor(model => model.FromName) %>
    </div>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Base64String) %>
    </div>
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.Base64String) %>
        <%: Html.ValidationMessageFor(model => model.Base64String) %>
    </div>
    <div>
        <%:Html.HiddenFor(model=>model.Name) %>
    </div>
    <p>
        <input type="submit" value="Send" />
    </p>
</fieldset>
<% } %>

Image Class(Model)(MetaData For Your Entity Class)

[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; }
}

 EmailFromModel(Model)

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; }
}

Screenshot


Posted on Mar 16, 2016 02:32 AM

Hi,

FetchImageUsingEntity.Models.Images is partial entity class


Posted on Mar 16, 2016 02:41 AM

Hi,

Refer below screenshot,here Images is Entity

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html