Journal Trends
Journal Trends

Reputation: 59

image file not pass to controller show null value MVC

I want to send an email with a file attachment. when I client send and use the break to check what is the data I am receiving then image file show null in the controller. anyone tells me where is my mistake and what is the problem in my code.so i am sharing controller code ,model and HTML kindly review my code and tell me what is the problem in my code.

Controller

  [HttpPost]
        public ActionResult Index(EmployeeModel obj)
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("[email protected]", "******");
            MailMessage msgobj = new MailMessage();
            msgobj.To.Add(obj.ToEmail);
            msgobj.From = new MailAddress("[email protected]");
            msgobj.Body = obj.EMailBody;
            msgobj.Subject = obj.EmailSubject;
            msgobj.CC.Add(obj.EmailCC);
            msgobj.Bcc.Add(obj.EmailBCC);

            if(obj.imageFile !=null)
            {
                msgobj.Attachments.Add(new Attachment(obj.imageFile.InputStream.ToString(), obj.imageFile.FileName));
            }

            client.Send(msgobj);
            ViewBag.Success = "Email Send Successfully";




            return View();
        }

Model:

[DataType(DataType.EmailAddress),Display(Name = "TO")]
        [Required]
        [Key]
        public string ToEmail { get; set; }

        [DataType(DataType.MultilineText)]
        [Display(Name ="Body")]
        [Required]
        public string EMailBody { get; set; }

        [Display(Name ="Subject")]
        [Required]
        public string EmailSubject { get; set; }

        [Display(Name ="CC")]
        [DataType(DataType.EmailAddress)]
        public string EmailCC { get; set; }
        [Display(Name ="BCC")]
        [DataType(DataType.EmailAddress)]
        public string EmailBCC { get; set; }

        public HttpPostedFileWrapper imageFile { get; set; }
        public string imageUrl { get; set; }

HTML

 @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Employee Model</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ToEmail, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ToEmail, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ToEmail, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EMailBody, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMailBody, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMailBody, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailSubject, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailSubject, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailSubject, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailCC, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailCC, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailCC, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailBCC, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailBCC, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailBCC, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.imageFile, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" id="imageFile" name="imageFile" accept="image/jpeg, image/png" />

            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Send" class="btn btn-default" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10 text-success">
                @ViewBag.Status
            </div>
        </div>
    </div>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

Upvotes: 0

Views: 755

Answers (2)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

First, you should modify this property:

public HttpPostedFileWrapper imageFile { get; set; }

to this one:

public HttpPostedFileBase imageFile { get; set; }

Second, add enctype="multipart/form-data to the BeginForm helper:

@using (Html.BeginForm("Index", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   // form contents
}

Also you're using Gmail SMTP engine but your network credential setting seem to use Hotmail, both of them have different settings. Here are examples of correct settings:

Gmail SMTP

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("[email protected]", "******");
MailMessage msgobj = new MailMessage();
msgobj.To.Add(obj.ToEmail);
msgobj.From = new MailAddress("[email protected]");
msgobj.Body = obj.EMailBody;
msgobj.Subject = obj.EmailSubject;
msgobj.CC.Add(obj.EmailCC);
msgobj.Bcc.Add(obj.EmailBCC);

if (obj.imageFile != null && obj.imageFile.ContentLength > 0)
{
    msgobj.Attachments.Add(new Attachment(obj.imageFile.InputStream, obj.imageFile.FileName));
}

client.Send(msgobj);

Hotmail SMTP

SmtpClient client = new SmtpClient("smtp.live.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("[email protected]", "******");
MailMessage msgobj = new MailMessage();
msgobj.To.Add(obj.ToEmail);
msgobj.From = new MailAddress("[email protected]");
msgobj.Body = obj.EMailBody;
msgobj.Subject = obj.EmailSubject;
msgobj.CC.Add(obj.EmailCC);
msgobj.Bcc.Add(obj.EmailBCC);

if (obj.imageFile != null && obj.imageFile.ContentLength > 0)
{
    msgobj.Attachments.Add(new Attachment(obj.imageFile.InputStream, obj.imageFile.FileName));
}

client.Send(msgobj);

Upvotes: 1

levent
levent

Reputation: 3634

you should change

 @using (Html.BeginForm()) 

with

@using (Html.BeginForm("Index", "YourControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))

What does enctype='multipart/form-data' mean?

Upvotes: 1

Related Questions