Office 365 kullanan bir hesap üzerinden c# üzerinden mail gönderimi yaparken 535 5.7.139 Authentication unsuccessful hatası hakkında

Office 365 kullanan bir hesap üzerinden c# üzerinden mail gönderimi yaparken 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant hatası alırsanız Office 365 üzerinde bazı işlemler yapmanız gerekmektedir.

Öncelikle C# kodu: 

 class Program

    {

        static void Main(string[] args)

        {

            MailMessage msg = new MailMessage();

            msg.To.Add(new MailAddress("serkansonmez16@gmail.com", "Serkan"));

            msg.From = new MailAddress("mailuser@contoso.com", "support");

            msg.Subject = "This is a Test Mail";

            msg.Body = "This is a test message using Exchange OnLine";

            msg.IsBodyHtml = true;


            SmtpClient client = new SmtpClient();

            client.UseDefaultCredentials = false ;

            client.Credentials = new System.Net.NetworkCredential("mailuser@contoso.com", "MailPassword");

            client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)

            client.Host = "smtp.office365.com";

            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            client.EnableSsl = true;

            try

            {

                client.Send(msg);

                //lblText.Text = "Message Sent Succesfully";

            }

            catch (Exception ex)

            {

                //lblText.Text = ex.ToString();

                Console.WriteLine(ex.Message);

                Console.Read();

            }

        }

    }



Power Shell üzerinde yapılacaklar:



Önce Power Shell üzerinde exchange 365'e erişim için link üzerinden aşağıdaki kod çalıştırılır.

1-) https://www.powershellgallery.com/packages/ExchangeOnlineManagement/2.0.5

   PS> Install-Module -Name ExchangeOnlineManagement -RequiredVersion 2.0.5


2- Admin kullanıcısına ait mail adresi girilecek, komutu çalıştırınca kullanıcı şifresini girmek gerekiyor.

Doğru şifreyi girdikten sonra Exchange'e ait tüm komutlar aktif hale geliyor.

    PS>Connect-ExchangeOnline -UserPrincipalName adminmailaddress@contoso.com

3- Domain üzerinde SMTP aktif hale getirilmeli, aşağıdaki komut çalıştırılacak.

    PS> Set-TransportConfig -SmtpClientAuthenticationDisabled $true


4- Gönderim yapılacak mail adresi için SMTP aktif hale getiriliyor.

    PS> Set-CASMailbox -Identity sendingMail@contoso.com -SmtpClientAuthenticationDisabled $false


5- Yukarıdaki işlemleri yapsanız da hata almaya devam edebilirsiniz. Son hatanın düzeltilmesi için 

TLS client gönderim için True yapılıyor.


    PS> Set-TransportConfig -AllowLegacyTLSClients $true

Hiç yorum yok:

BlackListIP control on Serenity platform (.NET Core)

 In the Serenity platform, if you want to block IPs that belong to people you do not want to come from outside in the .net core web project,...