V4.5
This commit is contained in:
132
GoogleAPI.cs
132
GoogleAPI.cs
@@ -1,12 +1,12 @@
|
||||
using Google.Apis.Auth.OAuth2;
|
||||
using Google.Apis.Gmail.v1;
|
||||
//using Google.Apis.Gmail.v1;
|
||||
using Google.Apis.Services;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Google.Apis.Sheets.v4.Data;
|
||||
using Google.Apis.Util.Store;
|
||||
using System.Net.Mail;
|
||||
using System.Net.Mime;
|
||||
using System.Reflection;
|
||||
//using System.Net.Mail;
|
||||
//using System.Net.Mime;
|
||||
//using System.Reflection;
|
||||
|
||||
namespace AiQ_GUI
|
||||
{
|
||||
@@ -18,6 +18,7 @@ namespace AiQ_GUI
|
||||
static readonly string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
|
||||
public const string spreadsheetId_ModelInfo = "1bCcCr4OYqfjmydt6UqtmN4FQETezXmZRSStJdCCcqZM";
|
||||
public const string DrivePath = @"G:\Shared drives\MAV Production GUI's\"; // Path to google shared drive
|
||||
public const string GoogleDrivePath = @"G:\Shared drives\MAV Production GUI's\AiQ\GUI's\";
|
||||
|
||||
// Startup and make necessary connections to Google servers and make sure user is logged in
|
||||
public static bool Setup()
|
||||
@@ -284,75 +285,76 @@ namespace AiQ_GUI
|
||||
return valuesRMA.Count + 2; // Gets the amount of rows, offsets for start from 1 error and adds one to be next row
|
||||
}
|
||||
|
||||
public static void EmailApproval(string ApprovalRow, string User)
|
||||
{
|
||||
FileStream GmailStream = new($"{DrivePath}R50IQ\\creds.json", FileMode.Open, FileAccess.Read);
|
||||
string[] ScopesGmail = [GmailService.Scope.GmailSend];
|
||||
// DEPRECATED
|
||||
//public static void EmailApproval(string ApprovalRow, string User)
|
||||
//{
|
||||
// FileStream GmailStream = new($"{DrivePath}R50IQ\\creds.json", FileMode.Open, FileAccess.Read);
|
||||
// string[] ScopesGmail = [GmailService.Scope.GmailSend];
|
||||
|
||||
using (GmailStream)
|
||||
{
|
||||
string credPathGmail = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart.json");
|
||||
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(GmailStream).Secrets, ScopesGmail, "user", CancellationToken.None, new FileDataStore(credPathGmail, true)).Result;
|
||||
GmailStream.Close();
|
||||
}
|
||||
// using (GmailStream)
|
||||
// {
|
||||
// string credPathGmail = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart.json");
|
||||
// credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(GmailStream).Secrets, ScopesGmail, "user", CancellationToken.None, new FileDataStore(credPathGmail, true)).Result;
|
||||
// GmailStream.Close();
|
||||
// }
|
||||
|
||||
// Build the MIME message with attachment
|
||||
using MailMessage mail = new();
|
||||
mail.From = new MailAddress("me");
|
||||
mail.To.Add("richard.porter@mav-systems.com");
|
||||
mail.To.Add("bradley.relyea@mav-systems.com");
|
||||
mail.To.Add("bradley.born@mav-systems.com");
|
||||
mail.Subject = "Approval required";
|
||||
mail.Body = $"Dear Rich,<br><br>Camera needs approval<br>" +
|
||||
$"https://docs.google.com/spreadsheets/d/1bCcCr4OYqfjmydt6UqtmN4FQETezXmZRSStJdCCcqZM/edit#gid=1931079354&range=A{ApprovalRow}" +
|
||||
$"<br><br><br>Thanks,<br><br>{User}";
|
||||
mail.IsBodyHtml = true;
|
||||
// // Build the MIME message with attachment
|
||||
// using MailMessage mail = new();
|
||||
// mail.From = new MailAddress("me");
|
||||
// mail.To.Add("richard.porter@mav-systems.com");
|
||||
// mail.To.Add("bradley.relyea@mav-systems.com");
|
||||
// mail.To.Add("bradley.born@mav-systems.com");
|
||||
// mail.Subject = "Approval required";
|
||||
// mail.Body = $"Dear Rich,<br><br>Camera needs approval<br>" +
|
||||
// $"https://docs.google.com/spreadsheets/d/1bCcCr4OYqfjmydt6UqtmN4FQETezXmZRSStJdCCcqZM/edit#gid=1931079354&range=A{ApprovalRow}" +
|
||||
// $"<br><br><br>Thanks,<br><br>{User}";
|
||||
// mail.IsBodyHtml = true;
|
||||
|
||||
// Attach the log file if it exists
|
||||
string logFilePath = LDS.MAVPath + Logging.LogFileName;
|
||||
if (File.Exists(logFilePath))
|
||||
{
|
||||
Attachment logAttachment = new(logFilePath, MediaTypeNames.Text.Plain);
|
||||
logAttachment.Name = Logging.LogFileName;
|
||||
mail.Attachments.Add(logAttachment);
|
||||
}
|
||||
// // Attach the log file if it exists
|
||||
// string logFilePath = LDS.MAVPath + Logging.LogFileName;
|
||||
// if (File.Exists(logFilePath))
|
||||
// {
|
||||
// Attachment logAttachment = new(logFilePath, MediaTypeNames.Text.Plain);
|
||||
// logAttachment.Name = Logging.LogFileName;
|
||||
// mail.Attachments.Add(logAttachment);
|
||||
// }
|
||||
|
||||
// Save the MIME message to a stream
|
||||
using MemoryStream ms = new();
|
||||
SmtpClient smtpClient = new(); // Only used to access the internal Write method
|
||||
Type mailWriterType = typeof(SmtpClient).Assembly.GetType("System.Net.Mail.MailWriter");
|
||||
object? mailWriter = Activator.CreateInstance(
|
||||
mailWriterType,
|
||||
BindingFlags.Instance | BindingFlags.NonPublic,
|
||||
null,
|
||||
[ms, true],
|
||||
null);
|
||||
// // Save the MIME message to a stream
|
||||
// using MemoryStream ms = new();
|
||||
// SmtpClient smtpClient = new(); // Only used to access the internal Write method
|
||||
// Type mailWriterType = typeof(SmtpClient).Assembly.GetType("System.Net.Mail.MailWriter");
|
||||
// object? mailWriter = Activator.CreateInstance(
|
||||
// mailWriterType,
|
||||
// BindingFlags.Instance | BindingFlags.NonPublic,
|
||||
// null,
|
||||
// [ms, true],
|
||||
// null);
|
||||
|
||||
typeof(MailMessage).InvokeMember(
|
||||
"Send",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
|
||||
null,
|
||||
mail,
|
||||
[mailWriter, true, true]);
|
||||
// typeof(MailMessage).InvokeMember(
|
||||
// "Send",
|
||||
// BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
|
||||
// null,
|
||||
// mail,
|
||||
// [mailWriter, true, true]);
|
||||
|
||||
ms.Position = 0;
|
||||
byte[] rawBytes = ms.ToArray();
|
||||
// ms.Position = 0;
|
||||
// byte[] rawBytes = ms.ToArray();
|
||||
|
||||
GmailService service = new(new BaseClientService.Initializer()
|
||||
{
|
||||
HttpClientInitializer = credential,
|
||||
ApplicationName = ApplicationName,
|
||||
});
|
||||
// GmailService service = new(new BaseClientService.Initializer()
|
||||
// {
|
||||
// HttpClientInitializer = credential,
|
||||
// ApplicationName = ApplicationName,
|
||||
// });
|
||||
|
||||
Google.Apis.Gmail.v1.Data.Message newMsg = new()
|
||||
{
|
||||
Raw = Convert.ToBase64String(rawBytes)
|
||||
.Replace("+", "-")
|
||||
.Replace("/", "_")
|
||||
.Replace("=", "")
|
||||
};
|
||||
// Google.Apis.Gmail.v1.Data.Message newMsg = new()
|
||||
// {
|
||||
// Raw = Convert.ToBase64String(rawBytes)
|
||||
// .Replace("+", "-")
|
||||
// .Replace("/", "_")
|
||||
// .Replace("=", "")
|
||||
// };
|
||||
|
||||
service.Users.Messages.Send(newMsg, "me").Execute();
|
||||
}
|
||||
// service.Users.Messages.Send(newMsg, "me").Execute();
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user