protected void btnGui_Click(object sender, EventArgs e) { try { String email = "*****@*****.**"; String subject = txtChuDe.Text; String body = "Thư được gửi từ" + txtEmail.Text + " với nội dung: " + txtNoiDung.Text; GoogleMail.Send(email, subject, body); MultiView1.ActiveViewIndex = 1; lblHoTen.Text = txtHoTen.Text; lblEmail.Text = txtEmail.Text; lblChuDe.Text = txtChuDe.Text; lblNoiDung.Text = txtNoiDung.Text; } catch (Exception ex) { lblthongbao.Text = "Xảy ra lỗi! " + ex.Message; } }
public string SendGoogleMail(GoogleMail googleMail) { string result = "FAILED"; try { string credentialsPath = ConfigurationManager.AppSettings["GSuiteCredntial"]; string credentialsPathNew = ConfigurationManager.AppSettings["GSuiteCredentialNew"]; UserCredential credential; //read credentials file using (FileStream stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read)) { string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); credPath = credentialsPathNew; credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; } string plainText = $"To: " + googleMail.ToMail + " \r\n" + $"Subject: " + googleMail.Subject + " \r\n" + "Content-Type: text/html; charset=utf-8\r\n\r\n" + $"<h1>" + googleMail.Body + "</h1>"; //call gmail service var service = new GmailService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); var newMsg = new Google.Apis.Gmail.v1.Data.Message(); newMsg.Raw = Base64UrlEncode(plainText.ToString()); service.Users.Messages.Send(newMsg, "me").Execute(); result = "SUCCESS"; } catch (Exception ex) { throw ex; } return(result); }
static public void Send(String To, String Cc, String Bcc, String Subject, String Body, String[] Attachments) { MailMessage Mail = GoogleMail.CreateMailMessage(To, Subject, Body); if (!String.IsNullOrEmpty(Cc)) { Mail.Cc = Cc.Replace(',', ';').Replace(" ", ""); } if (!String.IsNullOrEmpty(Bcc)) { Mail.Bcc = Bcc.Replace(',', ';').Replace(" ", ""); } if (Attachments != null) { foreach (String Attachment in Attachments) { Mail.Attachments.Add(Attachment); } } GoogleMail.Send(Mail); }
static public void Send(String To, String Subject, String Body) { MailMessage Mail = GoogleMail.CreateMailMessage(To, Subject, Body); GoogleMail.Send(Mail); }