Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
How can I send email in ASP.NET3.5.

I tried this code but it isn't working.
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;
using System.Web;

public partial class Smtp_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label9.Text = "";
    }

     protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = TextBox1.Text;
            mailMessage.To = TextBox2.Text;
            mailMessage.Cc = TextBox3.Text;
            mailMessage.Bcc = TextBox4.Text;
            mailMessage.Subject = TextBox5.Text;
            mailMessage.Body = TextBox6.Text;
           // string attach1 = null;
            
     		
            //string strFileName = null;
            /* Set the SMTP server and send the email with attachment */
            // SmtpMail.SmtpServer = "127.0.0.1";
            SmtpMail.SmtpServer.Insert(0, "smtp.yahoo.com");
            SmtpMail.Send(mailMessage);
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            
            
            Label9.Visible = true;
            
            Label9.Text = "Message sent.";
        }
        catch (Exception ex)
        {
            Label9.Visible = true;
            Label9.Text = ex.ToString();
        }
    }
}
<pre><pre lang="cs">using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mail;
using System.Web;


public partial class Smtp_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label9.Text = "";
    }

    protected void InitializeComponent()
    {
        this.Button1.Click += new System.EventHandler(this.Button1_Click);
        this.Load += new System.EventHandler(this.Page_Load);
    }

     protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            MailMessage mailMessage = new MailMessage();

            mailMessage.From = TextBox1.Text;
            mailMessage.To = TextBox2.Text;
            mailMessage.Cc = TextBox3.Text;
            mailMessage.Bcc = TextBox4.Text;
            mailMessage.Subject = TextBox5.Text;
            mailMessage.Body = TextBox6.Text;

           // string attach1 = null;



            //string strFileName = null;

            /* Set the SMTP server and send the email with attachment */

            // SmtpMail.SmtpServer = "127.0.0.1";
            SmtpMail.SmtpServer.Insert(0, "smtp.yahoo.com");

            SmtpMail.Send(mailMessage);

            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";


            Label9.Visible = true;

            Label9.Text = "Message sent.";
        }
        catch (Exception ex)
        {
            Label9.Visible = true;
            Label9.Text = ex.ToString();
        }
    }
}

Posted
Updated 1-Mar-11 23:18pm
v2

It can be because of various reasons. You need to look at them one by one.
Is the port open? Firewall permissions in place?
Further make sure you have configured SMTP configuration in Web.Config:
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>

If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
 
Share this answer
 
If you use "smtp.yahoo.com" service you'll have to assign the Port No, your yahoo address and the password on you SmtpClient object.

Or you also can send mail through your gmail account.

Sending E-mail using ASP.net through Gmail account (Gmail SMTP Server account)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900