Print Page | Close Window

Formulario para enviar un email a través ASP.NET

Printed From: Vida Una
Category: Programación
Forum Name: Códigos ASP y ASP.NET
Forum Description: Foro dedicado al mundo de la programación en código ASP y ASP.NET. Aquí encontrarás ejemplos y trucos en ASP muy útiles.
URL: https://www.vidauna.com/forum_posts.asp?TID=477
Printed Date: 28 Marzo 2024 at 11:53pm


Topic: Formulario para enviar un email a través ASP.NET
Posted By: Programador
Subject: Formulario para enviar un email a través ASP.NET
Date Posted: 06 Septiempbre 2009 at 12:01am

<%@ Page Language="VB" %>

<!DOCTYPE "-//W3C//DTD XHTML 1.0 Transitional//EN"" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd - http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat="server">

    Protected Sub btnEnviar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        '
        Dim correo As New System.Net.Mail.MailMessage()
        correo.From = New System.Net.Mail.MailAddress(txtDe.Text)
        correo.To.Add(txtPara.Text)
        correo.Subject = txtAsunto.Text
        correo.Body = txtTexto.Text
        correo.IsBodyHtml = True
        correo.Priority = System.Net.Mail.MailPriority.Normal

        Dim smtp As New System.Net.Mail.SmtpClient
       
  smtp.Host = "servidor SMTP"
  smtp.Port = 25 ' 464 si la cuenta es de Gmail
        smtp.Credentials = New System.Net.NetworkCredential("usuario", "contraseña")
        smtp.EnableSsl = False ' True si la cuenta es de Gmail
       
        Try
            smtp.Send(correo)
            LabelError.Text = "Mensaje enviado satisfactoriamente"
        Catch ex As Exception
            LabelError.Text = "ERROR: " & ex.Message
        End Try
       
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Esto (en ASP.NET 2.0) no se ejecuta... si AutoEventWireup="false"
        If Not IsPostBack Then
            txtTexto.Text = "Hola," & vbCrLf & _
                        "Esto es una prueba de envio de correo usando ASP.NET 2.0 con Visual Basic" & vbCrLf & _
                        "Saludos!!!"
            LabelError.Text = ""
        End If
    End Sub
</script>

<html xmlns=" http://www.w3.org/1999/xhtml - http://www.w3.org/1999/xhtml " >
<head runat="server">
    <title>Prueba para enviar correo usando ASP.NET 2.0 (Visual Basic)</title>
    <meta name="robots" content="noindex" />
</head>
<body>
    <form id="form1" runat="server">
        <table style="width: 550px">
            <tr>
                <td valign="top">
                    <asp:Label ID="Label1" runat="server" Text="De:"></asp:Label></td>
                <td><asp:TextBox ID="txtDe" runat="server" Width="95%">la cuenta a quien envías el mensaje</asp:TextBox></td>
            </tr>
            <tr>
                <td valign="top">
                    <asp:Label ID="Label2" runat="server" Text="Para:"></asp:Label></td>
                <td>
                    <asp:TextBox ID="txtPara" runat="server" Width="95%">la cuenta a quien envías el mensaje</asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPara" ErrorMessage="El formato del correo no es válido" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td>
            </tr>
            <tr>
                <td valign="top">
                    <asp:Label ID="Label3" runat="server" Text="Asunto:"></asp:Label></td>
                <td>
                    <asp:TextBox ID="txtAsunto" runat="server" Width="95%">Prueba de envio de correo con ASP.NET 2.0 (C#)</asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtAsunto" ErrorMessage="Debes escribir el asunto"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td valign="top">
                    <asp:Label ID="Label4" runat="server" Text="Texto:"></asp:Label></td>
                <td>
                    <asp:TextBox ID="txtTexto" runat="server" Columns="50" Rows="10" TextMode="MultiLine"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtTexto" ErrorMessage="Debes escribir algo en el texto"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><asp:Button ID="btnEnviar" runat="server" Text="Enviar" OnClick="btnEnviar_Click" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><asp:Label ID="LabelError" runat="server" Text=""></asp:Label></td>
            </tr>
        </table>
    </form>
</body>
</html>




Print Page | Close Window