Smart cities (SCs) are being constructed with the huge placement of the Internet of Things (IoT). Real-time enhancements to life quality based on comfort and efficiency. The key concerns in most SCs that immediately impact network performance are security and privacy. Numerous approaches are proposed for secure data transmission, but the current methods do not provide high accuracy and it provide high computational time. To resolve these problems, an Auto-metric Graph Neural Network for Attack Detection and Secure Data Transmission using Optimized Enhanced Identity-Based Encryption in IoT (AGNN-AWHSE-ST-IoT) is proposed. Primarily, the input data is taken from the NSL-KDD dataset. The input data is gathered with the aid of NSL-KDD is pre-processed using three steps, crisp data conversion, splitting, and normalization. Then the Pre-processed input is fed into the Colour Harmony Algorithm (CHA) based feature selection to select the important features. After feature selection, the preferred features are given to the AGNN classifier. After classifying, the data is given to Enhanced Identity-Based Encryption (EIBE), and it is optimized using Wild Horse Optimizer (WHO) for transmitting the data more safely. The outcomes of the normal data are displayed using the LCD monitor. The AGNN-AWHSE-ST-IoT method is implemented in PYTHON. The AGNN-AWHSE-ST-IoT method attains 8.888%, 13.953%, 19.512% higher accuracy, 2.105%, 6.593%, 8.988% higher cumulative accuracy, 54.285%, 54.285%, 52.941% lower encryption time, 8.2%, 3.3%, 6.9% lower decryption time, 11.627%, 10.344%, 6.666% higher security level and 60.869%, 70% and 64% lower computational time than the existing approaches such as SBAS-ST-IoT, BDN-GWMNN-ST-IoT and DNN-LSTM-ST-IoT respectively.