0

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.

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by vishalrane
0

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

        }
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.