?
Solved

asp.net c# send mail programmatically

Posted on 2011-10-17
3
Medium Priority
?
419 Views
Last Modified: 2012-05-12
Hello,

I have a form that allows content authors request a post on the public-facing website.
The form works as expected, however, I 'm attempting to add a feature that sends me an email in the background to let me know when a new request has been made.

My codebehind follows:

------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.Mail;


namespace HSCO_Sheriff_ExternalWeb
{
    public partial class frm_dataentry_COE : System.Web.UI.Page
    {

        string connectionString = WebConfigurationManager.ConnectionStrings["CalendarofEventsString"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            txtEventName.Focus();

            //lblDateEntered.Text = DateTime.Now.ToString();
            lblDateEntered.Text = DateTime.Now.ToShortDateString();

            if (Page.IsPostBack)
            {
               
            }

            lblEnteredByID.Text = HttpContext.Current.User.Identity.Name.ToString();

        }
       




        protected void inserter()
        {
            string insertSQL;
            insertSQL = "INSERT INTO tblCalendarofEvents (";
            insertSQL += "EventName,EventDescription,EventDateStart,EventTimeStart,EventTimeStartAMPM,EventDateEnd,EventTimeEnd,EventTimeEndAMPM,";
            insertSQL += "EventAddress,EventAddress2,EventCity,EventState,EventZipCode,EventEnteredBy,EventEnteredDate,EventActive)";
            insertSQL += "VALUES (";
            insertSQL += "@EventName,@EventDescription,@EventDateStart,@EventTimeStart,@EventTimeStartAMPM,@EventDateEnd,@EventTimeEnd,@EventTimeEndAMPM,";
            insertSQL += "@EventAddress,@EventAddress2,@EventCity,@EventState,@EventZipCode,@EventEnteredBy,@EventEnteredDate,@EventActive)";

            //SqlConnection con = new SqlConnection("Data Source=HCSO-DBS02; Initial Catalog=AcademyDB; User Id=testforluis; password=hcso");
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(insertSQL, con);


            cmd.Parameters.AddWithValue("@EventName", txtEventName.Text);
            cmd.Parameters.AddWithValue("@EventDescription", txtEventDescription.Text);
            cmd.Parameters.AddWithValue("@EventDateStart", txtEventDateStart.Text.ToString());
            cmd.Parameters.AddWithValue("@EventTimeStart", ddlTimeStart.SelectedItem.Value.ToString());
            cmd.Parameters.AddWithValue("@EventTimeStartAMPM", ddlTimeStartAMPM.SelectedItem.Value.ToString());
            cmd.Parameters.AddWithValue("@EventDateEnd", txtEventDateEnd.Text.ToString());
            cmd.Parameters.AddWithValue("@EventTimeEnd", ddlTimeEnd.SelectedItem.Value.ToString());
            cmd.Parameters.AddWithValue("@EventTimeEndAMPM", ddlTimeEndAMPM.SelectedItem.Value.ToString());
            cmd.Parameters.AddWithValue("@EventAddress", txtAddress.Text);
            cmd.Parameters.AddWithValue("@EventAddress2", txtAddress2.Text);
            cmd.Parameters.AddWithValue("@EventCity", txtCity.Text);
            cmd.Parameters.AddWithValue("@EventState", ddlState.SelectedItem.Value.ToString());
            cmd.Parameters.AddWithValue("@EventZipCode", txtZipCode.Text);
            cmd.Parameters.AddWithValue("@EventEnteredBy", lblEnteredByID.Text);
            cmd.Parameters.AddWithValue("@EventEnteredDate", lblDateEntered.Text);

            cmd.Parameters.AddWithValue("@Picture", System.Data.SqlDbType.Image);

            cmd.Parameters.AddWithValue("@EventActive", '1');

            lblOutput.Text = "Error inserting record. ";

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                lblOutput.Text = "1 event inserted.";
            }
            catch (Exception err)
            {
                lblOutput.Text = "Error inserting record. ";
                lblOutput.Text += err.Message;
            }
            finally
            {
                cmd.Connection.Close();

                txtEventName.Text = "";
                txtEventDescription.Text = "";
                txtEventDateStart.Text = "";
                ddlTimeStart.SelectedIndex = 0;
                ddlTimeStartAMPM.Text = "";
                txtEventDateEnd.Text = "";
                ddlTimeEnd.SelectedIndex = 0;
                ddlTimeEndAMPM.SelectedIndex = 0;
                txtAddress.Text = "";
                txtAddress2.Text = "";
                txtCity.Text = "";
                ddlState.SelectedIndex = 0;
                txtZipCode.Text = "";
                lblEnteredByID.Text = "";
                lblDateEntered.Text = "";
                txtEventImage.Text = "";


            }
        }




        protected void btn_submit_Click(object sender, EventArgs e)
        {


            inserter();
        }

        protected void btb_cancel_Click(object sender, EventArgs e)
        {

        }

             

        protected void txtEventDateStart_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txtEventDateEnd_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlTimeStart_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlTimeEnd_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
--------

ANY help with this would be greatly appreciated.
0
Comment
Question by:Codeaddict7423
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36982641
Have you taken a look at the SmtpClient class? You would, of course, need an SMTP to send the message to, but I imagine your organization has one. You may need to contact your network team to determine its address.
0
 
LVL 2

Accepted Solution

by:
Paulmc999 earned 2000 total points
ID: 36982676
using System.Net;
using System.Net.Mail;
.
.
.
SmtpClient smtpclient = new SmtpClient();
.
.
.
 MailAddress fromAddress = new MailAddress(Request.Form["email"]);
            MailAddress toAddress = new MailAddress("you@wherever.com");
            message.From = fromAddress;
            message.To.Add(toAddress);
            message.Subject = "subject";
            message.Body = "message here"
            smtpclient.Send(message);
.
.
.
web.config can hold host and port details:
    <system.net>
        <mailSettings>
            <smtp>
                <network host="localhost"
                port="25"
            </smtp>
        </mailSettings>
    </system.net>
0
 
LVL 3

Expert Comment

by:ExpertLogin_890
ID: 36985271
Hello

you can set the smtp mail server in code behind file before sending the email
 SmtpMail.SmtpServer = "server name"
default smtp server is localhost
 SmtpMail.SmtpServer = "localhost"

if yor are working in any organization so you need to get the smtp server name/ ip address from your network team also you can set the different port number default port is 80 used by the smtp server.

Or you can set the same configuration in web.config file
  <system.net>
        <mailSettings>
            <smtp>
                <network host="localhost"
                port="25"
            </smtp>
        </mailSettings>
    </system.net>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question