从asp.net中的联系人页面发送电子邮件

从asp.net中的联系人页面发送电子邮件,asp.net,vb.net,visual-studio-2010,email,Asp.net,Vb.net,Visual Studio 2010,Email,Iam在visual studio 2010中开发我想从联系人页面发送电子邮件我在下面列出了我的代码,它执行时没有错误,但电子邮件未发送 Partial Class _Default Inherits System.Web.UI.Page ''' <summary> ''' Actions when the Send button is clicked. ''' </summary> Protected Sub btnSend_Click(ByVal

Iam在visual studio 2010中开发我想从联系人页面发送电子邮件我在下面列出了我的代码,它执行时没有错误,但电子邮件未发送

Partial Class _Default
  Inherits System.Web.UI.Page

  ''' <summary>
  ''' Actions when the Send button is clicked.
  ''' </summary>
  Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click

    'Create instance of main mail message class.
    Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

    'Configure mail mesage
    'Set the From address with user input
    '    mailMessage.From = New System.Net.Mail.MailAddress(txtFromAddress.Text.Trim())
    'Get From address in web.config
    mailMessage.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("fromEmailAddress"))
    'Another option is the "from" attirbute in the <smtp> element in the web.config.

    'Set additinal addresses
    mailMessage.To.Add(New System.Net.Mail.MailAddress(txtToAddress.Text.Trim()))
    'mailMessage.CC
    'mailMessage.Bcc
    'mailMessage.ReplyTo

    'Set additional options
    mailMessage.Priority = Net.Mail.MailPriority.High
    'Text/HTML
    mailMessage.IsBodyHtml = False

    'Set the subjet and body text
    mailMessage.Subject = txtSubject.Text.Trim()
    mailMessage.Body = txtBody.Text.Trim()

    'Add one to many attachments
    'mailMessage.Attachments.Add(New System.Net.Mail.Attachment("c:\temp.txt")

    'Create an instance of the SmtpClient class for sending the email
    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()

    'Use a Try/Catch block to trap sending errors
    'Especially useful when looping through multiple sends
    Try
      smtpClient.Send(mailMessage)
    Catch smtpExc As System.Net.Mail.SmtpException
      'Log error information on which email failed.
    Catch ex As Exception
      'Log general errors
    End Try

    End Sub
End Class

     <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"           Inherits="_Default" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
      <title>Send Email</title>
       </head>
       <body>
        <form id="form1" runat="server">
       <div>
       <p>
        From:<br />
       <asp:TextBox ID="txtFromAddress" runat="server" Columns="35" /></p>
       <p>
       To:<br />
       <asp:TextBox ID="txtToAddress" runat="server" Columns="35" /></p>
      <p>
       Subject:<br />
       <asp:TextBox ID="txtSubject" runat="server" Columns="50" /></p>
      <p>
        Body:<br />
            <asp:TextBox ID="txtBody" runat="server" Columns="75" TextMode="MultiLine"      Rows="6" /></p>
        <p>
         <asp:Button ID="btnSend" runat="server" Text="Send Mail" /></p>
     </div>
   </form>
  </body>
  </html>

 web config 


     <?xml version="1.0"?>
             <!-- 

      -->
       <configuration>
    <!--Externalize From address-->
    <appSettings>
    <add key="fromEmailAddress" value="syntaxDon@gmail.com"/>
    </appSettings>
    <!--Externalize From address-->
    <connectionStrings/>
    <!--Mail settings-->
    <system.net>
    <mailSettings>
        <smtp>
        <network host="\localhost:"/>
        </smtp>
    </mailSettings>
    </system.net>
    <!--Mail settings-->
    <system.web>
    <!-- 
        Set compilation debug="true" to insert debugging 
        symbols into the compiled page. Because this 
        affects performance, set this value to true only 
        during development.

        Visual Basic options:
        Set strict="true" to disallow all data type conversions 
        where data loss can occur. 
        Set explicit="true" to force declaration of all variables.
    -->
    <compilation debug="true" strict="false" explicit="true"    targetFramework="4.0">
     </compilation>
      <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
          <namespaces>
            <clear/>
            <add namespace="System"/>
            <add namespace="System.Collections"/>
            <add namespace="System.Collections.Generic"/>
            <add namespace="System.Collections.Specialized"/>
            <add namespace="System.Configuration"/>
            <add namespace="System.Text"/>
            <add namespace="System.Text.RegularExpressions"/>
            <add namespace="System.Linq"/>
            <add namespace="System.Xml.Linq"/>
            <add namespace="System.Web"/>
            <add namespace="System.Web.Caching"/>
            <add namespace="System.Web.SessionState"/>
            <add namespace="System.Web.Security"/>
            <add namespace="System.Web.Profile"/>
            <add namespace="System.Web.UI"/>
            <add namespace="System.Web.UI.WebControls"/>
            <add namespace="System.Web.UI.WebControls.WebParts"/>
            <add namespace="System.Web.UI.HtmlControls"/>
        </namespaces>
       </pages>
       <!--
           The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
         -->
    <authentication mode="Windows"/>
    <!--
          The <customErrors> section enables configuration 
          of what to do if/when an unhandled error occurs 
          during the execution of a request. Specifically, 
          it enables developers to configure html error pages 
          to be displayed in place of a error stack trace.

         <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
         <error statusCode="403" redirect="NoAccess.htm" />
         <error statusCode="404" redirect="FileNotFound.htm" />
       </customErrors>
      -->
     </system.web>
部分类\u默认值
继承System.Web.UI.Page
''' 
单击发送按钮时的“”操作。
''' 
受保护的子BTN发送\单击(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理BTN发送。单击
'创建主邮件类的实例。
Dim mailMessage As System.Net.Mail.mailMessage=新系统.Net.Mail.mailMessage()
'配置邮件mesage
'使用用户输入设置发件人地址
'mailMessage.From=New System.Net.Mail.MailAddress(txtFromAddress.Text.Trim())
'从web.config中的地址获取
mailMessage.From=新系统.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings(“fromEmailAddress”))
'另一个选项是web.config中元素中的“from”attirbute。
'设置附加地址
mailMessage.To.Add(新的System.Net.Mail.MailAddress(txtToAddress.Text.Trim()))
'mailMessage.CC
'mailMessage.Bcc
'mailMessage.ReplyTo
'设置其他选项
mailMessage.Priority=Net.Mail.MailPriority.High
'文本/HTML
mailMessage.IsBodyHtml=False
'设置主题和正文文本
mailMessage.Subject=txtSubject.Text.Trim()
mailMessage.Body=txtBody.Text.Trim()
'添加一到多个附件
'mailMessage.Attachments.Add(新的System.Net.Mail.Attachment(“c:\temp.txt”)
'创建SmtpClient类的实例以发送电子邮件
Dim smtpClient As System.Net.Mail.smtpClient=新系统.Net.Mail.smtpClient()
'使用Try/Catch块捕获发送错误
'在循环多次发送时特别有用
尝试
smtpClient.Send(mailMessage)
将smtpExc捕获为System.Net.Mail.SmtpException
'记录电子邮件失败的错误信息。
特例
'记录一般错误
结束尝试
端接头
末级
发送电子邮件

发件人:

致:

主题:

正文:

网络配置

您必须将
btn发送\u单击
处理程序绑定到按钮:

<asp:Button ID="btnSend" runat="server" Text="Send Mail" OnClick="btnSend_Click" />


你的问题毫无意义……请一步一步地澄清你想要实现的目标。好的,我试过了,但没有用,我的web配置正确吗,因为我正在尝试从LocalHost发送。请检查以下内容: