Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Forget all I send (error of sending mail in asp.net). and

Please give the correct full code of sending mail in asp.net c#script.

Please help me.
Posted
Updated 24-Jan-11 21:48pm
v3
Comments
Dalek Dave 25-Jan-11 3:48am    
Edited to remove the childishness.

add this name space
using System.Web.Mail;

this code below in button

C#
MailMessage mail = new MailMessage();
mail.To = "mail_id";
//for example
mail.To = "info@twinkle.in";


mail.From = "Twinkle"; // or text box value
mail.Subject = "for a query";// or text box value
mail.Body = "First Name: " + TextBox1.Text + "\n Last Name: " +  TextBox2.Text;
SmtpMail.SmtpServer = "your server";

try
        {
            SmtpMail.Send(mail);
mail send succesffully
        }
        catch
        {

mail send not  succesffully
        }
 
Share this answer
 
v3
Comments
Dalek Dave 25-Jan-11 3:49am    
Edited for Code Block
C#
protected void btnSendmail_Click(object sender, 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;
   }
 }
 
Share this answer
 
v2
Comments
Dalek Dave 25-Jan-11 3:50am    
Edited for Code Block.
I did gave you a link to Microsoft article that explains all. Looks like you deleted that question :doh:

Now, here you go, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
 
Share this answer
 
try this code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace sendEmailThroughC_sharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{

try
{
SmtpClient clientConnectionWithGmailSmtp = new SmtpClient("smtp.gmail.com");//it connect user with gmail smtp server
MailMessage mailMessage = new MailMessage();
mailMessage.From =new MailAddress(fromTB.Text);
mailMessage.To.Add(toTb.Text);
mailMessage.Priority = MailPriority.High;
mailMessage.Subject = subjectTB.Text;
mailMessage.Body = messageBodyTB.Text;
if (textBox1.Text != string.Empty)
{
Attachment attachment = new Attachment(textBox1.Text);
mailMessage.Attachments.Add(attachment);
}

clientConnectionWithGmailSmtp.Port = 587;
clientConnectionWithGmailSmtp.Credentials = new System.Net.NetworkCredential("sendmailthroughc@gmail.com", "Chooseapassword");
clientConnectionWithGmailSmtp.EnableSsl = true;

clientConnectionWithGmailSmtp.Send(mailMessage);
MessageBox.Show("mail Send");
}

catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

}
}
 
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