I am working on a image sharing Web App that goes something like this:
1) The user can select from different categories (A, B, C, D)
2) Once the category is selected the user is redirected to a different page that will list all images from that category (The images are to my localdb)
3) Here I want to add the image, description and a button. The button needs to select the image to be attached to the email. What is left is to fill the To, Subject and Body parts of the email. This is my problem. I don't know how to automatically attach that image to the email.
4) Send the email
So far, I have this:
Model:
public class MailModel
{
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
Controller:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ViewResult Index(NASAWebAppDemo.Models.MailModel _objModelMail, HttpPostedFileBase fileUploader)
{
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.To.Add(_objModelMail.To);
mail.From = new MailAddress(_objModelMail.From);
mail.Subject = _objModelMail.Subject;
string Body = _objModelMail.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("something@gmail.com", "MyPass");
smtp.EnableSsl = true;
smtp.Send(mail);
return View("Index", _objModelMail);
}
else
{
return View();
}
And the View:
@model NASAWebAppDemo.Models.MailModel
@{
ViewBag.Title = "Email";
}
<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>
<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>
<tbody>
<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 name="fileUploader" type="file" /></td>
</tr>
<tr>
<td>Body:</td>
<td>@Html.TextAreaFor(m => m.Body)</td>
</tr>
</tbody>
</table>