views:

109

answers:

1

I'm in the process of upgrading my web pages from asp classic to asp.net. My previous code used CDO to send emails.

sch = "http://schemas.microsoft.com/cdo/configuration/" 
                Set cdoConfig = CreateObject("CDO.Configuration") 

                    With cdoConfig.Fields 
                        .Item(sch & "sendusing") = 2 
                        .Item(sch & "smtpserver") = "mail.mydomain.com" 
                        .update 
                    End With 

                Set cdoMessage = CreateObject("CDO.Message") 

                    strTo = "[email protected]"

                strFrom = "[email protected]"

                strSubject = "email subject"
                strBody = strBody & "This is the email body"


                With cdoMessage 
                        Set .Configuration = cdoConfig 
                        .From = strFrom
                        .To =  strTo
                        .Subject = strSubject
                        .HTMLBody = strBody
                        .Send 
                End With

                Set cdoConfig = Nothing
                Set cdoMessage = Nothing

This code works fine but I'd like to send emails from my asp.net pages. When I send from the .net pages, I get the error message: "No connection could be made because the target machine actively refused it xx.xx.xxx.xxx:2 "

My web.config settings:

<system.net>
    <mailSettings>
        <smtp from="[email protected]">
            <network host="mail.mydomain.com" port="2"/>
        </smtp>
    </mailSettings>
</system.net>

And the section of code that is giving me the error:

        Dim mailmsg As New MailMessage("[email protected]", txtSubmitterEmail.Text)

        mailmsg.Subject = "subject here"
        mailmsg.Body = "mail body here"
        mailmsg.IsBodyHtml = True

        Dim smtp As New SmtpClient

        smtp.Send(mailmsg)

I'm rather new to .Net but I've searched for hours and can not come up with a reason why the old code works but the new doesn't. Thanks in advance for the help!

+3  A: 

The SMTP server you want to reach is probably on port 25.

<network host="mail.mydomain.com" port="25"/>

This is different from the sendusing directive.

mmsmatt
In case the OP missed it: when the error message reports "xx.xx.xxx.xxx:2", the last number is the port number. SMTP is conventionally on port 25.
Bevan
Depending the OS ASP.NET might not have permission to send mail and can throw the same exception.
Jeroen
@mmsmatt Thanks! That worked. I didn't try port 25 because the code using CDO was working using port 2.
zeroef