c# - Using SmtpClient to send an email from Gmail -
i'm trying connect gmail account through smtpclient
seems not work should. specify port 465, enable ssl , define everything, takes 2 minutes , shows error message wasn't sent.
what doing wrong here?
try { mailmessage msg = new mailmessage(); msg.from = new mailaddress("myemail@gmail.com); msg.to.add(new mailaddress("theiremil@email.com)); msg.subject = "this subject"; msg.body = "this body"; smtpclient sc = new smtpclient("smtp.gmail.com", 465); sc.enablessl = true; sc.usedefaultcredentials = false; sc.credentials = new networkcredential("myemail@gmail.com", "pass"); sc.deliverymethod = smtpdeliverymethod.network; sc.send(msg); erroremail.text = "email has been sent successfully."; } catch (exception ex) { erroremail.text = "error: " + ex.message; }
you need allow "less secure apps":
https://support.google.com/accounts/answer/6010255
code:
try { new smtpclient { host = "smtp.gmail.com", port = 587, enablessl = true, timeout = 10000, deliverymethod = smtpdeliverymethod.network, usedefaultcredentials = false, credentials = new networkcredential("mymail@gmail.com", "mypassword") }.send(new mailmessage {from = new mailaddress("mymail@gmail.com", "myname"), = {"theirmail@mail.com"}, subject = "subject", body = "message", bodyencoding = encoding.utf8}); erroremail.text = "email has been sent successfully."; } catch (exception ex) { erroremail.text = "error: " + ex.message; }
Comments
Post a Comment