Send out email in code behind (C#) : Email « Components « ASP.Net






Send out email in code behind (C#)

<%@ Page language="c#" src="IssueReporter.aspx.cs" AutoEventWireup="false" Inherits="IssueReporter.IssueReporter" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 54px" runat="server" Font-Names="Verdana" Font-Size="X-Small">Your Name:</asp:Label>
      <asp:TextBox id="txtComment" style="Z-INDEX: 107; LEFT: 176px; POSITION: absolute; TOP: 112px" runat="server" Width="384px" Height="112px" TextMode="MultiLine" Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
      <asp:TextBox id="txtSender" style="Z-INDEX: 106; LEFT: 176px; POSITION: absolute; TOP: 80px" runat="server" Width="259px" Height="24px" Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
      <asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 86px" runat="server" Font-Names="Verdana" Font-Size="X-Small">Your Email:</asp:Label>
      <asp:Label id="Label3" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 118px" runat="server" Width="104px" Height="16px" Font-Names="Verdana" Font-Size="X-Small">Comment:</asp:Label>
      <asp:CheckBox id="chkPriority" style="Z-INDEX: 104; LEFT: 32px; POSITION: absolute; TOP: 240px" runat="server" Width="416px" Height="24px" Text="Please Reply Immediately!" Font-Names="Verdana" Font-Size="X-Small"></asp:CheckBox>
      <asp:TextBox id="txtName" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 48px" runat="server" Width="258px" Height="24px" Font-Names="Verdana" Font-Size="X-Small"></asp:TextBox>
      <asp:Button id="cmdSend" style="Z-INDEX: 108; LEFT: 32px; POSITION: absolute; TOP: 288px" runat="server" Width="88px" Height="24px" Text="Send"></asp:Button>
      <asp:Label id="lblResult" style="Z-INDEX: 109; LEFT: 40px; POSITION: absolute; TOP: 352px" runat="server" Width="432px" Height="72px" Font-Names="Verdana" Font-Size="X-Small"></asp:Label>
    </form>
  </body>
</HTML>

<%--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;

namespace IssueReporter
{
  /// <summary>
  /// Summary description for IssueReporter.
  /// </summary>
  public class IssueReporter : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.TextBox txtComment;
    protected System.Web.UI.WebControls.TextBox txtSender;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.CheckBox chkPriority;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.Button cmdSend;
    protected System.Web.UI.WebControls.Label lblResult;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
      // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void cmdSend_Click(object sender, System.EventArgs e)
    {
      MailMessage msg = new MailMessage();
      msg.Subject = "Issue Report";
      msg.Body = "Submitted By: " + txtName.Text + "\n";
      msg.Body += txtComment.Text;
      msg.From = txtSender.Text;

      msg.To = "yourname@youremailserver.com";

      if (chkPriority.Checked) msg.Priority = MailPriority.High;

      SmtpMail.SmtpServer = "localhost";
      SmtpMail.Send(msg);

      lblResult.Text="Message sent to SMTP service.";
    }
  }
}

--%>
           
       








Related examples in the same category

1.Simplest Way of Sending Email from Gmail
2.Send out an Email
3.Send email with attached file
4.Send email out through asp:form
5.Send email to all employees in database
6.Send email with html form message
7.Email message in a html format
8.Email form with cc and bcc
9.Email with priority
10.Send email function
11.Email with pictures
12.Email with attachment
13.Send email with more than one attachments
14.Send out an email in case of page error
15.Send an email