Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need code for send mail with
i use following cod but it has error.
protected void btnSubmit_Click(object sender, EventArgs e)
             MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);

            SmtpClient emailClient=new SmtpClient(txtSMTPServer.Text);
            emailClient.Send (message);
            litStatus.Text="Message Sent";
        catch (Exception ex) 

Updated 20-Dec-10 2:13am
Toniyo Jackson 20-Dec-10 8:13am    
Always write your code inside the code block.

What exception are you getting? What's the value of xtSMTPServer.Text? Most SMTP servers require credentials in order to send mail, and you're not setting any that I can see.
Share this answer
Try this,
MailMessage msgMail = new MailMessage();
SmtpClient smtp = new SmtpClient("servername", "portno");
msgMail.From = new MailAddress("fromaddress", "from name");
msgMail.To.Add(new MailAddress("toaddress"));

msgMail.Subject = "Subject";
msgMail.Body = "Body";
Share this answer
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:
     <smtp from="">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />

If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
Share this answer
Please try this one..
public void SendMail(string to, string subject, string body
            , string mailUserName, string mailPassword
            , string smtpHost, string mailFrom, bool isBodyHtml
            , int smtpPort, bool enableSSL)
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress(mailFrom);
            mailMsg.Subject = subject;
            mailMsg.IsBodyHtml = true;
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.Body = body;
            mailMsg.Priority = MailPriority.Normal;
            // Smtp configuration
            SmtpClient client = new SmtpClient();
            client.Credentials = new NetworkCredential(mailUserName, mailPassword);
            client.Port = smtpPort;
            client.Host = smtpHost;
            client.EnableSsl = enableSSL;
            catch (SmtpException smtpException)
                throw smtpException;

You may set the username, password etc by reading from the configuration file
Share this answer
Manfred Rudolf Bihy 20-Dec-10 10:08am    
Please use the <pre> code tags. Otherwise it's nearly impossible to read. Thanks!
Renjith.K.C 20-Dec-10 10:12am    
Thanx Bihy...Thanx for notifying about "<pre> code tags".

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