如何使用whatsapp API发送短信并winform C#?

时间:2015-09-18 02:41:23

标签: c# winforms whatsapp

我编写代码以使用whatsapp API和winform C#发送短信。我不知道它有什么问题。

有错误

  

“登录失败未经授权”

...但是我在“Whatsapp注册”中注册了“https://github.com/mgp25/WART”,并且有像图像这样的密码。

这是我的代码

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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);
 }

使用此代码,您可以毫无问题地发送短信。

祝你好运。