I'm having great difficulty figuring out how to send emails through my asp.net website, through a registered/online domain on a UKFasts Cloud Shared VPS.
My website and domain names are hosted with UKFast on one of their Cloud/VPS servers (eg. www.mysite.co.uk). My web app is hosted on a dedicated server (eg. www.mysite-ssl.co.uk). I want to send emails via the domain registered on my VPS, from the dedicated server.
I can connect via Outlook, and send receive emails without any issue:
However, because I'm on a VPS/Cloud server, UKFast advise I have to use "localhost" or "127.0.0.1" if I'm sending from code. But I'm failing to see what is different to Outlook connecting and sending emails, to what my code is trying to do from the dedicated server, via the VPS domain/mail:
Imports System.Net.Mail
Public Shared Function SendMail(ByVal email As String, ByVal name As String, ByVal hear As String, ByVal mess As String) As String
Dim mail As New MailMessage()
mail.From = New MailAddress(email)
mail.To.Add("******@gmail.com")
mail.Subject = "Contact Email from My Website"
mail.IsBodyHtml = True
Dim str As String = "<table border=""1"" cellpadding=""4"" cellspacing=""0""><tr><td>Name:</td><td>" & name & "</td></tr><tr><td>" & "Email:</td><td>" & email & "</td></tr><tr><td>Hear:</td><td>" & hear & "</td></tr><tr><td valign=""top"">Message:</td><td>" & mess.Replace(Chr(10), "<br />") & "</td></tr></table>"
mail.Body = str
Dim smtp As New SmtpClient("mail.-same as outgoing mail server in Outlook-.co.uk")
smtp.Port = 25
smtp.Credentials = New System.Net.NetworkCredential("UserNameFromOutlook","PasswordFromOutlook")
Try
smtp.Send(mail)
Catch ex As Exception
Return ex.ToString
Return ("error")
End Try
Return ("ok")
End Function
However, when sending this from the asp.net page, I get the error:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it (myipaddress):25 at System.Net.Sockets.Socket.
I can't see why Outlook can connect with no issues, but my SendMail code can't do the same, with the same credentials.
My web.config has:
<configuration>
<system.net>
<mailSettings>
<smtp from="info@busybeesdingwall.co.uk">
<network host="mail.-same as outgoing mail server in Outlook-" port="25" userName="UserNameFromOutlook" password="PasswordFromOutlook" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Is there something wrong with my code, that I can change to allow me to send emails?
Thanks for any help,
Mark