Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear friends please tell me how to send textbox1*, textbox2, textbox3*, textbox4*, textbox5, textbox6.(*compulsory field)

on submit_click -> it has to send all text fields to gmail account directly

i am trying on this from very long days so please tell me wit detail code and explanation. i am sorry if i am expecting more from this form like asking detail code and explanation whenever my friends are free please consider my request

Thanks for support


NOW I HAVE STUCKUP HERE PLEASE HELP ME PLEASE PLEASE..

this is error i am getting " This message may not have been sent by: karthikh87@gmail.com Learn more Report phishing " please help me resolving this issue and code is shown below

using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
 
public partial class _Default : System.Web.UI.Page 
{
      #region   "Send email"
      protected void btnSendmail_Click(object sender, EventArgs e)
      {
            // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
            // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();
 
            try
            {
                  MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
 
                  // You can specify the host name or ipaddress of your server
                  // Default in IIS will be localhost 
                  smtpClient.Host = "localhost";
 
                  //Default port will be 25
                  smtpClient.Port = 25;
 
                  //From address will be given as a MailAddress Object
                  message.From = fromAddress;
 
                  // To address collection of MailAddress
                  message.To.Add("karthikh87@gmail.com");
                  message.Subject = "Feedback";
 
                  // CC and BCC optional
                  // MailAddressCollection class is used to send the email to various users
                  // You can specify Address as new MailAddress("admin1@yoursite.com")
                  message.CC.Add("karthikh87@gmail.com");
                  message.CC.Add("karthikh87@gmail.com");
 
                  // You can specify Address directly as string
                  message.Bcc.Add(new MailAddress("karthikh87@gmail.com"));
                  message.Bcc.Add(new MailAddress("karthikh87@gmail.com"));
 
                  //Body can be Html or text format
                  //Specify true if it   is html message
                  message.IsBodyHtml = false;
 
                  // Message body content
                  message.Body = txtMessage.Text;
            
                  // Send SMTP mail
                  smtpClient.Send(message);
 
                  lblStatus.Text = "Email successfully sent.";
            }
            catch (Exception ex)
            {
                  lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
            }
            Response.Redirect("index.html");
      }
      #endregion
 
      #region "Reset"
      protected void btnReset_Click(object sender, EventArgs e)
      {
            txtName.Text = "";
            txtMessage.Text = "";
            txtEmail.Text = "";
      }
      #endregion
}
Posted
Updated 16-Jan-12 1:03am
v3
Comments
karthikh87 30-Jan-12 0:55am    
i want to know what is mail.yourservername.com and how can i get it..??

See here, it gives a generic email method: Sending an Email in C# with or without attachments: generic routine.[^]
All you have to do is read the data from your fields, and pass that as the "body" parameter along with the other necessary parameters.
 
Share this answer
 
Comments
Tech Code Freak 15-Jan-12 1:23am    
5up!
karthikh87 31-Jan-12 12:28pm    
i want to know what is mail.yourservername.com and how can i get it..??
OriginalGriff 31-Jan-12 14:01pm    
You will have to talk to / look at the website of your hosting service (or email supplier) - they will specify the SMTP address you need - you can't necessarily guess it or work it out.
pl check the sample code in the following thread:

http://www.java2s.com/Code/ASP/Components/SendoutemailincodebehindC.htm[^]

also see the following thread if you want to use Gmail for sending emails:

http://www.daniweb.com/web-development/aspnet/threads/287044"[^]
 
Share this answer
 
v2
Hi,

smtpClient.Host is surely "smtp.gmail.com"..

Cheers
 
Share this answer
 
Hello,

First of all declare one stringbuilder variable. append whatever you want to send in stringbuilder.make one function sendmail with four parameters like this

C#
public bool Sendmail(string mailFrom, string mailTo, string mailSubject, string mailBody)
    {

        if (str_MailComponent == "SMTP")
        {
            return SendMailBySmtp(mailFrom, mailTo, mailSubject, mailBody);
        }
        else
        {
            return false;
        }

    }

then get all the correct details of server, use below function to send the mail, just copy and paste it will work.

C#
public bool SendMailBySmtp(string from, string to, string subject, string body)
    {
        try
        {
            // Instantiate a new instance of MailMessage
            MailMessage ObjMailMessage = new MailMessage();

            // Set the sender address of the mail message
            ObjMailMessage.From = new MailAddress(from);

            // Set the recepient address of the mail message
            ObjMailMessage.To.Add(new MailAddress(to));

            // Set the subject of the mail message
            ObjMailMessage.Subject = subject;
            // Set the body of the mail message
            ObjMailMessage.Body = body;

            // Set the format of the mail message body as HTML
            ObjMailMessage.IsBodyHtml = true;

            // Set the priority of the mail message to normal
            ObjMailMessage.Priority = MailPriority.Normal;

            // Instantiate a new instance of SmtpClient           
            SmtpClient ObjSmtpClient = new SmtpClient(str_ServerDetails);
            ObjSmtpClient.Port = 25;
            
            //ObjSmtpClient.Credentials = CredentialCache.DefaultCredentials;

            if (!String.IsNullOrEmpty(str_UserName))
            {
                NetworkCredential ObjNC = new NetworkCredential(str_UserName, str_Password);
                ObjSmtpClient.Credentials = ObjNC;
            }
            else
            {
                ObjSmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
            }

            // Send the mail message
            ObjSmtpClient.Send(ObjMailMessage);
            return true;
        }
        catch (SmtpException ex)
        {
            //HandelError ObjhandleError = new HandelError();
            //ObjhandleError.ManageError(ex.Message, "SendMailClass.sendMailbySMTP()");
            //return false;
        }
        catch (FormatException ex)
        {
            //HandelError ObjhandleError = new HandelError();
            //ObjhandleError.ManageError(ex.Message, "SendMailClass.sendMailbySMTP()");
            //return false;
        }
        catch (ArgumentException ex)
        {
            //HandelError ObjhandleError = new HandelError();
            //ObjhandleError.ManageError(ex.Message, "SendMailClass.sendMailbySMTP()");
            //return false;
        }

        catch (InvalidOperationException ex)
        {
            //HandelError ObjhandleError = new HandelError();
            //ObjhandleError.ManageError(ex.Message, "SendMailClass.sendMailbySMTP()");
            //return false;
        }
        catch (System.Security.SecurityException ex)
        {
            //HandelError ObjhandleError = new HandelError();
            //ObjhandleError.ManageError(ex.Message, "SendMailClass.sendMailbySMTP()");
            //return false;
        }
        catch (ConfigurationErrorsException ex)// IN CASE OF SHARED HOSTING
        {
            //HandelError ObjhandleError = new HandelError();
            //ObjhandleError.ManageError(ex.Message, "SendMailClass.sendMailbySMTP()");
            //return false;
        }

        return false;
    }




pass the correct parameters to function.
 
Share this answer
 
v5
First add two Namespaces

C#
using System.Net;
using System.Net.Mail;


Use this code in the Button_Click event

XML
SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.EnableSsl = true;

            NetworkCredential credentials = new NetworkCredential("fromwhich mail you want to send mail(gmail)", "(your gmail password)");
            //NetworkCredential credentials = new NetworkCredential("abc@gmail.com", "XXXXXX");

            client.UseDefaultCredentials = false;
            client.Credentials = credentials;

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("suman251189@gmail.com");

           //add as many e-mail address as you want(any gmail,yahoo, rediff,etc)
            mail.To.Add(new MailAddress("suman251189@gmail.com"));           
            mail.To.Add(new MailAddress("rvsce.2008@gmail.com"));
            

            mail.Subject = "Test of Automation mail";

            mail.IsBodyHtml = true;

            mail.Body = "<html><body>";

            //Create your mail body
            mail.Body +="<br />Hi All,<br /><br />This is my first mail by scheduler task.<br /><br />Please tolerate me one or two day because it will come on every 15 mins automatically whenever my net will remain start.<br /><br />";
                     
            mail.Body +="I am checking this task so please .............tolerate me......<br /><br />";

            mail.Body +="</body></html>";


Please make it resolved if this help with your issue.

Thanks and Regards
Suman
 
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