簡體   English   中英

如何添加smtp hotmail賬號發送郵件

[英]How to add smtp hotmail account to send mail

我寫了一些代碼來發送電子郵件,但我也只能從gmail帳戶向gmail帳戶發送郵件,我想使用hotmail帳戶我該怎么做? 謝謝 它是

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);

我更改了一些代碼並成功測試(從 Hotmail 到 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);

我使用不同的 smtp 客戶端,並確保將套接字選項設置為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();
       }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM