Click here to Skip to main content
14,241,400 members
Rate this:
Please Sign up or sign in to vote.
I use this code for send mail ,

var smtp = new System.Net.Mail.SmtpClient();
                    {
                        //smtp.Host = "smtp.gmail.com";
                        //smtp.Port = 587;
                        //smtp.EnableSsl = true;

                        smtp.Host = "localhost";
                        smtp.Port = 25;
                        smtp.EnableSsl = false;
                        
                        smtp.UseDefaultCredentials = false;
                        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                        smtp.Credentials = new NetworkCredential(fromMail.ToString(), "2345664");
                        smtp.Timeout = 20000;

                    }
                    smtp.Send(Msg);
                    reader.Dispose();


using this method i can't send mail,from both local and server side.
i got this error -when i run my code on localhost

No connection could be made because the target machine actively refused it 127.0.0.1:25

but i also enable to send mail from server side.

can anyone tell me reason for that,and give solution for that.?
Posted
Updated 24-Dec-13 18:36pm
v2
Comments
   
Are you sure your localhost executes a SMTP server? :-)
—SA
ketan italiya 25-Dec-13 0:35am
   
actually i enable to send mail from server not from local i got this error when i run my code in local,

No connection could be made because the target machine actively refused it 127.0.0.1:25

but i enable to send mail,now i can't.
   
Where is your SMTP server? On your localhost? It's hard to believe... :-)
—SA
Mahesh Bailwal 25-Dec-13 1:01am
   
This error means that on the machine (server or local machine) where your code is running SMTP service is not available on port 25.
You need to check whether SMTP service is properly configured and running on the machine you are pointing to using "smtp.Host". Check below link "Setting up SMTP on IIS 7"

http://forums.iis.net/t/1157046.aspx
Rate this:
Please Sign up or sign in to vote.

Solution 2

Thanks a lot for giving solution,

now i use this code for sending mail,
protected void getDataforemail(int invitationID)
      {

          try
          {
              EventManagerDataContext db = new EventManagerDataContext();

              {
                  var q = (from a in db.EMR_EVENTs
                           join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID
                           join c in db.EMR_INVITATIONs on a.EventID equals c.EventID

                           where c.InvitationID == invitationID

                           select new
                           {
                               EventID = a.EventID,
                               Client_Name = b.Name,
                               Event_Name = a.Name,
                               Activation_Code = c.Activation_Code

                           });

                  var ev = q.FirstOrDefault();

                  StreamReader reader = new StreamReader(Server.MapPath("~/HTMLPage1.htm"));
                  string readFile = reader.ReadToEnd();
                  string myString = "";
                  myString = readFile;



                  MailAddress fromMail = new MailAddress("ketanitaliya16@gmail.com");

                  Msg.CC.Add(new MailAddress(clientEmail(getClientId(lblclientname.Text))));


                  myString = myString.Replace("$$Client_Name$$", ev.Client_Name);
                  myString = myString.Replace("$$Event_Name$$", ev.Event_Name);
                  myString = myString.Replace("$$Activation Code$$", ev.Activation_Code);

                  Msg.From = fromMail;


                  // Subject of e-mail
                  Msg.Subject = "Send Mail with HTML File";
                  Msg.Body = myString.ToString();
                  Msg.IsBodyHtml = true;






                  var smtp = new System.Net.Mail.SmtpClient();
                  {
                      smtp.Host = "smtp.gmail.com";
                      smtp.Port = 587;
                      smtp.EnableSsl = true;


                     smtp.UseDefaultCredentials = false;
                      smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                      smtp.Credentials = new NetworkCredential(fromMail.ToString(), "xxxxxxxx");
                      smtp.Timeout = 20000;

                  }
                  smtp.Send(Msg);
                  reader.Dispose();

              }
          }
          catch (Exception)
          {

              throw;
          }
      }

      protected void getInvitationID(int EventID)
      {
          try
          {
              EventManagerDataContext db = new EventManagerDataContext();
              {
                  var q = (from a in db.EMR_INVITATIONs
                           where a.EventID == EventID
                           select new
                           {
                               a.InvitationID
                           });



                  foreach (var inv in q)
                  {
                      var p = (from a in db.EMR_INVITATIONs
                               where a.InvitationID == inv.InvitationID
                               select new
                               {
                                   a.Email_Address,
                               });



                      foreach (var emailadd in p)
                      {
                          Msg = new MailMessage();
                          Msg.To.Add(new MailAddress(emailadd.Email_Address));
                          getDataforemail(inv.InvitationID);
                      }

                  }
              }
          }
          catch (Exception)
          {

              throw;
          }
      }


with using this code i can send mail from local server but when i uploaded it on server i can't send mail from server.

i got this error,
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

i find that mail is not send can anyone tell me solution for that.?
   
v2
Comments
JoCodes 25-Dec-13 2:25am
   
The code looks all. Check the password is correct or not. And finally , check
http://rochcass.wordpress.com/2013/05/25/error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated-the-server-response-was-5-5-1-authentication-required/
ketan italiya 25-Dec-13 4:27am
   
i solve it. with this code,thanks.
JoCodes 25-Dec-13 5:47am
   
You are welcome...

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