Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2006, 11:04 AM
Authorized User
 
Join Date: Jan 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default sending email problem

Basically what I want to do is write or get hold of code where the following is done.
The text from three text boxes to be taken out and sent to a specific email address e.g. college@college.com and not to any one else.
The text boxes names are.
From: name
Question:

Im having problem with the using.System.Net.Mail; saying Im missing an assembly reference. Does anyone have any solutions. The code is below also if you want to look at it and see an error in it in case that is.

This is the code Im using:
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;

namespace tut20
{
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label Name;
        protected System.Web.UI.WebControls.TextBox txtName;
        protected System.Web.UI.WebControls.TextBox txtEmail;
        protected System.Web.UI.WebControls.TextBox txtMessage;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.Label Label3;
        protected System.Web.UI.WebControls.Label lblStatus;
        protected System.Web.UI.WebControls.Button btnSendMail;

        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.btnSendMail.Click += new System.EventHandler(this.btnSendMail_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void btnSendMail_Click(object sender, System.EventArgs e)
        {
        {
            // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
            // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            try
            {
                MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

                // You can specify the host name or ipaddress of your server
                // Default in IIS will be localhost
                smtpClient.Host = "localhost";

                //Default port will be 25
                smtpClient.Port = 25;

                //From address will be given as a MailAddress Object
                message.From = fromAddress;

                // To address collection of MailAddress
                message.To.Add("admin1@yoursite.com");
                message.Subject = "Feedback";

                // CC and BCC optional
                // MailAddressCollection class is used to send the email to various users
                // You can specify Address as new MailAddress("admin1@yoursite.com")
                message.CC.Add("admin1@yoursite.com");
                message.CC.Add("admin2@yoursite.com");

                // You can specify Address directly as string
                message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
                message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

                //Body can be Html or text format
                //Specify true if it is html message
                message.IsBodyHtml = false;

                // Message body content
                message.Body = txtMessage.Text;

                // Send SMTP mail
                smtpClient.Send(message);

                lblStatus.Text = "Email successfully sent.";
            }
            catch (Exception ex)
            {
                lblStatus.Text = "Send Email Failed." + ex.Message;
            }
        }
        }
    }
}
 
Old February 3rd, 2006, 01:11 PM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You probably didn't load the library in to Visual Studio
What you should do i add the System.Net.dll file in the Project Dependencies






Similar Threads
Thread Thread Starter Forum Replies Last Post
Email Sending Problem dharmeshtandel Forum and Wrox.com Feedback 1 April 30th, 2008 08:46 AM
Email sending problem aneesh_sunu@yahoo.co.in ASP.NET 1.0 and 1.1 Basics 3 December 21st, 2006 01:16 PM
problem sending an email leo_vinay SQL Server 2000 3 July 23rd, 2006 12:55 PM
Problem with Sending Email rama26 Pro JSP 0 April 22nd, 2006 01:50 AM
Email Sending Problem.. gaurav_jain2403 VB.NET 2002/2003 Basics 2 February 4th, 2006 09:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.