Asp.Net ilə mail göndərmək

Asp.Net ilə mail göndərmək

Publish date : 2024/04/03
218 views

Visual Studio-da Console Application açıb, içərisinə bu kodu yerləşdirin 

Kodu görmək üçün tıqlayın...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MailSenderConsole
{

class Program
{
    static void Main(string[] args)
    {
        // Email credentials
        string fromMail = "thedotnetchannelsender22@gmail.com";
        string fromPassword = "lgioehkvchemfkrw";

        // Create a new email message
        MailMessage message = new MailMessage();
        message.From = new MailAddress(fromMail); // Set sender's email address
        message.Subject = "Test Subject"; // Set email subject
        message.To.Add(new MailAddress("turalnagi@gmail.com")); // Add recipient's email address
        message.Body = "<html><body> Test mail from ASOIU :) </body></html>"; // Set email body as HTML
        message.IsBodyHtml = true; // Specify that the body is HTML formatted

        // SMTP client configuration
        var smtpClient = new SmtpClient("smtp.gmail.com"); // SMTP server address
        smtpClient.Port = 587; // SMTP port number
        smtpClient.Credentials = new NetworkCredential(fromMail, fromPassword); // Set SMTP credentials
        smtpClient.EnableSsl = true; // Enable SSL for secure communication with the SMTP server

        try
        {
            // Attempt to send the email
            smtpClient.Send(message);
            Console.WriteLine("Email sent successfully!"); // Display success message
        }
        catch (SmtpException ex)
        {
            // If sending fails, catch the exception and display error message
            Console.WriteLine("Error sending email: " + ex.Message);
        }

        Console.ReadLine(); // Wait for user input before closing the console window
    }
}


   }

Sonra bu kodu xaker alətə çevirmək olar.

 

Qaynaq

 

 

 

 

 

Comments

Add comment