How can I send a comment by email?

I have text fields like name, email address, phone number. and comment on my page.

I need to send the values ​​to my email address ..

How can I do it? I do:

protected void btnSubmit_Click(object sender, EventArgs e) { SmtpClient client = new SmtpClient(); MailMessage message = new MailMessage(); try { MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text); SmtpClient.Host = "localhost"; SmtpClient.Port = 25; message.From = fromAddress; message.To.Add(" xyz@gmail.com "); message.Subject = "Feedback"; message.IsBodyHtml = false; message.Body = txtComment.Text; SmtpClient.Send(message); Response.Write("Email successfully sent."); } catch (Exception ex) { Response.Write("Send Email Failed." + ex.Message); } } 

and I get the following error:

 An object reference is required for the nonstatic field, method, or property 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)' 
+2
source share
4 answers
  SmtpClient.Host = "localhost"; SmtpClient.Port = 25; ~~~~~~~~~~~~~~~~~~~~ SmtpClient.Send(message); 

These lines try to use members of the SmtpClient class. However, since these members are not defined as static , you need to refer to an instance of this class that you called client .

Try

  client.Host = "localhost"; client.Port = 25; ~~~~~~~~~~~~~~~~~~~~ client.Send(message); 

Also, read this article about the differences between class and instance members.

Finally, since SmtpClient implements IDisposable, I would modify your code to wrap it in with a block, as this will ensure proper cleanup after the SMTP session ends.

 using (SmtpClient client = new SmtpClient()) { // YOUR CODE } 
+2
source
 public static string sendMail(string to, string title, string subject, string body) { try { MailMessage mail = new MailMessage(); SmtpClient smtp = new SmtpClient(); MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text); if (to == "") to = fromAddress.Address; MailAddressCollection m = new MailAddressCollection(); m.Add(to); mail.Subject = subject; mail.From = fromAddress; mail.Body = body; mail.IsBodyHtml = true; mail.ReplyTo = fromAddress; mail.To.Add(m[0]); smtp.Host = "smtp.gmail.com"; smtp.Port = 25; smtp.EnableSsl = true; smtp.Credentials = new System.Net.NetworkCredential(fromAddress.Address, "XXXX");//"XXXX" is the password of the sender. ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; smtp.Send(mail); return "done"; } catch (Exception ex) { return ex.Message; } } 
0
source

I did this: -

 protected void btnSubmit_Click(object sender, EventArgs e) { MailMessage mailmsg = new MailMessage(); string name = txtName.Text.ToString(); string contact = txtPhoneNo.Text.ToString(); string comment = txtComment.Text.ToString(); string checkIn = txtCheckIn.Text.ToString(); string from = txtEmail.Text.ToString(); mailmsg.To.Add(" recipientemailid@gmail.com "); mailmsg.From = new MailAddress(from.Trim()); mailmsg.Subject = "Test message"; mailmsg.Body = "This Query is submitted by user " + name.Trim() + ", "+ contact.Trim() + ", " + comment.Trim() + ", " + checkIn.Trim(); mailmsg.IsBodyHtml = true; SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.EnableSsl = true; NetworkCredential credentials = new NetworkCredential(" recipientemailid@Gmail.com ", "password"); client.Credentials = credentials; try { //try to send the mail message client.Send(mailmsg); Response.Write("sent!"); } catch { //some feedback if it does not work Response.Write("failed!"); } } 
0
source

Try

 client.Host = "localhost"; client.Port = 465; ~~~~~~~~~~~~~~~~~~~~ client.Send(message); 

Do not attempt to use or manage disposable emails, because you will be clearly exposed to spam traps. Your code is open and it is very easy to determine who uses the code and where it comes from.

0
source

Source: https://habr.com/ru/post/888816/


All Articles