c# - How to send a mail in Xamarin using System.Net.Mail.SmtpClient -
i trying send , email inside app using system.net.mail.smtpclient. when run code on phone java.lang.runtimeexception error , can't figure out why?
i have following code run send email when click button.
using system; using android.app; using android.widget; using android.os; using system.net; using system.net.mail; using system.net.mime; using system.threading; using system.componentmodel; namespace sendemail { [activity (label = "sendemail", mainlauncher = true)] public class activity1 : activity { protected override void oncreate (bundle bundle) { base.oncreate (bundle); // set our view "main" layout resource setcontentview (resource.layout.main); // our button layout resource, // , attach event button button = findviewbyid<button> (resource.id.mybutton); edittext text = findviewbyid<edittext> (resource.id.mailtext); button.click += delegate { string username = "****@gmail.com"; string password = "****"; system.net.networkcredential nc = new system.net.networkcredential(username, password); mailmessage mailmessage = new mailmessage(); mailmessage.to.add("****@gmail.com"); mailmessage.subject = "here subject"; mailmessage.from = new system.net.mail.mailaddress("****@gmail.com"); mailmessage.body = "application run time "; system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient("smtp.gmail.com"); smtpclient.usedefaultcredentials = false; smtpclient.enablessl = true; smtpclient.credentials = nc; smtpclient.port = 587; smtpclient.send(mailmessage); }; } } }
try works me. ;)
try { mailmessage mail=new mailmessage(); smtpclient smtpserver=new smtpclient("smtp.gmail.com"); mail.from=new mailaddress("from address here"); mail.to.add("to adress here"); mail.subject = "message subject"; mail.body = "message body"; smtpserver.port = 587; smtpserver.credentials=new system.net.networkcredential("username","password"); smtpserver.enablessl=true; servicepointmanager.servercertificatevalidationcallback=delegate(object sender, x509certificate certificate, x509chain chain, system.net.security.sslpolicyerrors sslpolicyerrors) { return true; }; smtpserver.send(mail); toast.maketext(application.context, "mail send sucessufully", toastlength.short).show(); } catch(exception ex) { toast.maketext(application.context,ex.tostring(),toastlength.long); }
Comments
Post a Comment