Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add smtp hotmail account to send mail

Tags:

c#

.net

asp.net

I wrote some codes so as to send e mail but I can only send mail from gmail account to gmail account also, I want to use hotmail accounts how can i do it? thanks It is

SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
like image 744
leventkalayz Avatar asked Nov 27 '22 11:11

leventkalayz


2 Answers

I changed a little of code and it tested successfully (from Hotmail to Gmail)

SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
like image 142
zhengchun Avatar answered Dec 05 '22 01:12

zhengchun


I use different smtp client and make sure to set the socket options to StartTls :

 using (SmtpClient client = new())
  {
     try 
     {
           await client.ConnectAsync("smtp.office365.com", 587, SecureSocketOptions.StartTls);
                        client.AuthenticationMechanisms.Remove("XOAUTH2");
                        
           await client.AuthenticateAsync("Your_User_Name", "Your_Password");

           await client.SendAsync(mailMessage);
        }
        catch
        {
           //log an error message or throw an exception, or both.
           throw;
       }
       finally
       {
            await client.DisconnectAsync(true);
            client.Dispose();
       }
 }
like image 22
Kaveh Naseri Avatar answered Dec 05 '22 01:12

Kaveh Naseri