Am trying to send email through godaddy workspace email account with an uploaded attachment on godaddy using asp.net mvc. The mail is sent but uploaded file is not attached properly.
At view level
<form action="@Url.Action("index")" method="post" data-form-title="CONTACT FORM" enctype="multipart/form-data">
@Html.AntiForgeryToken()
...other page inputs..
<div class=" row row-sm-offset">
<div class="col-xs-12 col-md-12">
<div class="form-group">
<label class="form-control-label" for="form1-h-name">Upload Design (Properties: Images Type - PNG, JPEG; Max Size - 500kb )</label>
@Html.TextBoxFor(x => x.Design,
new
{
@class = "form-control",
type = "file",
required = "true"
})
</div>
</div>
</div>
<div class="form-group">
<label class="form-control-label" for="form1-h-message">Enter Message (Optional)</label>
@Html.TextAreaFor(x => x.AdditionalMessage, new
{
@class = "form-control",
placeholder = "Enter additional message"
})
</div>
<div><button type="submit" class="btn btn-primary">Send Job</button></div>
</form>
The model class
public class SendPrintJob
{
other properties of the model class..
[Required]
[DataType(DataType.Date)]
public DateTime DeliveryDate { get; set; }
[Required]
public HttpPostedFileBase Design { get; set; }
public string AdditionalMessage { get; set; }
}
At the Controller level
[HttpPost, ActionName("index")][ValidateAntiForgeryToken]
public async Task<ActionResult> index_post(SendPrintJob obj)
{
papertypeList();
if (ModelState.IsValid)
{
var date = DateTime.Now;
int dateresult = DateTime.Compare(date, obj.DeliveryDate);
if (dateresult > 0)
{
FlashMessage.Danger("Delivery Date Error: Delivery date entered should be later than todays date!");
return View(obj);
}
string uploadErrorMsg = string.Empty;
string uploadedImageFormat;
if(!isValidUpload(obj.Design, out uploadErrorMsg, out uploadedImageFormat))
{
FlashMessage.Danger("Upload Error", uploadErrorMsg);
}
var body = "<p>Print Job<br/><br/>" +
"<b>SENDER DETAILS</b><br/>" +
"Name: {0}<br/>" +
"Email: {1}<br/>" +
"Phone Number: {2}<br/><br/>" +
"<b>JOB DETAILS</b><br>" +
"Delivery Date: {3}<br/>" +
"Paper Type: {4}<br/>" +
"Paper/Image Size: {5}<br/>" +
"Print Copies Required: {6}<br/>" +
"Trimming Required: {7}<br/><br/>" +
"Additional Message:<br/>" +
"{8}<br/><br/>" +
"Download email attachment for Design<br/>";
string add_Msg = null;
if (string.IsNullOrWhiteSpace(obj.AdditionalMessage))
add_Msg = "No Message!";
else
add_Msg = obj.AdditionalMessage;
var message = new MailMessage();
message.Body = string.Format(body, obj.Name, obj.email, obj.Phone,
obj.DeliveryDate.ToLongDateString(), obj.papertype, obj.paper_imagesize,
obj.numberofcopies, obj.triming, add_Msg);
message.To.Add(new MailAddress("reciepient's email address here"));
message.IsBodyHtml = true;
message.Subject = "Print job sent as at - "+date.ToLongDateString()+" / "+date.ToShortTimeString();
if(obj.Design != null)
message.Attachments.Add(new Attachment(obj.Design.InputStream, Path.GetFileName(obj.Design.FileName)));
var smtp = new SmtpClient();
await smtp.SendMailAsync(message);
FlashMessage.Confirmation("Print Job sent successfully.");
return RedirectToAction("index");
}
return View(obj);
}
The email is sent successfully but the picture attached has 0 byte(s)