0

in 3.5 .Net Framework i have a problem to sending an email on server. Code-

    MailMessage message = new MailMessage();

    message.From=new MailAddress("CMC Enquiry" + "<my1@domain.in>");
    message.To.Add(new MailAddress("vishalpatel8086@gmail.com"));

        message.CC.Add(new MailAddress("varun_aone@yahoo.com"));
        message.Subject = "Enquiry from CMC site";
        string p = "<b>Name: </b>" + TextBox1.Text;
        p += "<br><b>Mobile:</b> " + TextBox3.Text;
        p += "<br><b>Mail ID:</b> " + TextBox2.Text;
        p += "<br><b>Address:</b> " + TextBox4.Text;
        p += "<br><b>City:</b> " + TextBox5.Text;
        p += "<br><b>Location:</b>" + locationlst.SelectedItem.Text;
        p += "<br><b>College:</b> " + TextBox11.Text;
        p += "<br><b>Course:</b> " + DropDownList3.SelectedItem.Text;

        p += "<br><b>Query:</b> " + TextBox12.Text;
        message.Body = p;
        message.IsBodyHtml = true;

        SmtpClient SMTPServer = new SmtpClient("localhost");
   //     try
   //     {
            SMTPServer.Send(message);
            //result = "Your Enquiry has been Submitted !!";
            Label5.Text = "Your Enquiry has been Submitted !!";
            //Response.Write("<script language=JavaScript> alert('Your Enquiry has been Submitted !!'); </script>");
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox11.Text = "";
            TextBox12.Text = "";
            DropDownList3.SelectedIndex = 0;


  //      }
  //      catch
  //      {
   //        Label5.Text = "Low Server Problem !!Your Enquiry Not Submitted";


          //Response.Write("<script language=JavaScript> alert('Server Problem !!Your Enquiry Not Submitted'); </script>");
   //     }
        }
        else
        {
            Label5.Text = "Server Problem !!Your Enquiry Not Submitted";
        }
    }

This code is working my other web hosting server with my different website but is not working with new web site . A error is being occured like -

Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

    Error Is -
   Line 83:                 SMTPServer.Send(message);

   Source File: c:\inetpub\vhosts\cmcent.in\httpdocs\enquiry.aspx.cs    
   Line: 83
user3253285
  • 9
  • 1
  • 4
  • Looks to me like your 'new' server doesn't like being used like a relay. Should just be a case of supplying a username and password for a valid email account on the server. – laminatefish Feb 19 '14 at 10:08
  • See this answer on [using credentials with `SmtpClient`](http://stackoverflow.com/a/2766967/10427) – Geoff Feb 19 '14 at 10:47

1 Answers1

0

Your SmtpClient code needs to be revised as follow:

//Set your smtp client settings
var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.yourdomain.com"; //set with your smtp server
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential("fromEmail@yourdomain.com", "fromEmailPassword");
        smtp.Timeout = 20000;
    }
    // Now send your email with this smtp
    smtp.Send(message);

Optionally you can also try to enable/disable ssl & changing the port.

Soham
  • 1,281
  • 7
  • 15