Sample Email Code In .net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMail.aspx.cs" Inherits="SendMail" %>

 

<head runat="server"><title>title></head>
<body>
<form id="form1" runat="server">
Message to: <asp:TextBox ID="txtTo" runat="server" /><br>
Message from: <asp:TextBox ID="txtFrom" runat="server" /><br>
Subject: <asp:TextBox ID="txtSubject" runat="server" /><br>
Message Body:<br>
<asp:TextBox ID="txtBody" runat="server" Height="171px" TextMode="MultiLine" Width="270px" /><br>
<asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email" /><br>
<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>
</form>
</body>
</html>

===================
C# Code 
===================

using System;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{
protected void Btn_SendMail_Click(object sender, EventArgs e)
{
MailMessage mailObj = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
SmtpClient SMTPServer = new SmtpClient("localhost");
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
}

  • 57 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

Sample Code For Connection String In Asp to MsAccess Database and Mssql Server

Sample Code For Connection String In Asp to MsAccess DatabaseExample of a DSN connection<%DIM...

Connection String in the .net for Ms Access and MSSQL Server

Connection String in the .net for Ms Access using System.Data.Odbc;OdbcConnection conn = new...

Powered by WHMCompleteSolution