Windows Forms: Sending WhatsApp Message using C#

By FoxLearn 7/12/2017 9:04:04 PM   22.92K
How to send WhatsApp Message using C# Code

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SmsWhatsApp" and then click OK

sending whatsapp message using c# code

Step 2: Download WhatsApp APIThis is an API written in C# but it can be used in any .NET language. It's a fork from WhatsAPINet, which is based on Chat API

Step 3Design your form as below

sending whatsapp message in c#

Step 4: Add code to handle your form

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

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

        private void btnSend_Click(object sender, EventArgs e)
        {
            WhatsApp wa = new WhatsApp(txtPhone.Text, txtPassword.Text, txtName.Text, true);
            //Open connect
            wa.OnConnectSuccess += () =>
            {
                txtStatus.Text = "Connected...";
                byte[] img = File.ReadAllBytes("e:\\250.gif");
                //Login
                wa.OnLoginSuccess += (phone, data) =>
                {
                    txtStatus.Text += "\r\nLogin success !";
                    //wa.SendMessage(txtTo.Text, txtMessage.Text);
                    //Send whatsapp message with image file
                    wa.SendMessageImage(txtTo.Text, img, ApiBase.ImageType.GIF);
                    txtStatus.Text += "\r\nMessage sent !";
                };
                wa.OnLoginFailed += (data) =>
                {
                    txtStatus.Text += string.Format("\r\nLogin failed {0}", data);
                };
                wa.Login();
            };
            wa.OnConnectFailed += (ex) =>
            {
                txtStatus.Text += string.Format("\r\nConnect failed {0}", ex.StackTrace);
            };
            wa.Connect();
        }
    }
}

VIDEO TUTORIALS