Saturday, March 24, 2012

SmtpException 5.7.3 - Authentication error when sending mail from web page

I used the MailHelper code I found on here, modified what was needed and everything works until the actual .Send then I get this error:

System.Net.Mail.SmtpException was unhandled by user code
Message="Syntax error, command unrecognized. The server response was: 5.7.3 Authentication unsuccessful"
Source="System"
StackTrace:
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at C4_Website.MailHelper.SendMailMessage(String from, String recepient, String bcc, String cc, String subject, String body) in C:\Visual SourceSafe\C4 Website\C4 Website\App_Code\MailHelper.vb:line 47
at C4_Website.Notify_Me.btnSend_Click(Object sender, EventArgs e) in C:\Visual SourceSafe\C4 Website\C4 Website\Notify_Me.aspx.vb:line 16
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

I tried using the authentication method listed here:

http://www.systemnetmail.com/faq/4.2.aspx

I tried it 'as-is', when that didn't work, I added the domain parameter to the Credentials property. When that didn't work I also tried adding the domain into the username in the web.config like "domain\username". This is sending to my own Exchange server. What am I doing wrong?

Thanks.

It worked in my site.

Still, try to replace

SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Credentials = new NetworkCredential("username", "secret");

with just

SmtpClient smtp = new SmtpClient();


iliemail:

It worked in my site.

Still, try to replace

SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Credentials = new NetworkCredential("username", "secret");

with just

SmtpClient smtp = new SmtpClient();

That is what I had actually. I didn't use all of the code in that authentication page, just the .Credentials line and I had to modify it slightly. My code blows up at the last line in this sub:

1Imports System.Net.Mail23Public Class MailHelper4 ''' <summary>5 ''' Sends an mail message6 ''' </summary>7 ''' <param name="from">Sender address</param>8 ''' <param name="recepient">Recepient address</param>9 ''' <param name="bcc">Bcc recepient</param>10 ''' <param name="cc">Cc recepient</param>11 ''' <param name="subject">Subject of mail message</param>12 ''' <param name="body">Body of mail message</param>13 Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)14 ' Instantiate a new instance of MailMessage15 Dim mMailMessage As New MailMessage()1617 ' Set the sender address of the mail message18 mMailMessage.From = New MailAddress(from)19 ' Set the recepient address of the mail message20 mMailMessage.To.Add(New MailAddress(recepient))2122 ' Check if the bcc value is nothing or an empty string23 If Not bcc Is Nothing And bcc <> String.Empty Then24 ' Set the Bcc address of the mail message25 mMailMessage.Bcc.Add(New MailAddress(bcc))26 End If2728 ' Check if the cc value is nothing or an empty value29 If Not cc Is Nothing And cc <> String.Empty Then30 ' Set the CC address of the mail message31 mMailMessage.CC.Add(New MailAddress(cc))32 End If3334 ' Set the subject of the mail message35 mMailMessage.Subject = subject36 ' Set the body of the mail message37 mMailMessage.Body = body3839 ' Set the format of the mail message body as HTML40 mMailMessage.IsBodyHtml = True41 ' Set the priority of the mail message to normal42 mMailMessage.Priority = MailPriority.Normal4344 ' Instantiate a new instance of SmtpClient45 Dim mSmtpClient As New SmtpClient()46 ' Authenticate47 'mSmtpClient.Credentials = New Net.NetworkCredential("someuser", "somepwd", "somedomain")48 ' Send the mail message49 mSmtpClient.Send(mMailMessage)5051 End Sub52End Class

Here's how I call it:

1Protected Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click2 Dim msgBody As String34 msgBody = Me.txtFirstName.Text5 msgBody = msgBody & " "6 msgBody = msgBody & Me.txtLastName.Text7 msgBody = msgBody & " would like to be notified when the website is completed. Email: " & Me.txtEmail.Text & "."89 MailHelper.SendMailMessage(Me.txtEmail.Text, "myemail@.me.com", "", "", "website user enquiry", msgBody)10 End Sub

Here's the snippet from web.config:

1<system.net>2<mailSettings>3<smtp from="webuser@.mymailserver.com">4<network host="mail.mymailserver.com" port="25" userName="someuser" password="somepwd"/>5</smtp>6</mailSettings>7</system.net>8

Anyone?


Our server is an Exchange 2007 server. Is there any way that it could be something related to a problem in Exchange 2007 requiring a different way to do this?


It must be something specific to our Exchange 2007 Server. I just changed the server and username/pwd info to point to a yahoo business mail account that we have and it works. I think I'll start a new thread specific to Exchange 2007 so it might be easier for others to find information specific to that problem. Hopefully someone knows what needs to be different for 2007.

0 comments:

Post a Comment