Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. sending mails through webservices

sending mails through webservices

Scheduled Pinned Locked Moved ASP.NET
helpquestion
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shankbond
    wrote on last edited by
    #1

    Hi, I am sending emails through webservices, I am calling the webservice from my website and passing all the arguments to it ,until now no problem. But now I want the webservice to inform the user if an error or exception occurs is there a way by which I can do so? Any help shall be appreciated

    Thanks Shankbond

    S 1 Reply Last reply
    0
    • S shankbond

      Hi, I am sending emails through webservices, I am calling the webservice from my website and passing all the arguments to it ,until now no problem. But now I want the webservice to inform the user if an error or exception occurs is there a way by which I can do so? Any help shall be appreciated

      Thanks Shankbond

      S Offline
      S Offline
      sashidhar
      wrote on last edited by
      #2

      Return the exception in the form of String format...!

      [WebMethod]
      Public string SendMail()
      {
      try
      {
      return "mail sent";
      }
      Catch(Exception ie)
      {
      return ie.Message;
      }
      }

      LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

      S 1 Reply Last reply
      0
      • S sashidhar

        Return the exception in the form of String format...!

        [WebMethod]
        Public string SendMail()
        {
        try
        {
        return "mail sent";
        }
        Catch(Exception ie)
        {
        return ie.Message;
        }
        }

        LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

        S Offline
        S Offline
        shankbond
        wrote on last edited by
        #3

        Hi, Your method is very simple but when I tried to send the mail using webservice I tried like: testingservice service=new testingservice; string message=service.send mail(,,,.....parameters); where as I have declared the web service as [WebMethod] Public string SendMail(,,,.....parameters) { try { return "mail sent"; } Catch(Exception ie) { return ie.Message; } } I get an error and the application fails to rebuild Cannot implicitly convert type 'System.IAsyncResult' to 'string' Please reply

        Thanks Shankbond

        S 1 Reply Last reply
        0
        • S shankbond

          Hi, Your method is very simple but when I tried to send the mail using webservice I tried like: testingservice service=new testingservice; string message=service.send mail(,,,.....parameters); where as I have declared the web service as [WebMethod] Public string SendMail(,,,.....parameters) { try { return "mail sent"; } Catch(Exception ie) { return ie.Message; } } I get an error and the application fails to rebuild Cannot implicitly convert type 'System.IAsyncResult' to 'string' Please reply

          Thanks Shankbond

          S Offline
          S Offline
          sashidhar
          wrote on last edited by
          #4

          Hi, You misunderstood i think..! You have to write a web method for sending mail

          [WebMethod]
          Public string SendMail(,,,.....parameters)
          {
          try
          {
          //You Have to Write the logic for sending mail..!
          return "mail sent";
          }
          Catch(Exception ie)
          {
          return ie.Message;
          }
          }

          if you wrote the logic for sending the mail show me the code..!

          LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

          S 1 Reply Last reply
          0
          • S sashidhar

            Hi, You misunderstood i think..! You have to write a web method for sending mail

            [WebMethod]
            Public string SendMail(,,,.....parameters)
            {
            try
            {
            //You Have to Write the logic for sending mail..!
            return "mail sent";
            }
            Catch(Exception ie)
            {
            return ie.Message;
            }
            }

            if you wrote the logic for sending the mail show me the code..!

            LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

            S Offline
            S Offline
            shankbond
            wrote on last edited by
            #5

            Hi, here is the code:

            protected void Button4_Click(object sender, EventArgs e)
            {
            testingservice service = new testingservice();
            AsyncCallback callback = new AsyncCallback(hello);
            service.BeginSendMailGmail1(TextBoxto.Text.ToString(), TextBoxcc.Text.ToString(), TextBoxbcc.Text.ToString(), TextBoxbody.Text.ToString(), TextBoxsubject.Text.ToString(), TextBoxusername.Text.ToString(), TextBoxpassword.Text.ToString(), TextBoxserver.Text.ToString(), TextBoxport.Text.ToString(), TextBoxdisplayname.Text.ToString(),callback,sender);
            Response.Redirect("default4.aspx");
            }

            here is the other method but it will always be completed =true

            void hello(IAsyncResult result)
            {
            if (!result.IsCompleted)
            {
            Response.Write("failed");
            }
            }

            the code for webservice:

            [WebMethod]

            public string  SendMailGmail1(string To, string Cc, string Bcc, string body,string subject, string user\_name, string password, string server\_name, string portnumber, string display\_name)
            {
               
                SmtpClient client = new SmtpClient();
                MailMessage message = new MailMessage();
                client.Port = Convert.ToInt32(portnumber);
                client.Host = server\_name;
                client.UseDefaultCredentials = true;
                client.Credentials = new System.Net.NetworkCredential(user\_name, password);
                client.EnableSsl = true;
                try
                {
                    message = new MailMessage(new MailAddress(user\_name, display\_name), new MailAddress(To));
                    if (Cc != "")
                    {
                        string\[\] array1 = Cc.Split(',');
                        for (int i = 0; i < array1.Length; i++)
                        {
                            message.CC.Add(array1\[i\].ToString());
                        }
            
                    }
                    if (Bcc != "")
                    {
                        string\[\] array2 = Bcc.Split(',');
                        for (int i = 0; i < array2.Length; i++)
                        {
                            message.Bcc.Add(array2\[i\].ToString());
                        }
            
                    }
            
                    message.Body = body;
                    message.Subject = subject;
                    message.IsBodyHtml = true;
                    client.Send(message);
                    message.Dispose();
                    return "sent successfully";
                    
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString() + "----" + ex.InnerException.ToString();
                }
            }
            

            Thanks Shan

            S 2 Replies Last reply
            0
            • S shankbond

              Hi, here is the code:

              protected void Button4_Click(object sender, EventArgs e)
              {
              testingservice service = new testingservice();
              AsyncCallback callback = new AsyncCallback(hello);
              service.BeginSendMailGmail1(TextBoxto.Text.ToString(), TextBoxcc.Text.ToString(), TextBoxbcc.Text.ToString(), TextBoxbody.Text.ToString(), TextBoxsubject.Text.ToString(), TextBoxusername.Text.ToString(), TextBoxpassword.Text.ToString(), TextBoxserver.Text.ToString(), TextBoxport.Text.ToString(), TextBoxdisplayname.Text.ToString(),callback,sender);
              Response.Redirect("default4.aspx");
              }

              here is the other method but it will always be completed =true

              void hello(IAsyncResult result)
              {
              if (!result.IsCompleted)
              {
              Response.Write("failed");
              }
              }

              the code for webservice:

              [WebMethod]

              public string  SendMailGmail1(string To, string Cc, string Bcc, string body,string subject, string user\_name, string password, string server\_name, string portnumber, string display\_name)
              {
                 
                  SmtpClient client = new SmtpClient();
                  MailMessage message = new MailMessage();
                  client.Port = Convert.ToInt32(portnumber);
                  client.Host = server\_name;
                  client.UseDefaultCredentials = true;
                  client.Credentials = new System.Net.NetworkCredential(user\_name, password);
                  client.EnableSsl = true;
                  try
                  {
                      message = new MailMessage(new MailAddress(user\_name, display\_name), new MailAddress(To));
                      if (Cc != "")
                      {
                          string\[\] array1 = Cc.Split(',');
                          for (int i = 0; i < array1.Length; i++)
                          {
                              message.CC.Add(array1\[i\].ToString());
                          }
              
                      }
                      if (Bcc != "")
                      {
                          string\[\] array2 = Bcc.Split(',');
                          for (int i = 0; i < array2.Length; i++)
                          {
                              message.Bcc.Add(array2\[i\].ToString());
                          }
              
                      }
              
                      message.Body = body;
                      message.Subject = subject;
                      message.IsBodyHtml = true;
                      client.Send(message);
                      message.Dispose();
                      return "sent successfully";
                      
                  }
                  catch (Exception ex)
                  {
                      return ex.Message.ToString() + "----" + ex.InnerException.ToString();
                  }
              }
              

              Thanks Shan

              S Offline
              S Offline
              sashidhar
              wrote on last edited by
              #6

              shankbond wrote:

              AsyncCallback callback = new AsyncCallback(hello);

              I dont Understand why you need asychronous? here is sample code i developed now Modify your code depending on your requirement...!

              //For WebService
              [WebMethod]
              public string SendMail()
              {
              try
              {
              MailMessage msg=new MailMessage ("From@xxx.com","To@xxx.com","Sub","body");
              SmtpClient client = new SmtpClient("YourDomain", 25);
              client .Send (msg);
              return "hai";
              }
              catch (Exception ie)
              {
              return ie.Message ;
              }
              }

              For Calling Webservice..!

              localhost.WebService rss = new localhost.WebService();
              string msg=rss.SendMail();
              Response.Write(msg);

              I am Going Bye May the group will help if you need further assistance..!:thumbsup:

              LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

              1 Reply Last reply
              0
              • S shankbond

                Hi, here is the code:

                protected void Button4_Click(object sender, EventArgs e)
                {
                testingservice service = new testingservice();
                AsyncCallback callback = new AsyncCallback(hello);
                service.BeginSendMailGmail1(TextBoxto.Text.ToString(), TextBoxcc.Text.ToString(), TextBoxbcc.Text.ToString(), TextBoxbody.Text.ToString(), TextBoxsubject.Text.ToString(), TextBoxusername.Text.ToString(), TextBoxpassword.Text.ToString(), TextBoxserver.Text.ToString(), TextBoxport.Text.ToString(), TextBoxdisplayname.Text.ToString(),callback,sender);
                Response.Redirect("default4.aspx");
                }

                here is the other method but it will always be completed =true

                void hello(IAsyncResult result)
                {
                if (!result.IsCompleted)
                {
                Response.Write("failed");
                }
                }

                the code for webservice:

                [WebMethod]

                public string  SendMailGmail1(string To, string Cc, string Bcc, string body,string subject, string user\_name, string password, string server\_name, string portnumber, string display\_name)
                {
                   
                    SmtpClient client = new SmtpClient();
                    MailMessage message = new MailMessage();
                    client.Port = Convert.ToInt32(portnumber);
                    client.Host = server\_name;
                    client.UseDefaultCredentials = true;
                    client.Credentials = new System.Net.NetworkCredential(user\_name, password);
                    client.EnableSsl = true;
                    try
                    {
                        message = new MailMessage(new MailAddress(user\_name, display\_name), new MailAddress(To));
                        if (Cc != "")
                        {
                            string\[\] array1 = Cc.Split(',');
                            for (int i = 0; i < array1.Length; i++)
                            {
                                message.CC.Add(array1\[i\].ToString());
                            }
                
                        }
                        if (Bcc != "")
                        {
                            string\[\] array2 = Bcc.Split(',');
                            for (int i = 0; i < array2.Length; i++)
                            {
                                message.Bcc.Add(array2\[i\].ToString());
                            }
                
                        }
                
                        message.Body = body;
                        message.Subject = subject;
                        message.IsBodyHtml = true;
                        client.Send(message);
                        message.Dispose();
                        return "sent successfully";
                        
                    }
                    catch (Exception ex)
                    {
                        return ex.Message.ToString() + "----" + ex.InnerException.ToString();
                    }
                }
                

                Thanks Shan

                S Offline
                S Offline
                sashidhar
                wrote on last edited by
                #7

                If The Web Service is in you application Keep the break point and check wats happening in webservice..!You can track the problem..!:thumbsup:

                LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                S 2 Replies Last reply
                0
                • S sashidhar

                  If The Web Service is in you application Keep the break point and check wats happening in webservice..!You can track the problem..!:thumbsup:

                  LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                  S Offline
                  S Offline
                  shankbond
                  wrote on last edited by
                  #8

                  Cool it worked thanks. I need the asynchronous mode only one problem left now how to pop up a message box or so, when the user is simply surfing on some other page?

                  Thanks Shankbond

                  1 Reply Last reply
                  0
                  • S sashidhar

                    If The Web Service is in you application Keep the break point and check wats happening in webservice..!You can track the problem..!:thumbsup:

                    LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                    S Offline
                    S Offline
                    shankbond
                    wrote on last edited by
                    #9

                    is there a way that I can add to Your reputation :) :)

                    Thanks Shankbond

                    S 2 Replies Last reply
                    0
                    • S shankbond

                      is there a way that I can add to Your reputation :) :)

                      Thanks Shankbond

                      S Offline
                      S Offline
                      sashidhar
                      wrote on last edited by
                      #10

                      :laugh:

                      LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                      1 Reply Last reply
                      0
                      • S shankbond

                        is there a way that I can add to Your reputation :) :)

                        Thanks Shankbond

                        S Offline
                        S Offline
                        sashidhar
                        wrote on last edited by
                        #11

                        shankbond wrote:

                        is there a way that I can add to Your reputation

                        I dont Know Much Abt reputation may be its new in codeproject I will Let you know After I know abt fully..!:thumbsup:

                        LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups