SMTP CONCEPT


DESIGN:




CODINGS:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;

namespace mailotp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            generateotp();
            sendemail();
        }
        string otpm = string.Empty;
        public void generateotp()
        {//to genarate OTP

            {

                string num = "0123456789abcdef";
                int len = num.Length;
                string otp = string.Empty;
                int getindex;
                int otpdigit = 5;
                string finaldigit;


                for (int i = 0; i < otpdigit; i++)
                {
                    do
                    {
                        getindex = new Random().Next(0, len);
                        finaldigit = num.ToCharArray()[getindex].ToString();
                    } while (otp.IndexOf(finaldigit) != -1);
                    {
                        otp += finaldigit;
                    }

                    otpm = otp;
                }

            }

        }

        public void sendemail()
        {
            //argument (from email Address,To email Address)
            MailMessage mail = new MailMessage("archanachezhiyan2004@gmail.com",textBox1.Text);
            mail.Subject = "OTP FORM DAILY ENTRY";
            mail.Body = "code=" + otpm;
            mail.IsBodyHtml = true;
            //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment   
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.Credentials = new NetworkCredential()
            {
                //email ID
                UserName = "archanachezhiyan2004@gmail.com",
                //email ID password
                Password = "smna nlkr uplc uzqc"
            };
            smtp.EnableSsl = true;
            smtp.Send(mail);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (otpm == textBox2.Text)
            {
                MessageBox.Show(" OTP is Valid");
            }
            else
            {
                MessageBox.Show(" Please Enter valid OTP");
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Comments