0

having problems with this codes. When tested it keeps giving me the error message "Your message failed to send, please try again. "

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net.Mail;
using System.Net;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("reciever@domain.com");
            //Configure the address we are sending the mail from
            MailAddress address = new MailAddress("sender@gmail.com");
            msg.From = address;
            //Append their name in the beginning of the subject
            msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
            msg.Body = txtMessage.Text;

            //Configure an SmtpClient to send the mail.
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true; //only enable this if your provider requires it
            //Setup credentials to login to our sender email address ("UserName", "Password")
            NetworkCredential credentials = new NetworkCredential("sender@gmail.com", "XXXXXXX");
            client.Credentials = credentials;

            //Send the msg
            client.Send(msg);

            //Display some feedback to the user to let them know it was sent
            lblResult.Text = "Your message was sent!";

            //Clear the form
            txtName.Text = "";
            txtMessage.Text = "";
        }
        catch
        {
            //If the message failed at some point, let the user know
            lblResult.Text = "Your message failed to send, please try again.";
        }
    }
}
4
Contributors
5
Replies
14
Views
2 Years
Discussion Span
Last Post by DaveAmour
0

if you get rid of the try..catch block, asp.net should provide you with the actual error. At the moment, you are seeing that message because an error is causing the catch block to execute.

0

To see the real error, you could use:

try
{
    ...
}
catch(Exception ex)
{
//If the message failed at some point, let the user know
lblResult.Text = "Your message failed to send, please try again. Details: " + ex.ToString();
}
0

I have some working gmail code as below.

See if this helps you fix your problem.

        using (var mailMessage = new MailMessage(ApplicationConfig.Instance.EmailSenderAddress, recipient, subject, body))
        {
            mailMessage.IsBodyHtml = true;

            var basicCredentials = new NetworkCredential("myusername@gmail.com", "XXXXXXXXXXXXXXXXXXX");

            var smtpClient = new SmtpClient("smtp.gmail.com")
            {
                UseDefaultCredentials = false,
                Credentials = basicCredentials,
                EnableSsl = true
            };

            smtpClient.Send(mailMessage);
        }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.