Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void Sendmail()
    {
        string emailBody;
        try
        {
            emailBody = "<html><body";
            emailBody += "table";
            emailBody += "<tr><td> Title</td><td>"+txt_Title.Text+"</td></tr>";
            emailBody += "<tr><td> Name</td><td>" + txt_Name.Text + "</td></tr>";
            emailBody += "<tr><td> Email</td><td>" + txt_Email.Text + "</td></tr>";
            emailBody += "<tr><td> Contact</td><td>" + txt_Contact.Text + "</td></tr>";
            emailBody += "<tr><td> Message</td><td>" + txt_Description.Text + "</td></tr>";
            emailBody += "</table>";
            emailBody += "</body></html>";
            
            
            MailMessage msg = new MailMessage("fromgamil","togmail","Request",emailBody);
            SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
            NetworkCredential netcrd = new NetworkCredential("mygmailaddress","password");
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = netcrd;
            mailClient.EnableSsl = true;
            msg.IsBodyHtml = true;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(msg);
            mailClient.Timeout = 20000;
        }
        catch(Exception ex)
        {

        }

and in web.config file
XML
<system.net>
    <mailSettings>
      <smtp from="mygmailaddress">
        <network host="smtp.gmail.com" port="587" userName="mygmailusername" password="gmailpassword" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

give the right solution for this problem
Posted
Updated 12-Mar-13 20:07pm
v2
Comments
Vani Kulkarni 13-Mar-13 2:15am    
Are you able to see any error? If so what is it? Posting code is fine, also highlight or explain the issue you are facing using Improve Question.
Shubham Choudhary 13-Mar-13 2:26am    
why are you using web.config file!!! if you provides password in page !!!
and second thing what is the error you are faceing
Hari Ram Jat 14-Mar-13 6:26am    
mail is not sending !
Shubham Choudhary 14-Mar-13 6:40am    
so!!! are you try on google!!!
and what is the error comming

1 solution

C#
public static string SendMail(string from, string to, string subject, string body)
    {
        MailMessage mailMessage = new MailMessage();

        mailMessage.From = new MailAddress(from);
        mailMessage.To.Add(to);
        mailMessage.Subject = subject;
        mailMessage.IsBodyHtml = true;
        mailMessage.BodyEncoding = Encoding.UTF8;
        mailMessage.Body = body;
        //  mailMessage.Attachments = attach;
        mailMessage.Priority = MailPriority.Normal;

        SmtpClient client = new SmtpClient();
        client.Credentials = new NetworkCredential("mygmailaddress", "password");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;  
        MailMessage message = mailMessage;
       
        try
        {
            client.Send(message);
            return "true";
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();

        }
    }

This Code Works fine....
 
Share this answer
 
v2

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