سؤال

Here I am in a strange situation. When I send the email from localhost it is working fine but when I upload the page to the server and try to send email, I get the following error

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

I'm displaying the error message in a label for testing purpose and the try is also misplaced I know, I will set it later.

the code I am using is

 if (Page.IsValid)
    {
try
            {
            StringBuilder message = new StringBuilder();
            message.Append("Hello My Name is ");
            message.Append(txtName.Text);
            message.AppendLine();
            message.AppendLine("My Contact Number " + txtContactNumber.Text.ToString());
            message.AppendLine();
            message.AppendLine();
            message.AppendLine("My Email Id Is " + txtFromEmailAddress.Text.ToString());
            message.AppendLine();
            message.Append(txtEmailBody.Text);


        MailMessage mailMessage = new MailMessage("xxx@gmail.com", "yyy@gmail.com");
        mailMessage.Subject = "New Client Query";
        mailMessage.Body = message.ToString();

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 25);
        //smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential()
        {
            UserName = "xxx@gmail.com",
            Password = "password"
        };
        smtpClient.EnableSsl = true;

            smtpClient.Send(mailMessage);


        txtContactNumber.Text = "";
        txtFromEmailAddress.Text = "";
        txtName.Text = "";
        txtEmailBody.Text = "";
        lblEmailStatus.Text = "Email Sent Successfully.";
        lblEmailStatus.ForeColor = System.Drawing.Color.Yellow; 
        }
        catch(Exception ex)
        {
            lblEmailStatus.Text = ex.Message + " <br> " + ex.Source;
        }
    }
    else
    {

        lblEmailStatus.Text = "Error!   Email Not Sent ";
        lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
    }

I have googled for a couple of hours and checked links at this site as well but I still cant figure it out.

Now I request you guys here if any one have any solutions / hint.

هل كانت مفيدة؟

المحلول 2

i got the reason finally .

the email i was sending emails from was kind of hacked some days before and for the security reasons google team had kind of marked my email as unsecure . i changed the emails address and it is working fine thanks you all.

نصائح أخرى

Try this

    public string SendEmailTest(String EmailMessage, String FromMail, String MailPassword, String MailServer, String To, String CC, String BCC, String DisplayName, String Subject, String Attachment)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient();

            MailMessage message = new MailMessage();

            MailAddress fromAddress;

            fromAddress = new MailAddress(FromMail);

            smtpClient.Host = MailServer;
            smtpClient.Port = 25;

            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMail, MailPassword);
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = SMTPUserInfo;

            message.From = fromAddress;

            message.To.Add(new MailAddress(To, DisplayName));
            if (CC != "")
                message.CC.Add(new MailAddress(CC, DisplayName));
            if (BCC != "")
                message.Bcc.Add(new MailAddress(BCC, DisplayName));

            message.Subject = Subject;

            message.IsBodyHtml = true;
            message.Body = EmailMessage;

            if (Attachment != "")
                message.Attachments.Add(new Attachment(Attachment));

            message.Priority = MailPriority.High;

            smtpClient.Send(message);
            return "SendEmail";
        }
        catch (Exception ex)
        {
            return "Email :" + ex;
        }

    }

Based on the Google Gmail Documentation it would appear that the Port should be 587 not 25. I found a few other questions that seem to be related here and here.

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top