SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot pavanpuligandla's Avatar
    Join Date
    Sep 2008
    Location
    hyderabad
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    formatting an email in c#

    Hii..
    i wrote a sample c# code for sending an email, but my problem is i would like to send it like a formatted text.
    eg: From :
    Messgae Subject : Subject
    Message Body : Body, FirstName LastName, telephone, mobile, address
    all in break lines.as i'm new to this c# programming, can anyone pls help me out..
    below is my c# code
    Code Csharp:
    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.Drawing;
     
    namespace c.c._contact
    {
        public partial class c_contact : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
     
            }
     
            protected void Button_Click(object sender, EventArgs e)
            {
                if (ValidateForm1() == false)
                {
                    SendMail();
                }
            }
     
            private void SendMail()
            {
                try
                {
     
                    MailMessage mail = new MailMessage();
                    mail.To = "xyz@somedomain.com";
                    mail.From = Email.Text;
                    mail.Subject = ("Title:" + Title.Text ("FirstName:") + FirstName.Text ("LastName:") + LastName.Text);
                    mail.Body = Message.Text;
                    //label.Text = "Please Wait...";
                    SmtpMail.SmtpServer = "localhost";
                    SmtpMail.Send(mail);
                    lblStatus.Text = "Your enquiry has been submitted successfully and we will get back to you as soon as possible.";
                    //Clear Form fields
                    FirstName.Text = " ";
                    LastName.Text = " ";
                    AddressLine1.Text = " ";
                    AddressLine2.Text = " ";
                    County.Text = " ";
                    Town.Text = " ";
                    Postcode.Text = " ";
                    Country.Text = " ";
                    Mobile.Text = " ";
                    Telephone.Text = " ";
                    Email.Text = " ";
                    Subject.Text = " ";
                    Message.Text = " ";
                    Required.Text = " ";
                }
                catch (Exception ex)
                {
                    lblStatus.Text = "Send Email Failed." + ex.Message;
                }
     
     
                //label.Text = "Your Enquiry Has Been Submitted, We will Get Back to you sooner.";
                //Response.Write("Your Enquiry Has Been Submitted, We will Get Back to you sooner.");
     
            }
     
            protected bool ValidateForm1()
            {
                bool ErrorsFound;
                Required.Text = "* denotes required field";
                ErrorsFound = false;
     
                if (this.FirstName.Text == "" || this.FirstName.Text == null)
                {
                    this.lblFirstName.Text = "*";
                    this.lblFirstName.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblFirstName.Text = "";
                }
     
                if (this.LastName.Text == "" || this.LastName.Text == null)
                {
                    this.lblLastName.Text = "*";
                    this.lblLastName.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblLastName.Text = "";
                }
     
                if (this.AddressLine1.Text == "" || this.AddressLine1.Text == null)
                {
                    this.lblAddressLine1.Text = "*";
                    this.lblAddressLine1.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblAddressLine1.Text = "";
                }
     
                if (this.Town.Text == "" || this.Town.Text == null)
                {
                    this.lblTown.Text = "*";
                    this.lblTown.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblTown.Text = "";
                }
     
                if (this.County.Text == "" || this.County.Text == null)
                {
                    this.lblCounty.Text = "*";
                    this.lblCounty.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblCounty.Text = "";
                }
     
                if (this.Postcode.Text == "" || this.Postcode.Text == null)
                {
                    this.lblPostcode.Text = "*";
                    this.lblPostcode.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblPostcode.Text = "";
                }
     
                if (this.Country.Text == "" || this.Country.Text == null)
                {
                    this.lblCountry.Text = "*";
                    this.lblCountry.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblCountry.Text = "";
                }
     
     
                if (this.Telephone.Text == "" || this.Telephone.Text == null)
                {
                    this.lblTelephone.Text = "*";
                    this.lblTelephone.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblTelephone.Text = "";
                }
     
                if (this.Email.Text == "" || this.Email.Text == null)
                {
                    this.lblEmail.Text = "*";
                    this.lblEmail.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblEmail.Text = "";
                }
     
     
                if (this.Subject.Text == "" || this.Subject.Text == null)
                {
                    this.lblSubject.Text = "*";
                    this.lblSubject.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblSubject.Text = "";
                }
     
                if (this.Message.Text == "" || this.Message.Text == null)
                {
                    this.lblMessage.Text = "*";
                    this.lblMessage.ForeColor = Color.Red;
                    ErrorsFound = true;
                }
                else
                {
                    this.lblMessage.Text = "";
                }
     
                return ErrorsFound;
     
            }
        }
    }
    PHP Professional turned ASP.NET Professional

  2. #2
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Clogland
    Posts
    322
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For a line break your can use:
    Code Csharp:
    Environment.NewLine
    or
    Code Csharp:
     "\r\n"

    Also, instead of
    Code Csharp:
    if (this.FirstName.Text == "" || this.FirstName.Text == null)
    you can use
    Code Csharp:
    if (String.IsNullOrEmpty(FirstName.Text))

    Edit: Another option is to send your email as HTML:
    Code Csharp:
    mail.IsBodyHtml = true;
    and then use HTML markup for the new line "<br />"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •