SmtpClient fails to send email - vb.net

My website (I'll call it mydomain.com) is hosted on a dedicated server from 1&1 running Windows Server 2008 R2. For antivirus protection, it has AVG File Server Edition 2013. Windows Firewall is not enabled.
On that server I have installed SmarterMail and use it for all my email through a subdomain mail.mydomain.com. From my site, I send email and it's working fine.
Today I set up an account for email hosting with Rackspace and am trying to test it as a replacement for hosting on my own server. I'm testing with a different domain, myotherdomain.com. I have set up two user accounts in Rackspace as well as Outlook on my local system to send and receive email for this new domain, and it works fine in Outlook.
However, when I try to send email from my site, I get an error. Here is the VB.NET code I'm using in Page_Load of my test page:
Dim Msg As New MailMessage("orders#myotherdomain.com", "customer#yahoo.com")
With Msg
.Subject = "Email test"
.Body = "This is a test message."
.IsBodyHtml = True
End With
Dim Smtp As New SmtpClient
With Smtp
.UseDefaultCredentials = False
.Credentials = New NetworkCredential("orders#myotherdomain.com", strPassword)
.DeliveryMethod = SmtpDeliveryMethod.Network
.EnableSsl = True
.Host = "secure.emailsrvr.com"
.Port = 465
End With
Try
Smtp.Send(Msg)
lblMessage.Text = "Success!"
Catch SmtpEx As SmtpException
lblMessage.Text = "SMTP exception: " & SmtpEx.Message
If SmtpEx.InnerException IsNot Nothing Then lblMessage.Text &= " (InnerException: " & SmtpEx.InnerException.Message & ")"
Catch GeneralEx As Exception
lblMessage.Text = "General exception: " & GeneralEx.Message
End Try
I can log in to the Rackspace email portal with orders#myotherdomain.com and the value of strPassword.
The SMTP error message I get from attempting to send email from my site is: Failure sending mail. (InnerException: Unable to read data from the transport connection: net_io_connectionclosed.)
In my 1&1 Control Panel, I have configured the DNS settings for myotherdomain.com for an IP address that isn't used anywhere else, and I've defined the reverse mapping for that IP address to myotherdomain.com. I have also specified that it uses another mail server and have defined the mx0 and mx1 server names, as specified by Rackspace.
Can someone please help with ideas as to why email is not working from my site when it's the domain that is hosted on Rackspace? Thanks!

I've just come across the same issue using Rackspace SMTP. The problem is that the .net SmtpClient doesn't support Implicit SSL, which is what is provided on port 465 on that SMTP server. I switched to using port 587 and it worked correctly.
See http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx

Related

.Invoke(“SetPassword”, …) results in “RPC server is unavailable” error

I have a page to create new users in our active directory using VB.NET
I’m using the following code
Dim rootEntry As New DirectoryEntry
With rootEntry
.Path = "LDAP://" & strServer & "/" & strLDAP
.AuthenticationType = AuthenticationTypes.Secure
.Username = strServerUsername
.Password = strServerPassword
End With
Dim newUser As DirectoryEntry = rootEntry.Children.Add("CN=" & strCN, "user")
With newUser
.CommitChanges()
.Properties("userPrincipalName").Value = TextPN.Text
.Properties("sAMAccountName").Value = TextAlias.Text
.Properties("givenname").Value = TextGivenname.Text
.Properties("sn").Value = TextSurname.Text
……
.CommitChanges()
.Invoke("setPassword", New Object() {strDefaultPassword})
.CommitChanges()
.Properties("userAccountControl").Value = &H0001
.CommitChanges()
End With
This code worked fine in the past.
Now we’ve migrated our webserver to Windows Server 2008 R2 and IIS 7.5, and suddenly the code is not working anymore. (.net framework is 2.0 and cannot be changed)
The user is still created in our active directory, but the account is automatically disabled and the password is not set.
Investigating this issue shows that an exception is thrown at the line
.Invoke("setPassword", New Object() {strDefaultPassword})
Exception
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
The user account which is used to connect to the AD is still the same and had domain admin rights.
Since nothing has changed to the code, I think there must be another reason why this is not working anymore? Firewall settings, IIS configuration,..?
Any ideas??
I know there is a similar case here Trying to create a new Active Directory user, Invoke("SetPassword",pwd) throws "The RPC server is unavailable"
, but this doesn’t help me out.
Check TCP/UDP 445 port is opened on your firewall.
To connect to an AD server from outside a domain, you need the following ports to be opened :
. TCP/UDP 389 (LDAP)
. TCP 3268 (GC)
. TCP/UDP 445 (SMB over IP)
DirectoryEntry.Invoke() requires AuthenticationType.Secure. What this means is that it needs to be able to authenticate the request via Kerberos or NTLM.
It attempts to use LDAPS (TCP 636) first, then falls back to CiFS (TCP445) if/when it times out or fails because of a missing or invalid certificate. If neither of these ports are open, it will fail with an "RPC Server unavailable" exception.

sending mail(Transaction failed. The server response was: Relay rejected for policy reasons)

I have a windows application that i am using to send emails. i can send an email for any
one to his company mail directly (I am using domino server to send the mail)
for example i can send to test#landmarkgroup.com
but when i send to test#hotmail.com it is giving the following error
Transaction failed. The server response was: Relay rejected for policy reasons.
here is my code
Dim MyMailMessage As New MailMessage()
MyMailMessage.From = New MailAddress("test#landmarkgroup.com")
MyMailMessage.To.Add("harb.nayef#hotmail.com")
MyMailMessage.Subject = txtSubject.Text
MyMailMessage.Body = txtBody.Text
Dim SMTPServer As New SmtpClient("112.12.29.62")
SMTPServer.Port = 25
SMTPServer.Send(MyMailMessage)
Yes, it fails because the Domino server has been setup to not accept SMTP relays from unknown sources (to avoid spam).
Contact the admin of the Domino server so that you can set up Domino to accept relay from machines running your application (if they can be uniquely identified), or from internal hosts (if that's the case), and/or combined with a SMTP account to login to SMTP.

Send email via vb.net web application

I'm trying to send a simple plain-text email through my VB.net web application.
I've followed the instructions here:
http://www.systemnetmail.com/faq/3.1.1.aspx
But, regardless of what email addresses I use, I keep getting the message "unable to connect to the remote server".
Here is my code
'Create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("<email1>")
mail.To.Add("<email2>")
'set the content
mail.Subject = "This is an email"
mail.Body = "this is a sample body"
'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)
Why is this not working?
Are you sure that your pc (127.0.0.1, loopback ip) is a SMTP server?
Dim smtp As New SmtpClient(host) means that your pc tries to connect to smtp server host and use it to send an email.
Check that and you gonna solve your problem...
Just to try: change 127.0.0.1 with the default SMTP server you use in your email software and see what happens...
More: catch the exception (if one is raised) and take note of the message...
Try to add this
smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
and review your firewall settings, maybe the port is closed.

vb.net Send Smtp Mail From IIS FormatException

Well i got a page that sends emails and everything runs fine in the developer web server but when i publish to a IIS Server i get a FormatException when i try to send the mail....
ex.Message = "the specified string is not in the form required for an e-mail address."
the email is in this sample someone#gmail.com
Im using the "SmtpClient" and the "MailMessage" classes to send mails..
And the server runs windows server 2003
Edit:
Im using same smtp settings on the Dev Webserver as im doing in IIS.
Check that you've enabled local email relaying for the SMTP service on your Windows Server 2003 box. By 'local' I mean that applications on your local machine can use the SMTP service to relay emails - but you almost certainly don't want to allow external connections to do the same.
Make sure you don't have any invisible whitespace around your email address. For example, if you are getting the email from a database, or input control, try trimming it, like:
mail.To.Add( New MailAddress( txtEmailAddress.Text.Trim() ) )
If any one gets this problem the solution is don't use the empty constructor of MailMessage!!!!!

make an http post from server using user credentials - integrated security

I'm trying to make a post, from an asp classic server side page, using the user credentials...
I'm using msxml2.ServerXMLHTTP to programatically make the post
I've tried with several configurations in the IIS 5.1 site, but there's no way I can make IIS run with a specified account...
I made a little asp page that runs whoami to verify what account the iis process i using...
with IIS 5.1, using integrated security the process uses:
my_machine\IWAM_my_machine
I disable integrated security, and leave a domain account as anonymous access, and I get the same (¿?)
to test the user I do the following
private function whoami()
dim shell, cmd
set shell = createObject("wscript.shell")
set cmd = shell.exec( server.mapPath( "whoami.exe" ) )
whoami = cmd.stdOut.readAll()
set shell = nothing: set cmd = nothing
end function
is it because I'm issuing a shell command?
I'd like to make http post calls, to another site that works with integrated security...
So I need some way to pass the credentials, or at least to run with a specified account, and then configure the remote site to thrust that account...
I thought that just setting the site to work with integrated security would be enough...
How can I achieve such a thing?
ps: with IIS6,happens the same
but if I change the pool canfiguration I get the following info from whoami
NT AUTHORITY\NETWORK SERVICE
NT AUTHORITY\LOCAL SERVICE
NT AUTHORITY\SYSTEM
if I set a domain account, I get a "service unavailable" message...
edit: found this
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/275269ee-1b9f-4869-8d72-c9006b5bd659.mspx?mfr=true
it says what I supossed, "If an authenticated user makes a request, the thread token is based on the authenticated account of the user", but somehow I doesn't seem to work like that... what could I possibly be missing?
edit:
well the whoami thing is obviously fooling me, I tried with the following function
private function whoami_db( serverName, dbName )
dim conn, data
set conn = server.createObject("adodb.connection")
conn.open "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
"Initial Catalog=" & dbName & ";Data Source=" & serverName
set data = conn.execute( "select suser_sname() as user_name" )
whoami_db = data("user_name")
data.close: conn.close
set data = nothing: set conn = nothing
end function
and everything seemed to be working fine...
but how can I make msxml2.ServerXMLHTTP work with the user credentials???
You are correct whoami.exe was confusing you. Launching a separate process caused the new process to run as the user of the current process. On XP that would be the COM+ application host (DLLHOST) and would normally run as IWAM_<machine>. On IIS6 it would the w3wp.exe work process and typically runs as NT AUTHORITY\Network Service.
However a thread processing a HTTP request will impersonate a different security token. With integrated security as you have discovered this would the security token of the user making the request, as your SSPI experiment bears out. With anonymous access the anonymous user configured on the site/application is used, this is typically <MACHINE>\IUSR_<machine>.
As to your specific problem with ServerXMLHTTP this goes back to the underlying component WinHTTP. This by default will only send the current users credentials if the server being accessed is the proxy bypass list. Even then it possible to the ServerXMLHTTP configures it to never send the user credentials, I've not test that scenario myself.
Unfortunately ServerXMLHTTP provides very limited access to the configuration details on WinHTTP. However if this is a show stopper then you could always use the WinHTTP component directly yourself:-
Dim oWinHTTP
Dim oDOM
Const AutoLogonPolicy_Always = 0
Set oWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oWinHTTP.SetAutoLogonPolicy AutoLogonPolicy_Always
oWinHTTP.Open "GET", "http://remoteserver.org/getsomexml.xxx", False
oWinHTTP.Send
If oWinHTTP.Status = 200 Then
Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")
oDOM.async = false
oDOM.Load oWinHTTP.ResponseStream
End If
Set oWinHTTP = Nothing
That should work for http, for https it gets real messy.