send email C# using smtp server with username password authentification

左心房为你撑大大i 提交于 2019-12-22 12:26:24

问题


I have a piece of code that sends email.. heres the code

This is not working for me. This a remote smtp service ... and i double checked that email web access works fine .. i can login using the gui, recieve and send emails.

But when i try to do it through code .. it fails with the message ...

{System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 No AUTH command has been given.

Can anybody advise ... and also they dont have EWS exposed ie.e exchange web service ./.. this is the way to go ..

port is 25 and no SSL or TLS

Button b = sender as Button;
try
{
    MailMessage msg = new MailMessage(senderEmail, recieverEmail, "afdasfas", "safasfa");
    //MailMessage msg = new MailMessage(senderEmail, recieverEmail, subject, subject);
    System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient(EmailSmtpServer, outgoingPort);
    System.Net.NetworkCredential auth = new System.Net.NetworkCredential(senderEmail, senderPassword);
    mailclient.Host = EmailSmtpServer;
    mailclient.UseDefaultCredentials = false;
    mailclient.Credentials = auth;
    mailclient.Send(msg);
    MessageBox.Show(b.Content + ":WORKED");
}
catch (Exception e4)
{
    MessageBox.Show(b.Content + ": " +e4.Message);
    MessageBox.Show(b.Content + ": " + e4.StackTrace);
}
|improve this question

回答1:


I don't think you want to set

mailclient.UseDefaultCredentials = true;

Read more about this option here.


You should also test that you can actually send email through that smtp server.

Try connecting via telnet and authorizing. There are also plenty of services that will let you send a test message online (http://pingability.com/smtptest.jsp for example).




回答2:


maybe you need to Add mailclient.EnableSsl = true;



来源:https://stackoverflow.com/questions/2731778/send-email-c-sharp-using-smtp-server-with-username-password-authentification

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!