c# - cannot send email through button asp.net -
im planning send email image attached receiver. when click on button, nothing happens. error or success note not visible. can me out on this? thank you. here codes: aspx
<%@ page title="" language="c#" masterpagefile="~/admin/site1.master" autoeventwireup="true" codebehind="submitpurchaseorder.aspx.cs" inherits="islandgas.admin.submitpurchaseorder" %> <asp:content id="content1" contentplaceholderid="title" runat="server"> </asp:content> <asp:content id="content2" contentplaceholderid="content" runat="server"> <form id="form1" runat="server"> <div> <table style=" border:1px solid" align="center"> <tr> <td colspan="2" align="center"> <b>purchase order supplier</b> </td> </tr> <tr> <td> gmail username: </td> <td> <asp:textbox id="txtusername" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="requiredfieldvalidator1" runat="server" errormessage="user name required." display="dynamic" controltovalidate="txtusername" forecolor="red"></asp:requiredfieldvalidator> <asp:regularexpressionvalidator id="regularexpressionvalidator7" runat="server" errormessage="invalid format." display="dynamic" validationexpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" controltovalidate="txtusername" forecolor="red"></asp:regularexpressionvalidator> </td> </tr> <tr> <td> gmail password: </td> <td> <asp:textbox id="txtpwd" runat="server" textmode="password"></asp:textbox> <asp:regularexpressionvalidator id="regularexpressionvalidator1" runat="server" errormessage="no special characters." controltovalidate="txtpwd" forecolor="red" validationexpression="^[a-za-z0-9]+$" display="dynamic"></asp:regularexpressionvalidator> <asp:requiredfieldvalidator id="requiredfieldvalidator2" runat="server" errormessage="password required." display="dynamic" controltovalidate="txtpwd" forecolor="red"></asp:requiredfieldvalidator> </td> </tr> <tr> <td> subject: </td> <td> <asp:textbox id="txtsubject" runat="server"></asp:textbox> <asp:regularexpressionvalidator id="regularexpressionvalidator3" runat="server" errormessage="no special characters." controltovalidate="txtsubject" forecolor="red" validationexpression="^[a-za-z0-9 ]+$" display="dynamic"></asp:regularexpressionvalidator> <asp:requiredfieldvalidator id="requiredfieldvalidator3" runat="server" errormessage="subject required." display="dynamic" controltovalidate="txtsubject" forecolor="red"></asp:requiredfieldvalidator> </td> </tr> <tr> <td> to: </td> <td> <asp:textbox id="txtto" runat="server"></asp:textbox> <asp:regularexpressionvalidator id="regularexpressionvalidator2" runat="server" errormessage="invalid format." display="dynamic" validationexpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" controltovalidate="txtto" forecolor="red"></asp:regularexpressionvalidator> <asp:requiredfieldvalidator id="requiredfieldvalidator4" runat="server" errormessage="recipient required." display="dynamic" controltovalidate="txtto" forecolor="red"></asp:requiredfieldvalidator> </td> </tr> <tr> <td> attach file: </td> <td> <asp:fileupload id="fileupload1" runat="server" /> <asp:requiredfieldvalidator id="requiredfieldvalidator5" runat="server" errormessage="please upload image" controltovalidate="fileupload1" forecolor="red"></asp:requiredfieldvalidator> <asp:regularexpressionvalidator id="regularexpressionvalidator5" runat="server" errormessage="invalid file type" controltovalidate="fileupload1" forecolor="red" validationexpression="^.*\.(jpg|jpg|gif|gif|doc|doc|pdf|pdf|png|png)$"></asp:regularexpressionvalidator> </td> </tr> <tr> <td valign="top"> body: </td> <td> <asp:textbox id="txtbody" runat="server" textmode="multiline" columns="30" rows="10" ></asp:textbox> <asp:regularexpressionvalidator id="regularexpressionvalidator6" runat="server" errormessage="no special characters." controltovalidate="txtbody" forecolor="red" validationexpression="^[a-za-z0-9]+$" display="dynamic"></asp:regularexpressionvalidator> <asp:requiredfieldvalidator id="requiredfieldvalidator6" runat="server" errormessage="message body required." display="dynamic" controltovalidate="txtbody" forecolor="red"></asp:requiredfieldvalidator> </td> </tr> <tr> <td> </td> <td> <asp:button id="btnsubmit" text="send" runat="server" onclick="btnsubmit_click" /> </td> </tr> </table> </div> </form> </asp:content>
and code behind:
protected void btnsubmit_click(object sender, eventargs e) { try { mailmessage msg = new mailmessage(); // sender e-mail address. msg.from = new mailaddress(txtusername.text); // recipient e-mail address. msg.to.add(txtto.text); msg.subject = txtsubject.text; // file upload path string filename = fileupload1.postedfile.filename; string mailbody = txtbody.text + "<br/><img src=cid:companylogo>"; string filename = path.getfilename(filename); msg.attachments.add(new attachment(fileupload1.postedfile.inputstream, filename)); //linkedresource myimage = new linkedresource(filename); // create html view alternateview htmlmail = alternateview.createalternateviewfromstring(mailbody, null, "text/html"); // set contentid property. value of contentid property must same // src attribute of image tag in email body. //myimage.contentid = "companylogo"; // htmlmail.linkedresources.add(myimage); msg.alternateviews.add(htmlmail); // remote smtp server ip. smtpclient smtp = new smtpclient(); smtp.host = "smtp.gmail.com"; smtp.port = 587; smtp.credentials = new system.net.networkcredential(txtusername.text, txtpwd.text); smtp.enablessl = true; smtp.send(msg); msg = null; //clientscript.registerstartupscript( "<script>alert('mail sent thank you...');if(alert){ window.location='sendmail.aspx';}</script>"); response.write("<script>alert('email sent');</script>"); } catch (exception ex) { response.write("<script>alert('unable send email');</script>"); console.writeline("{0} exception caught.", ex); }
is main div wrapped in form?
<form runat="server" id="frm1"> .... </form>
Comments
Post a Comment