发送电​​子邮件在ASP.NET 2.0中 [英] Sending Email in ASP.NET 2.0

查看:83
本文介绍了发送电​​子邮件在ASP.NET 2.0中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IAM努力实现发送mail.i曾尝试以下过程code。

iam trying to implement code of sending mail.i have tried the following procedure.

配置

<configuration>
  <!-- Add the email settings to the <system.net> element -->
  <system.net>
    <mailSettings>
      <smtp>
        <network 
             host="localhost" 
             port="25"
             userName="?"
             password="?" />
      </smtp>
    </mailSettings>
  </system.net>

HTML

<table border="0">
    <tr>
        <td><b>Your Email:</b></td>
        <td><asp:TextBox runat="server" ID="UsersEmail" Columns="30"></asp:TextBox></td>
    </tr>
    <tr>
        <td><b>Subject:</b></td>
        <td><asp:TextBox runat="server" ID="Subject" Columns="30"></asp:TextBox></td>
    </tr>
    <tr>
        <td colspan="2">
            <b>Body:</b><br />
            <asp:TextBox runat="server" ID="Body" TextMode="MultiLine" Columns="55" Rows="10"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <asp:Button runat="server" ID="SendEmail" Text="Send Feedback" />
        </td>
    </tr>
</table>

code-背后

Code-behind

protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
    '!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS'
    Const ToAddress As String = "you@youremail.com"

    '(1) Create the MailMessage instance'
    Dim mm As New MailMessage(UsersEmail.Text, ToAddress)

    '(2) Assign the MailMessage's properties'
    mm.Subject = Subject.Text
    mm.Body = Body.Text
    mm.IsBodyHtml = False

    '(3) Create the SmtpClient object'
    Dim smtp As New SmtpClient

    '(4) Send the MailMessage (will use the Web.config settings)'
    smtp.Send(mm)
End Sub

但它不工作。误差运输未能连接到服务器。

推荐答案

您正在使用的实例化对象SmtpClient它在这行你code的默认(无参数)构造函数:

You are instantiating your SmtpClient object using it's default (parameterless) constructor in this line of your code:

'(3) Create the SmtpClient object'
Dim smtp As New SmtpClient

由于发送电子邮件要求您有机会获得一个有效的SMTP服务器,此构造将尝试使用实例的SMTP服务器设置为你的web.config文件中定义,特别是在System.Net部分的对象。下面是这样一个例子:

Since sending an email requires that you have access to a valid SMTP server, this constructor will attempt to instantiate the object using the SMTP server settings as defined in your web.config file, specifically in the System.Net section. Below is an example of this:

<system.net>
  <mailSettings>
    <smtp>
      <network host="[your smtp server address]" port="[your smtp port - usually 25]"/>
    </smtp>
  </mailSettings>
</system.net>

如果这是缺失,您SmtpClient对象没有SMTP服务器连接。这可能导致在您遇到的错误消息。

If this is is missing, your SmtpClient object has no SMTP server to connect to. This is likely to result in the error message that you are experiencing.

要解决此问题,您可以此部分添加到您的web.config文件中,指定为SmtpClient对象有效的SMTP服务器连接到,也可以直接当省略和硬code服务器地址你利用它的一个重载的构造函数接受SMTP服务器地址/端口号作为参数实例化SmtpClient对象。请参见这里的构造细节。

To resolve this, you can either add this section to your web.config file, specifying a valid SMTP server for the SmtpClient object to connect to, or you can omit the and hard-code the server address directly when you instantiate your SmtpClient object by utilising one of it's overloaded constructors that accepts the SMTP server address/port number as parameters. See here for the constructor details.

这方面的一个例子是:

'(3) Create the SmtpClient object'
Dim smtp As New SmtpClient("[your SMTP server address]", 25)

请注意,但是,尽管你可以在SmtpClient的构造函数指定SMTP服务器地址/端口,它通常被认为是更好的做法是在web.config文件中配置这些设置,并在$使用默认的(无参数)构造函数C $℃。使用web.config中的方法可以更新SMTP服务器地址/端口,您无需重新编译code使用。

Be aware, however, that although you can specify the SMTP server address/port in the SmtpClient's constructor, it's generally considered better practise to configure these settings in the web.config file and use the default (parameterless) constructor in your code. Using the web.config method allows you to update the SMTP Server address/port that you use without re-compiling your code.

这篇关于发送电​​子邮件在ASP.NET 2.0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆