read code mailbox with oauth2 in csharp
public static string Get_AccessToken_Hotmail(string data_hotmail, string _Proxy_ = "")
{
string accessToken = null;
try
{
using (Leaf.xNet.HttpRequest request = new Leaf.xNet.HttpRequest())
{
if (!string.IsNullOrEmpty(_Proxy_))
{
Fake_proxy(request, _Proxy_);
}
string postData = $"client_id={data_hotmail.Split('|')[3]}" +
$"&refresh_token={data_hotmail.Split('|')[2]}" +
$"&grant_type=refresh_token";
string response = request.Post("https://login.microsoftonline.com/common/oauth2/v2.0/token", postData, "application/x-www-form-urlencoded").ToString();
dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
accessToken = json.access_token;
}
}
catch
{
try
{
using (Leaf.xNet.HttpRequest request = new Leaf.xNet.HttpRequest())
{
string postData = $"client_id={data_hotmail.Split('|')[3]}" +
$"&refresh_token={data_hotmail.Split('|')[2]}" +
$"&grant_type=refresh_token";
string response = request.Post("https://login.microsoftonline.com/common/oauth2/v2.0/token", postData, "application/x-www-form-urlencoded").ToString();
dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
accessToken = json.access_token;
}
}
catch { }
}
return accessToken;
}
Get Inbox :
public static string Get_Code_Hotmail_OAuth2(string data_hotmail, string accessToken)
{
string code = null;
try
{
var server = new MailServer("outlook.office365.com", data_hotmail.Split('|')[0], accessToken, ServerProtocol.Imap4);
server.AuthType = ServerAuthType.AuthXOAUTH2;
server.SSLConnection = true;
server.Port = 993;
var mailClient = new MailClient("TryIt");
mailClient.Connect(server);
var mailInfos = mailClient.GetMailInfos();
if ((uint)mailInfos.Length > 0U)
{
for (int i = 0; i < (mailInfos.Length > 20 ? 20 : mailInfos.Length); i++)
{
var info = mailInfos[mailInfos.Length - i - 1];
var mail = mailClient.GetMail(info);
// process handle mail in here
}
}
mailClient.Quit();
}
catch
{
}
return null;
}
Public Last updated: 2024-10-16 02:03:53 PM