0

I am trying to design a 'Contact Us' page using ASP.NET and C#.

Here is my code-behind the 'Send' button:

 try
    {
        if (Page.IsValid)
        {



            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("SENDER@gmail.com");
            mailMessage.To.Add("RECEIVER@gmail.com"); //can have multiple here
            mailMessage.Subject = txtSubject.Text;
            mailMessage.Body = "<b>Sender Name:</b>" + txtName.Text + "<br/>"
               + "<b>Sender Email:</b>" + txtEmail.Text + "<br/>"
               + "<b>Comments:</b>" + txtComments.Text;

            //for the HTML tags mentioned in the body above
            mailMessage.IsBodyHtml = true;


            //IN THE TUTORIAL, HE IS USING GMAIL'S SMTP SERVER:
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.Credentials =
                new System.Net.NetworkCredential("SENDER@gmail.com", "PASSWORD");
            smtpClient.Send(mailMessage);

            Label1.Text = "Thank you for contacting us";
            Label1.ForeColor = System.Drawing.Color.Blue;
            txtName.Enabled = false;
            txtEmail.Enabled = false;
            txtComments.Enabled = false;
            txtSubject.Enabled = false;
            Button1.Enabled = false;
        }


    }
    catch (Exception ex)
    {
        // Log - Event Viewer or table
        Label1.ForeColor = System.Drawing.Color.Red;
        Label1.Text = "There is an unknown problem. Please try later";

    }

In the tutorial which I followed, you need to assignthe sender's email & password to the smtpClient.Credentials.

Is there a way to submit this email without having to know the sender's password?

Sweg
  • 99
  • 1
  • 14
  • Have you tried with wrong password? hope it will work. – Vijay Jan 10 '17 at 12:55
  • Have you tried using the default credentials, just to get the tutorial working? Check the example at https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx for details. – swatsonpicken Jan 10 '17 at 12:58
  • 1
    Typically, the email server requires credentials, to prevent spam. You may have a set of credentials that the Hosting server can use to send email, instead of trying to send via the same creds as the person sending the email. – Eric Burdo Jan 10 '17 at 12:59
  • Possible duplicate of [How to send a mail directly to SMTP server without authentication?](http://stackoverflow.com/questions/9763455/how-to-send-a-mail-directly-to-smtp-server-without-authentication) – chambo Jan 10 '17 at 12:59
  • @swatsonpicken The example does work. At the moment, when I click 'Submit', the email is sent From and To the admin email as I know the credentials. On the Contact Us form, the sender is asked to enter their email address, this is sent as part of the email body – Sweg Jan 10 '17 at 13:03
  • @EricBurdo At the moment, on Submit, the email is sent both To and From the admin email, while the email which the user has entered in the Contact Us form is displayed in the email body. Would this be a satisfactory way of implementing this Contact Us function? – Sweg Jan 10 '17 at 13:05

1 Answers1

0

Usually some form of authentication is required to send emails through SMTP servers for security and/or quota reasons. If you absolutely must send and email without authentication there are plenty of "no authentication" SMTP servers out there willing to send your email for you.

chambo
  • 491
  • 3
  • 8
  • I was thinking of adding another textfield where the user can enter their password to send the email, but they would most likely not be willing to do that, right? Maybe keeping the functionality as-is, would be the best – Sweg Jan 10 '17 at 13:06
  • 1
    @Sweg I think you're confusing the terms "sender" and "receiver" here. The person filling out the Contact Us form is not the "sender." The sender is just any email account that is allowed to send mail through an SMTP server. Usually web applications have an SMTP email service account setup somewhere that they use to send emails. – chambo Jan 10 '17 at 13:15