c# - Error in sending email with a button -
what want send email through button click attached image it. have code aspx page:
<div> <table style=" border:1px solid" align="center"> <tr> <td colspan="2" align="center"> <b>send mail using gmail credentials in asp.net</b> </td> </tr> <tr> <td> gmail username: </td> <td> <asp:textbox id="txtusername" runat="server"></asp:textbox> </td> </tr> <tr> <td> gmail password: </td> <td> <asp:textbox id="txtpwd" runat="server" textmode="password"></asp:textbox> </td> </tr> <tr> <td> subject: </td> <td> <asp:textbox id="txtsubject" runat="server"></asp:textbox> </td> </tr> <tr> <td> to: </td> <td> <asp:textbox id="txtto" runat="server"></asp:textbox> </td> </tr> <tr> <td> attach file: </td> <td> <asp:fileupload id="fileupload1" runat="server" /> </td> </tr> <tr> <td valign="top"> body: </td> <td> <asp:textbox id="txtbody" runat="server" textmode="multiline" columns="30" rows="10" ></asp:textbox> </td> </tr> <tr> <td> </td> <td> <asp:button id="btnsubmit" text="send" runat="server" onclick="btnsubmit_click" /> </td> </tr> </table> </div>
and here code behind:
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>"; 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; page.registerstartupscript("usermsg", "<script>alert('mail sent thank you...');if(alert){ window.location='sendmail.aspx';}</script>"); } catch (exception ex) { console.writeline("{0} exception caught.", ex); }
however line
page.registerstartupscript("usermsg", "<script>alert('mail sent thank you...');if(alert){ window.location='sendmail.aspx';}</script>");
gives me error saying:
registerstartupscript(string,string) obsolete.
any idea , how solve it? thank you!
try
using system; using system.net.mail; using system.io; protected void page_load(object sender, eventargs e) { } 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); } }
Comments
Post a Comment