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="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

4

2 回答 2

1
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
        "noreplay@myDomain.com", 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";
    }

您只需要添加控件

toTextBox, subTextBox, msgTextBox

于 2013-03-08T22:33:50.070 回答
0

您没有与您的主机连接。要检查的第一个区域是您的 web.config 文件。听起来您已经设置好了:

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

确认已设置好后,您可能需要在服务提供商中设置一些内容。通常,他们需要在工具中进行设置。

于 2013-03-08T22:34:42.090 回答