1

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: Outlook Setup

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="[email protected]">
            <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

7
  • telnet yourIPserver 25 is that working for you? Commented Mar 8, 2013 at 22:38
  • Hi - no regardless of telnet mail.myipserver.co.uk, or telnet mail.myipserver.co.uk:25, or telnet mail.myipserver.co.uk:587, it advises "Could not open connection to the host on port XX: Connect failed" - so how does Outlook manage it? Commented Mar 8, 2013 at 22:48
  • you'd never be able to send any email until you get something like: 220 test.auto.mySMTPserver.com ESMTP Service (Lotus Domino Release 8.5.3FP2 HF95) ready at Tue, 30 Oct 2012 08:27:31 -0700 on telnet command. So, you should focus on open up that connection. Commented Mar 8, 2013 at 22:56
  • well, may be you are using the wrong syntax. Try: "telnet mail.myipserver.co.uk 587" because when you're typing "mail.myipserver.co.uk:587" it just takes the port 23 as default and not the 587. Commented Mar 8, 2013 at 23:01
  • Ah, I tried "mail.myipserver.co.uk 25" now that responded with 220 mail.myipserver.co.uk ESMTP - but how do I amend my code, to reflect that? If I change to ...Dim smtp As New SmtpClient("mail.myipserver.co.uk 25")... I just get a message saying the Remote name could not be resolved - thanks again. Commented Mar 8, 2013 at 23:05

2 Answers 2

1
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
        "[email protected]", toTextBox.Text, subTextBox.Text, msgTextBox.Text);

        SmtpClient SMTPServer = new SmtpClient("yourSMTPServer", 25);

        try
        {
            SMTPServer.Send(mailObj);
        }
        catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }

        Label1.Text = "Msg sent";
    }

You just have to add the controls

toTextBox, subTextBox, msgTextBox

Sign up to request clarification or add additional context in comments.

5 Comments

Hi - ok, tried that, but the message is still: 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.DoConnect - tried with 25 and 587 (still can't understand why Outlook can do it, and not code!)
how are you connecting to the SMTP server? VPN ? is outlook hosted in the same machine that the code ?
Hi - I'm sitting in my home, at my local computer - with Outlook running with the settings as in the screenshot above. The code is running on a dedicated server with UKFast. It is attempting to use the same email account that my local Outlook is hooked up to, to send emails via the other VPS UKFast server that my website is on (website for advertising the application, and the applications itself, are on two separate servers, both hosted by UKFast).
I can't see the image 'cause I'm firewalled at work. Could you run the code in your local computer? Are you sure that we are using the right port?
Hi @BrOSs - I'm going to mark your resposne as the answer - as it IS working on my local computer for some reason. THere must be something on the dedicated server, stopping it running from there. THank you so much for helping trouble shoot this, I really appreciate your time and patience!
0

You aren't connecting with your host. The first area to check is your web.config file. It sounds like you have set this up already:

<mailSettings>
<smtp>
<network host="localhost"

Once you verify that this is set up, you will probably have something to set up within your service provider. Often, they require setup within their tools.

2 Comments

Hi - I have added my web.config to my post. Trouble is, I need to setup host to be the mailserver on the VPS, not on the dedicated server. I want to be able to connect and send emails, in exactly the same way Outlook allows me to - ie. from a different computer. Thanks, Mark
I thought " the target machine actively refused it " meant there was a connection made, but for some reason, it wasn't allowed through,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.