0

我正在将我的网页从 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 很陌生,但我已经搜索了几个小时,无法找到旧代码有效但新代码无效的原因。在此先感谢您的帮助!

4

1 回答 1

3

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

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

这与sendusing指令不同。

于 2010-08-16T21:53:42.000 回答