SMTP Authentication System.Net.Mail???

Penhall

Perch
This is bugging the hell out of me. I am trying to send an email using System.Net.Mail

It sends fine to email accounts hosted @ jodohost but errors when sending to anything outside the jodo network. The error I get is: Mailbox name not allowed. The server response was: sorry, that domain isn't allowed to be relayed thru this MTA (#5.7.1)

Basically, just testing with a button and the click code of:

Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
Dim Smtp As New Net.Mail.SmtpClient()
Dim SmtpUser As New System.Net.NetworkCredential()
Dim TOName As String = "Some Name"
Message.From = New Net.Mail.MailAddress([EMAIL="[email protected]"][email protected][/EMAIL], "Jodohost hosted account")
Message.To.Add(New Net.Mail.MailAddress("[email protected]", TOName))
Message.IsBodyHtml = True
Message.Subject = "Test Email ASPX"
Message.Body = "This is a friggin test"
Smtp.UseDefaultCredentials = True
Smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
Try
Smtp.Send(Message)
EmailStatus.Text = "Success"
Catch ex As Exception
EmailStatus.Text = ex.Message.ToString
Finally
End Try
End Sub

Web Config is setup as

Code:
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="mail.jhhostedsite.com" password="properpassword" port="25" userName="[email protected]"/>
</smtp>
</mailSettings>
</system.net>

Anythoughts before I pull what's left of my hair out.

Thanks
 
Are you sure Smtp.UseDefaultCredentials = True is doing the right thing? It sounds like it's trying to use the credentials of the web server user.

Have you tried it with the credentials directly in code? Something like:

Smtp.Credentials = New System.Net.NetworkCredential("User", "Password")

That's the way I've done it and I haven't set UseDefaultCredentials so I guess it's false by default.

I know you really want it in the config file but I'd try this first to at least see if it works.
 
Smtp.UseDefaultCredentials = True

I was going back and forth on that one too but it doesn't make a difference... same error.

Smtp.Credentials = New System.Net.NetworkCredential("User", "Password")

Haven't tried that yet but will give it a shot.

Here's the kicker I found this morning though, using the same script with a different host that I use too works fine (of course changing the server/username/password info in the web.config)

This leads me to the conclusion its a server issue at JH.

Ticket time I guess.
 
Oh, and adding Smtp.Credentials and Smtp.Host directly to the code behind seems to work fine. :boom:

:hurt:
 
It does seem a bit suspicious if it works on another server.

If it was me, if you haven't already done so, I'd first make sure that the Jodo server works with authenticated smtp from a regular client, eg, Thunderbird, Outlook or whatever. That probably works because others would have probably reported it by now but if it doesn't then that's definitely something simple to report.

If that works then the next thing I'd do is try doing smtp via telnet on both servers and see if there is a difference and, if there is, then that's something you can report to JodoHost but it gets tricky for them then if it works on a regular client.

I can't say I've done smtp via telnet with authentication but it looks easy enough if you base64 encode the user name and password.

http://technet.microsoft.com/en-us/library/aa995718(EXCHG.65).aspx

Do you have this running on your desktop in Visual Studio? Does that have the same problem? You might have to use port 587 if your ISP blocks 25. An alternative to messing with telnet might be to run Wireshark on your local machine while you try it with both smtp servers and observe any difference in the conversations.
 
you mean it is working when doing this, or it is seeing the code but still giving the error?

Works fine manually adding the credentials in the codebehind.

Code:
It does seem a bit suspicious if it works on another server.

Yup, strange situation.

Our messages "crossed". I think you need to Google for some examples using the config file.

Oh, trust me, multiple hours trying to find a solution. Web.Config coding seems fine compared to anything I can find.

Do you have this running on your desktop in Visual Studio? Does that have the same problem? You might have to use port 587 if your ISP blocks 25.

Yup, had taken that into account but as I was getting a server response message, the connection was fine, just being rejected.
 
Have you tried it with defaultCredentials="false" in the config file?

I was looking at this which I think agrees with an MS example I saw. Also, I really don't think you want that UseDefaultCredentials = True. I never bother setting the delivery method either but I think it's network by default.
 
True or False in the code-behind or the web.config makes no difference. Same error: Mailbox name not allowed. The server response was: sorry, that domain isn't allowed to be relayed thru this MTA (#5.7.1)
 
As an FYI, sending using localhost works fine on the server in code-behind - just trying to avoid being market as spam with no rDNS.
 
Well ... I don't know.

No promises but I might spend a few minutes tonight trying this from home because it's probably something I want to do myself soon and I'm curious.

Please confirm just to be clear ...

You get the same problem when you try it locally with Visual Studio trying to relay through a JodoHost mail server. That eliminates anything to do with JodoHost's web server.

It works with the credentials in code but not in the config file. Therefore this all boils down to how to read the credentials from the config file.

I know you said it worked with another mail server. Are you sure that you weren't already authenticated for that server perhaps by a mail client at the same IP address (just a wild guess)? I think if a mail server doesn't need authenticating then it doesn't care if you try to give it wrong credentials.

In case it makes a difference, what mail server at JodoHost is this? I have a reseller account with domains using various mail servers so I might be able to test with the same server.
 
You get the same problem when you try it locally with Visual Studio trying to relay through a JodoHost mail server. That eliminates anything to do with JodoHost's web server

Yes, directly from development server through VS2008 gives the exact same error.

It works with the credentials in code but not in the config file. Therefore this all boils down to how to read the credentials from the config file.

Correct.

I know you said it worked with another mail server. Are you sure that you weren't already authenticated for that server perhaps by a mail client at the same IP address (just a wild guess)? I think if a mail server doesn't need authenticating then it doesn't care if you try to give it wrong credentials.

Nope, other tested mail server needs authentication too and was not connect to it anywhere at the time.

In case it makes a difference, what mail server at JodoHost is this? I have a reseller account with domains using various mail servers so I might be able to test with the same server.

Mail3 I believe.

Thanks for helping me think through this, I'm not a .Net-spert and learning as I go. Right now, having it work with code-behind credentials is meeting my purposes but I'd love to get the web.config version working just to justify the time I've spent trying to get it to run. :)
 
I know the answer. :) I got the same result as you until I stopped setting UseDefaultCredentials to anything.

So ... do not set UseDefaultCredentials. The act of setting it to either true or false makes it ignore the config file. Don't set it in code and don't have a defaultCredentials element in the config. That works for me on mail3.

The documentation here is correct I guess but it does leave that point out.

Set UseDefaultCredentials = true and it tries to use the default which I think means the Windows user that IIS is running. That's no use.

Set UseDefaultCredentials = false and it will use whatever you set the Credentials property to or anonymous if you haven't set it.

Don't set it at all and it uses the config.
 
Back
Top