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>
答案 0 :(得分:0)
您必须将btnSend_Click
处理程序绑定到按钮:
<asp:Button ID="btnSend" runat="server" Text="Send Mail" OnClick="btnSend_Click" />