asp.net mvc 4 letter from site add attachment -
i have feedback form on site, looks
i created model form
using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.linq; using system.web; namespace corepartners_site2.models { public class feedbackform { public string name { get; set; } public string email { get; set; } public string phone { get; set; } public string company { get; set; } public string additionalinformation { get; set; } [fileextensions(extensions = "doc,txt,pdf")] public httppostedfilebase projectinformation { get; set; } } }
and created view
@using (html.beginform("feedback", "home", formmethod.post, new { id = "feedback-form" })) { @html.textboxfor(model => model.name, null, new { @class = "text-field" }) @html.textboxfor(model => model.email, null, new { @class = "text-field" }) @html.textboxfor(model => model.phone, null, new { @class = "text-field" }) @html.textboxfor(model => model.company, null, new { @class = "text-field" }) @html.textareafor(model => model.additionalinformation, new { cols="1", rows="1" }) @html.textboxfor(model => model.projectinformation, null, new { type="file", @class="input-file" }) <a href="#" class="link1" onclick="document.getelementbyid('feedback-form').submit()"><em><b>send</b></em></a> }
i'd know form work a
submit instead <input type="submit" />
?
and dont know how make attachment letter, tried make
[httpget] public actionresult feedback() { return view(); } [httppost] public actionresult feedback(feedbackform model) { system.net.mail.mailmessage msg = new system.net.mail.mailmessage(); msg.bodyencoding = encoding.utf8; msg.from = new mailaddress(model.email, @resources.global.feedback_email_title); msg.to.add("tayna-anita@mail.ru"); string message = @resources.global.feedback_name + ": " + model.name + "\n" + @resources.global.feedback_email + ": " + model.email + "\n" + @resources.global.feedback_phone + ": " + model.phone + "\n" + @resources.global.feedback_company + ": " + model.company + "\n\n" + model.additionalinformation; msg.body = message; msg.isbodyhtml = false; //attachment if (model.projectinformation != null) { httppostedfilebase attfile = model.projectinformation; int attachfilelength = attfile.contentlength; if (attachfilelength > 0) { string strfilename = path.getfilename(model.projectinformation.filename); model.projectinformation.saveas(server.mappath(strfilename)); mailattachment attach = new mailattachment(server.mappath(strfilename)); msg.attachments.add(attach); string attach1 = strfilename; } } smtpclient client = new smtpclient("smtp.mail.ru", 25); client.usedefaultcredentials = false; client.enablessl = false; try { client.send(msg); } catch (exception ex) { } feedbackform tempform = new feedbackform(); return view(tempform); }
but shows mistake in msg.attachments.add(attach);
, seems not work.
well answer first question yes can use anchor tag in place of submit input.
you want disable tags default behaviour javascript/jquery , have submit form:
$(function () { $('a.something').on("click", function (e) { e.preventdefault(); $('feedback-form').submit(); }); });
the error receiving because using wrong object type msg.attachments.add() method. need use attachment object not mailattachment object.
something this:
stream attachmentstream = file.openread (file); streams.add (attachmentstream); mail.attachments.add (new attachment (attachmentstream, path.getfilename (file));
Comments
Post a Comment