asp.net邮件格式验证

这篇博客展示了如何在ASP.NET中编写代码以实现从Web应用发送邮件的功能。通过使用MailMessage和SmtpMail类,作者提供了从指定邮箱发送邮件的示例,并在页面中包含了邮件接收者、主题和正文的输入验证。当发送成功时,状态提示会显示'消息已发送',如果发送失败,则会显示错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

'Coded by AbhijitMehta for the article
'Sending EMail From Your own Web Application
'for www.codeguru.com
<%@ Page Language="VB" %>
<%@ import Namespace="System.Web.Mail" %>
<script runat="server">

    Sub bottonSend_Click(sender As Object, e As EventArgs)
   
        Dim emailMessage As MailMessage
        emailMessage=New MailMessage
        emailMessage.From=labelFrom.Text
        emailMessage.To=textTo.Text
        emailMessage.Subject=textSubject.Text
        emailMessage.Body=textMessage.Text
        SmtpMail.SmtpServer = "10.0.2.95"
        Try
            SmtpMail.Send(emailMessage)
            labelStatus.Text="Message sent!"
            buttonSend.Enabled=False
        Catch ex as Exception
            labelStatus.Text="Unable to send the e-mail message"
        End Try
    End Sub
   
    Sub bottonNewMessage_Click(sender As Object, e As EventArgs)
   
        textTo.Text=""
        textSubject.Text=""
        textMessage.Text=""
        buttonSend.Enabled=True
        labelStatus.Text=""
    End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <h2>Send an E-mail Message
        </h2>
        <h4>From<asp:Label id="labelFrom" style="Z-INDEX: 100; LEFT: 64px; POSITION: absolute; TOP: 64px" runat="server" width="205px">Manu@MyWorld.net</asp:Label>
        </h4>
        <h4>To<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 101; LEFT: 368px; POSITION: absolute; TOP: 96px" runat="server" Width="186px" ControlToValidate="textTo" ErrorMessage="You must enter a recipient!"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 100; LEFT: 56px; POSITION: absolute; TOP: 120px" runat="server" Width="248px" ControlToValidate="textTo" ErrorMessage="Invalid e-mail address!" ValidationExpression="/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*"></asp:RegularExpressionValidator>
            <asp:TextBox id="textTo" style="Z-INDEX: 102; LEFT: 56px; POSITION: absolute; TOP: 96px" runat="server" Width="273px"></asp:TextBox>
        </h4>
        <h4>Subject<asp:TextBox id="textSubject" runat="server" Width="269px"></asp:TextBox>
        </h4>
        <h4>Message::<asp:RequiredFieldValidator id="RequiredFieldValidator2" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 192px" runat="server" Width="321px" ControlToValidate="textMessage" ErrorMessage="You must enter a message!"></asp:RequiredFieldValidator>
        </h4>
        <p>
            <asp:TextBox id="textMessage" runat="server" Width="518px" TextMode="MultiLine" Height="63px"></asp:TextBox>
        </p>
        <p>
            <asp:Button id="buttonSend" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 304px" onclick="bottonSend_Click" runat="server" Text="Send"></asp:Button>
            <asp:Button id="bottonNewMessage" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 304px" onclick="bottonNewMessage_Click" runat="server" Text="New Message"></asp:Button>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
            <asp:Label id="labelStatus" style="Z-INDEX: 106; LEFT: 16px; POSITION: absolute; TOP: 368px" runat="server" width="289px" font-bold="True" forecolor="Red"></asp:Label>
        </p>
    </form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值