无法建立连接,因为目标计算机主动拒绝它

时间:2010-08-16 21:51:50

标签: asp.net vb.net smtp system.net.mail

我正在将我的网页从asp classic升级到asp.net。我之前的代码使用CDO发送电子邮件。

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 = "toaddr@mydomain.com"

                strFrom = "fromaddr@mydomain.com"

                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

此代码工作正常,但我想从我的asp.net页面发送电子邮件。当我从.net页面发送时,我收到错误消息:“无法建立连接,因为目标计算机主动拒绝它xx.xx.xxx.xxx:2”

我的web.config设置:

<system.net>
    <mailSettings>
        <smtp from="myemail@mydomain.com">
            <network host="mail.mydomain.com" port="2"/>
        </smtp>
    </mailSettings>
</system.net>

给我错误的代码部分:

        Dim mailmsg As New MailMessage("myemail@mydomain.com", txtSubmitterEmail.Text)

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

        Dim smtp As New SmtpClient

        smtp.Send(mailmsg)

我对.Net很新,但是我已经搜索了几个小时,并且无法想出旧代码可以工作的原因,但新代码没有。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您要访问的SMTP服务器可能位于端口25上。

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

这与sendusing指令不同。