Click here to Skip to main content
14,390,578 members
Rate this:
Please Sign up or sign in to vote.
See more:
This is what the error says and line 26 is where it says client.Send(message);

at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at ContactUsForm.send_Click(Object sender, EventArgs e) in d:\10Ap\prototype\ContactUsForm.aspx.cs:line 26

What I have tried:

This is the code i'm using

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

    }

    protected void send_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage message = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;

            client.Credentials = new System.Net.NetworkCredential("smajwegmail.com", "Password here");

            client.Send(message);

            status.Text = "Mail was send successfully...!";

            from.Text = "";
            to.Text = "";
            subject.Text = "";
            body.Text = "";
        }

        catch (Exception ex)
        {
            status.Text = ex.StackTrace;
        }
    }
}

		    
Posted
Updated 10-Apr-19 16:52pm
Comments
Dave Kreskowiak 10-Apr-19 19:42pm
   
What's the error message? What you posted is the stack trace where the error occurred.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The SmtpFailedRecipientException pretty much states that in it's current state that this email cannot be deliver to the recipient.

You would be most likely get better results if you added a specific catch for this exception and review the Message property or any InnerException. Here is a simplified version; they key with "stacked" catches() is that the generic Exception EX needs to be last as it will catch any exception that hits that line
try { /* your code */ }
catch (SmtpFailedRecipientException SX) { status.Text = SX.Message; }
catch (Exception EX) { status.Text = ex.StackTrace; }
I do find your code a little different than most contact forms; usually the to/from are hardcoded to prevent the form from being for nefarious purposes. This could actually be causing the problem if the network credentials being used do not have permission to send on behalf of the from address being used. Google is pretty tight on enforcing the rules.

Another common issue when using gmail is that the account being used must have the Allow less secure apps... setting be in the on position.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100