ThanhLam112358
ThanhLam112358

Reputation: 916

How to send sms using whatsapp API and winform C#?

I write the code to send sms using whatsapp API and winform C#. I don't know what problem with it.

It has error

"Login failed not-authorized"

...but I registered in "Whatsapp registration" download from "https://github.com/mgp25/WART" and have password like image.

Here is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WhatsAppApi;

namespace sms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void btn_send_Click(object sender, EventArgs e)
    {
        WhatsApp wa = new WhatsApp(txt_phone.Text, txt_pass.Text, txt_name.Text, true);
        wa.OnConnectSuccess+= () =>
            {
                txt_status.Text = "Connect...";
                wa.OnLoginSuccess+= (phone, data) =>
                    {
                        txt_status.Text += "\r\nConnection success!";
                        wa.SendMessage(txt_to.Text, txt_message.Text);
                        txt_status.Text += "\r\nMessage Sent!";
                    };
                wa.OnLoginFailed+= (data) =>
                    {
                        txt_status.Text += string.Format("\r\bLogin failed {0}", data);
                    };
                wa.Login();
            };
        wa.OnConnectFailed+= (ex) =>
            {
                txt_status.Text += string.Format("\r\bConnect failed {0}", ex.StackTrace);
            };
        wa.Connect();
    }
}
}

App

Upvotes: 1

Views: 8616

Answers (1)

raBinn
raBinn

Reputation: 182

Try this:

private void Form1_Load(object sender, EventArgs e)  {
   _instance = new WhatsAppDLL.WhatsAppDLL(phonenumber, pass_, name);
   _instance.OnLoginSuccess += _instance_OnLoginSuccess;
   _instance.OnLoginFailed += _instance_OnLoginFailed;
}

 void _instance_OnLoginSuccess(string phoneNumber, byte[] data)  {
            this.txtLog.Text = this.txtLog.Text + Environment.NewLine + "LOGIN realizado correctamente " + phoneNumber.ToString() + Environment.NewLine;
  }

 void _instance_OnLoginFailed(string data)  {
            this.txtLog.Text = this.txtLog.Text + Environment.NewLine + "LOGIN ERROR : " + Environment.NewLine +
                               "Mensaje error: " + data.ToString() + Environment.NewLine;
 }

 private void btnConnect_Click(object sender, EventArgs e)  {
     _instance.Connect();
     _instance.Login();
     this.lblResultadoLogin.Text = _instance.ConnectionStatus.ToString();
 }

 private void btnSendSms_Click(object sender, EventArgs e)  {                 
     _instance.SendMessage(sendTo, message);
 }

With this code you can send sms without problems.

Good luck.

Upvotes: 1

Related Questions