Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can Anyone give code for mail concept:


in my form having name,phno,email,address,query this details will mail to desired mail id....ple give code for this...
Posted

Hi,

Try this...

In you web.config example:

C#
<add key="EmailTo" value="ToYou@TestEmail.com.ph;" />
<add key="EmailCc" value="ToMe@TestEmail.com.ph;" />
<add key="EmailFrom" value="FromMe@TestEmail.com" />
<add key="SMTP_Address" value="123.456.780.9" />



Code behind:

C#
public static class EmailHandler
      {
          public static string SendEmailFeedback(string subject, string message, string contact)
          {
              string strEmailResult = string.Empty;
              string strEmailFrom = string.Empty;
              string strEmailTo = string.Empty;
              string strEmailCc = string.Empty;
              string strEmailSMTP = string.Empty;

              strEmailFrom = ConfigurationSettings.AppSettings["EmailFrom"].ToString();
              strEmailTo = ConfigurationSettings.AppSettings["EmailTo"].ToString();
              strEmailCc = ConfigurationSettings.AppSettings["EmailCc"].ToString();
              strEmailSMTP = ConfigurationSettings.AppSettings["SMTP_Address"].ToString();

              try
              {
                  MailMessage emailMessage = new MailMessage();
                  SmtpClient mailClient = new SmtpClient(strEmailSMTP);
                  emailMessage.Priority = MailPriority.High;
                  emailMessage.From = new MailAddress(strEmailFrom);
                  emailMessage.Subject = subject;
                  emailMessage.Body = message;
                  emailMessage.IsBodyHtml = true;

                  if ((strEmailTo.Contains("@")) && (strEmailTo.Length > 4))
                  {
                      int count = CountStringOccurrences(strEmailTo, ";");

                      for (int i = 0; i < count; i++)
                      {
                          string strToRecieptient = strEmailTo.Split(';')[i].ToString();
                          emailMessage.To.Add(strToRecieptient);
                      }

                  }

                  if (strEmailCc != string.Empty)
                  {
                      if ((strEmailCc.Contains("@")) && (strEmailCc.Length > 4))
                      {
                          int icount = CountStringOccurrences(strEmailCc, ";");

                          for (int i = 0; i < icount; i++)
                          {
                              string strCCrecipient = strEmailCc.Split(';')[i].ToString();
                              emailMessage.Bcc.Add(strCCrecipient);
                          }
                      }
                  }

                  mailClient.Send(emailMessage);

                  strEmailResult = "Message was successfully sent to the System Administrator.";
              }
              catch (Exception ex)
              {
                  strEmailResult = ex.Message.ToString();
              }

              return strEmailResult;
          }

          private static int CountStringOccurrences(string text, string pattern)
          {
              int count = 0;
              int i = 0;
              while ((i = text.IndexOf(pattern, i)) != -1)
              {
                  i += pattern.Length;
                  count++;
              }
              return count;
          }
      }


Please do not forget to vote if could help, so that others may consider as an answer...

Regards,
 
Share this answer
 
Comments
raj ch 14-Dec-11 2:12am    
my vote of 5
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("kaarthickit1989@gmail.com");
mail.To.Add("ashok@yahoo.com");
mail.From = new MailAddress("kkaarthickit@gmail.com");
mail.Subject = "Email using Gmail";

string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}

Enjoy the Life.........
 
Share this answer
 
Comments
M.Narmatha 14-Dec-11 0:49am    
thanks a lot...
Add the Name space: using System.Net.Mail;
 
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