Hi guys,

Pls i am newbie to asp.net website and i am building a page in my website called contact us.

i want to create a simple contact us form which will send the messages in the content of the form to my email.

i have tried a few times but it doesnt work.

I will be grateful if anybody can help me out.

thanks in advance.

Dani AI

Generated

Short, practical summary for a contact-us form that reliably sends mail. Common reasons a working form fails are: SMTP settings missing or wrong, hosting providers blocking outbound SMTP, using an outdated mail API, or sending from an address that fails SPF/DMARC checks. ’s class is a useful starting point, but it uses the older System.Web.Mail namespace and contains a likely typo in Send() that references _smtp instead of the SMTP field shown earlier; that will prevent the SMTP server from being set. For classic ASP.NET on the .NET Framework prefer System.Net.Mail; for newer ASP.NET Core apps prefer a library such as MailKit.

A minimal, modern approach (System.Net.Mail) looks like this:

using System.Net;
using System.Net.Mail;

var msg = new MailMessage();
msg.From = new MailAddress("no-reply@yourdomain.com", "Website");
msg.To.Add("owner@yourdomain.com");
msg.ReplyToList.Add(new MailAddress("visitor@example.com"));
msg.Subject = "Contact form subject";
msg.Body = "Message body here";

using (var smtp = new SmtpClient("smtp.yourhost.com", 587))
{
    smtp.Credentials = new NetworkCredential("smtpuser", "smtppass");
    smtp.EnableSsl = true;
    smtp.Send(msg); // or SendMailAsync in async code
}

Practical troubleshooting checklist (apply in order): confirm the SMTP host, port and credentials; enable SSL/TLS if required; verify the From address belongs to the sending domain and place the visitor’s address in ReplyTo; catch and log SmtpException (StatusCode and InnerException often show the provider response); test connectivity from the server (telnet or a simple console app) to the SMTP port; check hosting docs for blocked ports or required relay settings; and inspect mail server / spam filter bounce messages for SPF/DKIM/DMARC failures. Tutorials referenced earlier by and can help with step-by-step configuration, but the key is to capture the exact exception and SMTP response string — that is the most useful data to diagnose a failed send.

Recommended Answers

All 4 Replies

I've always used this class.. always worked like a charm.. and very easy to implement:

using System;
    using System.Web.Mail;
    using System.Collections;

    namespace Service.Classes
    {
        /// <summary>
        /// Summary description for Email.
        /// </summary>
        public class Email : IDisposable
        {

            #region –DECLARATIONS–

                private string strSMTP = string.Empty;
                private string strFrom = string.Empty;

                MailMessage mail;

            #endregion

            #region –PROPERTIES–

                public string SMTP
                {
                    get
                    {
                        return strSMTP;
                    }
                    set
                    {
                        strSMTP = value;
                    }
                }    

                public string From
                {
                    get
                    {
                        return strFrom;
                    }
                    set
                    {
                        strFrom = value;
                    }
                }    

            #endregion

            #region –CONSTRUCTORS & DESTRUCTOR–
        
                /// <summary>
                /// Creates an instance of an email message
                /// </summary>
                public Email()
                {
                    mail = new MailMessage();
                }

                ~ Email()
                {
                    Dispose();
                }
            #endregion

            #region –PUBLIC METHODS–
                /// <summary>
                /// Sets the email message attributes
                /// </summary>
                /// <param name="attachmentPath">The full path of the attachment, or NULL if no attachment</param>
                /// <param name="to">Destination email address</param>
                /// <param name="bcc">BCC email address, or NULL if no</param>
                /// <param name="cc">CC email address, or NULL if no</param>
                /// <param name="subject">The subject of the message, or NULL if no</param>
                /// <param name="mp">The priority of the message</param>
                /// <param name="body">The body of the message</param>
                /// <returns>Returns OK if all properties are successfully added, or an exception</returns>
                public string SetMail(string attachmentPath, string to, string bcc, string cc, string subject, MailPriority mp, string body)
                {
                    try
                    {
                        if (attachmentPath != null)
                        {
                            MailAttachment ma = new MailAttachment(@" +attachmentPath + ");
                            mail.Attachments.Add(ma);
                        }

                        mail.To = to;

                        if (bcc != "NULL")
                        {
                            mail.Bcc = bcc;
                        }

                        if (cc != "NULL")
                        {
                            mail.Cc = cc;                        
                        }
                        
                        if (subject != "NULL")
                        {
                            mail.Subject    = subject;
                        }

                        mail.From        = strFrom;
                        mail.BodyFormat = MailFormat.Text;
                        mail.Priority    = mp;
                        mail.BodyFormat = MailFormat.Text;
                        mail.Body        = body;
                        
                        return "OK";

                    }
                    catch (Exception e)
                    {
                        return e.Message.ToString();
                    }
                }

                /// <summary>
                /// Sends the email message
                /// </summary>
                public void Send()
                {        
                    if (strSMTP != string.Empty)
                    {
                        SmtpMail.SmtpServer = _smtp;
                        SmtpMail.Send(mail);
                    }
                    else
                    {
                        throw new ApplicationException("SMTP server is not set");
                    }
                }
            #endregion

            #region IDisposable Members
            
                private Component _component    = new Component();
                private bool disposed            = false;
                
                /// <summary>
                /// Disposes the object
                /// </summary>
                public void Dispose()
                {
                    if(!this.disposed)
                    {
                        _component.Dispose();
                    }
                    disposed = true;         
                }

            #endregion

        }
    }

P.S. if it was helpful, please set this thread as SOLVED.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.