c# - Sending mail and pulling data from app.config -
i have situation. html code standard 3 text boxes , 1 button:
<add key="cma_contact_form_email" value="somec@gmail.com"/> <add key="cma_contact_to_address" value="some@gmail.com"/> <add key="smtpserver" value="smtp.gmail.com" /> <add key="enablessl" value = "true"/> <add key="smtpport" value="993" /> <add key="smtpuser" value="some@gmail.com" /> <add key="smtppass" value="pass" />
code behind:
protected void imagebutton_click(object sender, eventargs e){ mailmessage msg = new mailmessage(); msg.to.add(configurationmanager.appsettings["cma_contact_form_email"]); msg.from = new mailaddress(configurationmanager.appsettings["cma_contact_to_address"]); msg.body += "name: " + txtname.text + "\n"; msg.body += "email: " + txtemail.text + "\n"; msg.body += "message: \n" + txtmessage.text + "\n"; msg.subject = txtname.text; msg.isbodyhtml = true; smtpclient smtp = new smtpclient(); smtp.host = configurationmanager.appsettings["smtpserver"]; //or smtp server address smtp.port = convert.toint32(configurationmanager.appsettings["smtpport"]); smtp.enablessl = convert.toboolean(configurationmanager.appsettings["enablessl"]); smtp.credentials = new system.net.networkcredential(configurationmanager.appsettings["smtpuser"], configurationmanager.appsettings["smtppass"]); //or smtp email id , password try { smtp.send(msg); lblpost.text = "thank you, question has been submitted our cma heldesk."; } catch (exception ex) { lblpost.text = "error occured while sending message. " + ex.message; } placeholder.visible = false; msgplaceholder.visible = true; }
normally should work, time-out error. know catch might be? thanks!
you can use following method check if connection permissible before doing actual send email , should handle smtpexception. has statuscode property, tell why send() failed.
you can call below method this
if(testconnection("smtpserveraddress",port)) sendemail(); public static bool testconnection(string smtpserveraddress, int port) { iphostentry hostentry = dns.gethostentry(smtpserveraddress); ipendpoint endpoint = new ipendpoint(hostentry.addresslist[0], port); using (socket tcpsocket = new socket(endpoint.addressfamily, sockettype.stream, protocoltype.tcp)) { //try connect , test rsponse code 220 = success tcpsocket.connect(endpoint); if (!checkresponse(tcpsocket, 220)) { return false; } // send helo , test response code 250 = proper response senddata(tcpsocket, string.format("helo {0}\r\n", dns.gethostname())); if (!checkresponse(tcpsocket, 250)) { return false; } // if got here it's can connect smtp server return true; } } private static bool checkresponse(socket socket, int expectedcode) { while (socket.available == 0) { system.threading.thread.sleep(100); } byte[] responsearray = new byte[1024]; socket.receive(responsearray, 0, socket.available, socketflags.none); string responsedata = encoding.ascii.getstring(responsearray); int responsecode = convert.toint32(responsedata.substring(0, 3)); if (responsecode == expectedcode) { return true; } return false; }
gmail settings
- gmail smtp server address: smtp.gmail.com
- gmail smtp user name: example@gmail.com
- gmail smtp password: password
- gmail smtp port: 465
- gmail smtp tls/ssl required: yes
Comments
Post a Comment