Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
        }
    }
}
C#

Posted
Updated 10-Apr-19 15: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

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
C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900