How to connect to yahoo mail using smtp in asp.net

To connect to Yahoo Mail using SMTP in ASP.NET, you'll need to follow these steps:

Step 1: Enable Less Secure Apps

To allow your ASP.NET application to connect to Yahoo Mail using SMTP, you need to enable "Less Secure Apps" in your Yahoo Mail account settings. Here's how:

  1. Sign in to your Yahoo Mail account.
  2. Click on your profile picture or avatar in the top right corner, then select "Account Info" from the dropdown menu.
  3. Scroll down to the "Account security" section.
  4. Click on "Less Secure Apps" and toggle the switch to "On".

Step 2: Configure SMTP Settings

In your ASP.NET application, you'll need to configure the SMTP settings to connect to Yahoo Mail. You can do this in the Web.config file or in your code-behind file.

Here's an example of how to configure the SMTP settings in the Web.config file:

<configuration>
    <appSettings>
        <add key="smtpServer" value="smtp.mail.yahoo.com" />
        <add key="smtpPort" value="587" />
        <add key="smtpUsername" value="your_yahoo_email_address" />
        <add key="smtpPassword" value="your_yahoo_email_password" />
    </appSettings>
</configuration>

Replace your_yahoo_email_address and your_yahoo_email_password with your actual Yahoo Mail email address and password.

Step 3: Use the SmtpClient Class

In your ASP.NET code-behind file, you can use the SmtpClient class to send emails using the configured SMTP settings. Here's an example:

using System.Net.Mail;

// Get the SMTP settings from the Web.config file
string smtpServer = ConfigurationManager.AppSettings["smtpServer"];
int smtpPort = int.Parse(ConfigurationManager.AppSettings["smtpPort"]);
string smtpUsername = ConfigurationManager.AppSettings["smtpUsername"];
string smtpPassword = ConfigurationManager.AppSettings["smtpPassword"];

// Create a new SmtpClient instance
SmtpClient client = new SmtpClient();

// Set the SMTP settings
client.Host = smtpServer;
client.Port = smtpPort;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(smtpUsername, smtpPassword);

// Create a new MailMessage instance
MailMessage message = new MailMessage();
message.From = new MailAddress(smtpUsername);
message.To.Add(new MailAddress("recipient_email_address"));
message.Subject = "Test Email";
message.Body = "This is a test email sent using Yahoo Mail SMTP";

// Send the email
client.Send(message);

Replace recipient_email_address with the email address of the recipient.

Note: Make sure to handle any exceptions that may occur when sending the email, such as network errors or authentication failures.

That's it! With these steps, you should be able to connect to Yahoo Mail using SMTP in your ASP.NET application.