Send email hotmail c#
Here is an example of how to send an email using Hotmail (Outlook.com) in C#:
using System;
using System.Net.Mail;
using System.Net;
// Replace with your Hotmail/Outlook.com email address
string fromEmail = "[email protected]";
string fromPassword = "your_password";
string toEmail = "[email protected]";
string subject = "Test Email";
string body = "This is a test email sent from C#";
// Create a new SmtpClient object
SmtpClient client = new SmtpClient();
// Set the SMTP server and port
client.Host = "smtp-mail.outlook.com";
client.Port = 587;
// Set the email address and password
client.Credentials = new NetworkCredential(fromEmail, fromPassword);
// Set the email message
MailMessage message = new MailMessage(fromEmail, toEmail, subject, body);
// Send the email
client.Send(message);
Note:
- You need to replace
[email protected]
andyour_password
with your actual Hotmail/Outlook.com email address and password. - You also need to replace
[email protected]
with the email address of the recipient. - Make sure to enable less secure apps in your Hotmail/Outlook.com account settings to allow C# to send emails on your behalf.
- Also, you can use
System.Net.Mail.SmtpClient
class to send email, but it's deprecated, you can useSystem.Net.Mail.SmtpClient
class instead.
You can also use Microsoft.Exchange.WebServices
NuGet package to send email using Hotmail/Outlook.com, here is an example:
using Microsoft.Exchange.WebServices.Data;
// Replace with your Hotmail/Outlook.com email address
string email = "[email protected]";
string password = "your_password";
string toEmail = "[email protected]";
string subject = "Test Email";
string body = "This is a test email sent from C#";
// Create a new ExchangeService object
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
// Set the email address and password
service.Credentials = new NetworkCredential(email, password);
// Set the email message
EmailMessage message = new EmailMessage(service);
message.Subject = subject;
message.Body = new TextBody(body);
message.ToRecipients.Add(new EmailAddress(toEmail));
// Send the email
service.Send(message);
Make sure to install Microsoft.Exchange.WebServices
NuGet package and also you need to enable less secure apps in your Hotmail/Outlook.com account settings to allow C# to send emails on your behalf.