1

There seems to be quite some duplicates out there but its all from quite a number of years old and since, Gmail has updated its security features, most of them codes returns an error.

Is there a updated code to achieve this purpose.

The code I am using is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.Net;
namespace uni{

public partial class contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void SendMail()
    {
        var fromAddress = "";// Gmail Address from where you send the mail
        var toAddress = mail.Value.ToString();
        const string fromPassword = "";//Password of gmail address
        string subject = "User Query";
        string body = "From: " + n.Text + "\n";
        body += "Email: " + em.Text + "\n";
        body += "Query: \n" + mssg.Text + "\n";

        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }

        smtp.Send(fromAddress, toAddress, subject, body);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
            con.Open();
            string inscmd = " Insert into Feedback (Name, Email, Message) values (@Name, @Email, @Message)";
            SqlCommand insert = new SqlCommand(inscmd, con);
            insert.Parameters.AddWithValue("@Name", n.Text);
            insert.Parameters.AddWithValue("@Email", em.Text);
            insert.Parameters.AddWithValue("@Feedback", mssg.Text);

            try
            {
                insert.ExecuteNonQuery();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' Your query was successfully submitted.')", true);
                con.Close();

                try
                {
                    SendMail();
                    Response.Redirect("Thanks.html");
                }
                catch (Exception) { }
            }
            catch (SqlException)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' There was some error please try again.')", true);
            }
        }
        catch (Exception)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' There was some error please try again.')", true);
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lemdor
  • 150
  • 3
  • 15
  • Hi @Lemdor, What exactly is the problem? –  Dec 18 '16 at 19:38
  • I get a "There was some error please try again." error. the message i send from the contact form doesnt get send at all @FernandaInesDuran – Lemdor Dec 18 '16 at 20:13
  • Possibly firewall config? Have you tried allowing outbound connections on port 587? – Scotty Dec 19 '16 at 03:17
  • ummm...not sure how i can do that @Scotty any tips? – Lemdor Dec 19 '16 at 07:30
  • 1
    Just because an answer is old does not mean it isn't valid. Guess how old the SMTP protocol is? There are a ton of working examples on SO (http://stackoverflow.com/q/298363/964043). – dmarietta Dec 19 '16 at 20:50
  • @dmarietta thanks for the link, however, cant seem to implement it. could you kindly put up a Fiddle using that for reference. thanks again – Lemdor Dec 21 '16 at 03:32

0 Answers0