0

i want to send an e-mail to my gmail account consisting of the value in a textbox. the code below is what i'm trying to do but it's not working could you please tell me what could be my problem ? when i run the program becomes not responding than it tells me failure to send mail

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("myusername@gmail.com");
        msg.To.Add("myusername@gmail.com");
        msg.Subject = "test";
        msg.Body = txtName.Text;

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = "www.gmail.com"; 

        smtpClient.Send(msg);
user3679986
  • 53
  • 1
  • 10

1 Answers1

0

Try this,

         string mailServer, mailFrom;
         int port;
         string mailId, mailPass;
         string subject;

         StringBuilder mailBody = new StringBuilder();

         subject = "subject";
         mailBody.Length = 0;
         mailBody.Append("<html><body><div>");
         mailBody.Append(txtName.Text);
         mailBody.Append("</div></body></html>");


        mailServer = "smtp.gmail.com";
        mailFrom = "something";
        mailId = "example@gmail.com";
        mailPass = "xxxxx";
        port = 587;
        MailMessage mail = new MailMessage(mailId, mailTo, subject, mailBody.ToString());
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient(mailServer, port);
        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mailId, mailPass);
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = nc;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Send(mail);

Change the mailid, password and all with appropriate values.

Ullas
  • 11,450
  • 4
  • 33
  • 50