public EmailResult run(Email_raw _Email_raw, EmailConfig _EmailConfig) { Send_Gmail _Send_Gmail = new Send_Gmail(); EmailResult _EmailResult = new EmailResult(); _EmailResult = _Send_Gmail.send(_Email_raw, _EmailConfig); return(_EmailResult); }
public EmailResult send(Email_raw _Email_raw, EmailConfig _EmailConfig) { // EmailConfig _EmailConfig = new EmailConfig(); // _EmailConfig.clientId = "739330450434-e4cq0bonlglucdnmodofbjg09qj26u36.apps.googleusercontent.com"; // var clientSecret = ConfigurationManager.AppSettings["ClientSecret"]; // _EmailConfig.clientSecret = "dfDfdOJeobb1x0VNrTDHsEGO"; //var senderName = ConfigurationManager.AppSettings["EmailSenderName"]; //var senderAddress = ConfigurationManager.AppSettings["EmailSenderAddress"]; //var receiverName = ConfigurationManager.AppSettings["EmailReceiverName"]; //var receiverAddress = ConfigurationManager.AppSettings["EmailReceiverAddress"]; EmailResult _emailResult = new EmailResult(); try { _EmailConfig.credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = _EmailConfig.clientId, ClientSecret = _EmailConfig.clientSecret, }, new[] { GmailService.Scope.GmailCompose }, "user", CancellationToken.None).Result; _EmailConfig.service = new GmailService(new BaseClientService.Initializer { HttpClientInitializer = _EmailConfig.credential, ApplicationName = _EmailConfig.ApplicationName, }); // only the raw parameter of the message resource needs set, see: http://stackoverflow.com/questions/24460422/how-to-send-a-message-successfully-using-the-new-gmail-rest-api // from that there is a working example in the RFC 2822nspecification: "From: John Doe <*****@*****.**>\nTo: Mary Smith <*****@*****.**>\nSubject: Saying Hello\nDate: Fri, 21 Nov 1997 09:55:06 -0600\nMessage-ID: <*****@*****.**>\n\nThis is a message just to say hello.\nSo, \"Hello\"."; // use that as a working base // the values Date and Message-ID have no bearing on the final email (or didn't to me) so have keep them as placeholders and haven't tried to replace them // const string subject = "Email Subject"; // there are some issues around the body encoding/decoding // this message decoded will have a '5' at the end // a full stop at the end will make an invalid raw parameter // but this was good enough for my purposes... //const string body = "Hello this is an email wriien by a very simple console application"; //senderName = "51ShareLuggage"; //senderAddress = "*****@*****.**"; //receiverName = "pangxiong"; //receiverAddress = "*****@*****.**"; // format the message var text = string.Format("From: {0} <{1}>\nTo: {2} <{3}>\nSubject: {4}\nDate: Fri, 21 Nov 1997 09:55:06 -0600\nMessage-ID: <*****@*****.**>\n\n{5}", _Email_raw.senderName, _Email_raw.senderAddress, _Email_raw.receiverName, _Email_raw.receiverAddress, _Email_raw.subject, _Email_raw.body); // must be base64 encoded but also web safe and also initially UTF8 // http://stackoverflow.com/questions/24460422/how-to-send-a-message-successfully-using-the-new-gmail-rest-api // http://stackoverflow.com/questions/13017703/java-and-net-base64-conversion-confusion var encodedText = System.Web.HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(text)); Console.WriteLine("Raw email:\n{0}", encodedText); Message message = new Message { Raw = encodedText }; Message request = _EmailConfig.service.Users.Messages.Send(message, "me").Execute(); _emailResult.ResultCode = "200"; //Console.WriteLine( // string.IsNullOrEmpty(request.Id) ? "Issue sending, returned id: {0}" : "Email looks good, id populated: {0}", // request.Id); //Console.WriteLine( _emailResult.ResultText = string.IsNullOrEmpty(request.Id) ? "Failed" : "Success"; _emailResult.ResultCode = string.Format(string.IsNullOrEmpty(request.Id) ? "Issue sending, returned id: {0}" : "Email looks good, id populated: {0}", request.Id); } catch (Exception Ex) { _emailResult.ResultCode = "437"; _emailResult.ResultText = "Failed"; _emailResult.ResultCode = "SystemError"; } return(_emailResult); }