Mark
Mark

Reputation: 7818

how do I send emails in asp.net via code

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

Upvotes: 1

Views: 915

Answers (2)

BrOSs
BrOSs

Reputation: 929

    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

Upvotes: 1

illinoistim
illinoistim

Reputation: 446

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.

Upvotes: 0

Related Questions