Sie sind auf Seite 1von 3

ASP.NET: Sending Emails --------------------------------------Introduction Sending email currently is important for applications for attracting people to u se applications/websites.

Sending email through websites is a better way to not allow any third party into your application. Some features like Feedback, Newsle tter, Messages, Contact Us etc. are similar to sending email. That is because th ese features in a website sends email to the specified person. There is also the need to send emails in ASP.Net. Requirement To integrate a Send email feature in your website, the most important requiremen t is for Simple Mail Transfer Protocol (SMTP). The other side requires a good de sign to show whatever you want to send in the email and code to do it. The following namespace is required to add Simple Mail Transfer Protocol (SMTP) : using System.Net.Mail Source code Add the following code in the source code page: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inheri ts="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .auto-style1 { color: #A52A2A; } </style> </head> <body> <form id="form1" runat="server"> <div> <table align=center> <tr> <td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Bro wn;">To:</span></td> <td><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td> </tr> <tr> <td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Bro wn;">CC:</span></td> <td><asp:textbox id="txtEmail" Width="241" Runat="server"></asp:textbox></td> </tr> <tr> <td class="auto-style1"><strong>Subject&nbsp;</strong></td> <td> <asp:TextBox ID="TextBox1" runat="server" Width="241"></asp:TextBox>

</td> </tr> <tr> <td colSpan="2" ><span style="font-family:Verda na; font-size:12px; font-weight:bold; color:Brown;">Message:</span></td> </tr> <tr> <td align="center" colSpan="2" width=100%><asp: textbox id="txtMessage" Width="100%" Runat="server" Height="99" TextMode="MultiL ine" MaxLength="400"></asp:textbox></td> </tr> <tr> <td colSpan="2">&nbsp;</td> </tr> <tr> <td align=center> <asp:Button ID="Button1" runat="server" OnClick="Button1 _Click" Text="Send" /> </td> <td align=center>&nbsp;</td> </tr> <tr> <td colSpan="2"><asp:label id="lblStatus" Runat ="server" EnableViewState="False"></asp:label></td> </tr> </table> </div> </form> </body> </html>

Code behind: --------------Add the following code in the code behind page in the C# language on the Button Click event: using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Net.Mail;

public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { MailMessage mail = new MailMessage(); mail.To.Add(txtName.Text); mail.To.Add(txtEmail.Text);

mail.From = new MailAddress("email address from where you want to send e mails"); mail.Subject = "testing"; string Body = txtMessage.Text; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential ("add email address", "password"); //Or your Smtp Email ID and Password smtp.EnableSsl = true; smtp.Send(mail); } } Run

Das könnte Ihnen auch gefallen