winforms 如何使用WhatsApp API和Winform C#发送短信?

cczfrluj  于 2023-04-21  发布在  C#
关注(0)|答案(1)|浏览(167)

我写的代码发送短信使用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();
    }
}
}

qhhrdooz

qhhrdooz1#

试试这个:

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

有了这个代码,你可以发送短信没有问题。
祝你好运

相关问题