7

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("xxx@gmail.com");
mail.To.Add("kalaylevent@gmail.com");
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("kalaylevent@gmail.com", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
leventkalayz
  • 212
  • 2
  • 5
  • 16
  • you just place the email to: To.Add(themail@hotmail) – Aristos Mar 24 '12 at 11:29
  • I cannot send mail to hotmail account by using this method I guess that I have to add general smpt server bla.... – leventkalayz Mar 24 '12 at 11:44
  • You have an hotmail account and do you want to send mail to someone using their Smtp Service or do you want send mail to an hotmail account using gmail Smtp Service? – Steve Mar 24 '12 at 11:44
  • Yes, I want to send mail to hotmail account or from hotmail account so, I want to use hotmail and gmail together – leventkalayz Mar 24 '12 at 11:46

2 Answers2

31

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("youremail@hotmail.com");
mail.To.Add("to@gmail.com");
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("youremail@hotmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
zhengchun
  • 1,261
  • 13
  • 19
  • Thanks, it is so helpful for me – leventkalayz Mar 24 '12 at 12:20
  • Also, how can i check the smtp authentication so, Firstly it check the authetication if it is true , the user can send mail after authentication response(it is mail password or username true or not) – leventkalayz Mar 24 '12 at 12:22
  • sorry,this question i can't answer you because i also don't know.but i can give you some suggest,you can use tcp/ip with smtp protocol and connect the smtp server.this open mail server may be can help you .[LumiSoft Mail Server](http://www.lumisoft.ee/lsWWW/ENG/Products/Mail_Server/mail_index_eng.aspx?type=info),the simple method is use try..exception.if get authentication failed then you can use other account. – zhengchun Mar 24 '12 at 13:59
  • thanks +1, it takes some time for the mail to send, can i make it instant? – Mister Verleg Mar 17 '16 at 19:30
  • Why did you call smtpSERVER a variable of type SmtpCLIENT? – Massimiliano Kraus Jun 04 '20 at 18:10
  • PLEASE NOTE Hotmail, Outlook.com SMTP Server Hotmail/Live/Outlook.com SMTP server address is smtp.office365.com (smtp.live.com does not work anymore) – Momodu Deen Swarray Jun 14 '22 at 10:27
3

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();
       }
 }
Kaveh Naseri
  • 1,102
  • 2
  • 15
  • 24