c# - SMTP authentication using GMAIL, error 5.5.1 -
i'm getting little trouble when try using smtp
, mailmessage
in c#:
it's first time using smtp
, mailmessage
, so, might wrong lines, i've done far , need of guys.
i have searched lot of things , here on stackoverflow, far, can't think anymore i'm doing wrong it. when try running (mode debugging , normal run) application console
(just tests on console, pretty simple) gives me error
the smtp server requires secure connection or client not authenticated. server response was: 5.5.1 authentication required. learn more at
i understood following error on visual studio 2013
, problem because don't know properties , methods of class smtp
, mailmessage
, , makes me not understand i'm doing wrong on code,
below, class email
on my console application
/// <summary> /// email server /// </summary> protected smtpclient smtpclient { get; set; } /// <summary> /// message content /// </summary> protected mailmessage mailmessage { get; set; } #endregion /// <summary> /// método enviar e-mail /// </summary> /// <param name="smtp"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="priority"></param> public string enviaremail(string smtp, string from, string to, string subject, string body, bool priority) { try { smtpclient = new smtpclient(); smtpclient.host = "smtp.gmail.com"; smtpclient.port = 587; smtpclient.enablessl = true; smtpclient.deliverymethod = smtpdeliverymethod.network; smtpclient.credentials = new networkcredential("raffa.ferreiira@gmail.com","mypassword"); smtpclient.usedefaultcredentials = true; mailmessage = new mailmessage(); mailmessage.from = new mailaddress(from, "raffa ferreira", encoding.utf8); mailmessage.to.add(new mailaddress(to, "fulano teste", encoding.utf8)); mailmessage.subject = subject; mailmessage.body = body; mailmessage.bodyencoding = encoding.utf8; mailmessage.bodyencoding = encoding.getencoding("iso-8859-1"); if (priority == false) { mailmessage.priority = mailpriority.normal; } else { mailmessage.priority = mailpriority.high; } smtpclient.send(mailmessage); } catch(smtpfailedrecipientexception ex) { console.writeline("mensagem : {0} " + ex.message); } catch(smtpexception ex) { console.writeline("mensagem smpt fail : {0} " + ex.message); } catch(exception ex) { console.writeline("mensagem exception : {0} " + ex.message); } string mensagem = "e-mail enviado"; return mensagem; }
and on
program
i'm callingclass email
passing parameters:
class program { static void main(string[] args) { string smtp = "smtp.live.com"; string = "raffa.ferreiira@gmail.com"; string = "raffa.ferreiira@live.com"; string subject = "communicate"; string body = "hello, text of test message you" email email1 = new email(); email1.enviaremail(smtp, from, to, subject, body, true); } }
i ask around here, on stackoverflow
when can't search answer of doubts anymore, need help. first time using smtp
, mailmessage
....!
why having error ? wrong on code ? , ?
try setting usedefaultcredentials
false
.
from msdn article:
"set property to true when this smtpclientobject should, if requested server, authenticate using default credentials of logged on user."
...
"if the usedefaultcredentials property set to false,then value set in the credentials property used"
Comments
Post a Comment