private static void SendEmailData(List <File> files, string emailJwtToken, string emailServiceUrl, string unhandledEmailTemplateId) { try { var emailFiles = new List <EmailFile>(); foreach (File file in files) { EmailFile emailFile = new EmailFile() { Date = file.Timestamp.ToString(), UploadedBy = file.UploadedBy, Timestamp = file.Timestamp.ToString("yyyy-mm-dd HH:mm:ss"), ErrorMessage = file.ErrorMessage }; emailFiles.Add(emailFile); } using var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", emailJwtToken); var content = new StringContent(JsonConvert.SerializeObject(emailFiles), Encoding.UTF8, "application/json"); var result = httpClient.PostAsync($"{emailServiceUrl}/{unhandledEmailTemplateId}", content).Result; if (!result.IsSuccessStatusCode) { throw new Exception($"Http request to {emailServiceUrl} result: {(int)result.StatusCode} {result.StatusCode}"); } } catch (Exception e) { throw new Exception($"SendEmailData method: {e.Message}"); } }
/// <summary> /// 發送未寄郵件 /// </summary> public void Send() { _entEmail = new Email(_conn); _entReceiver = new EmailReceiver(_conn); _entFile = new EmailFile(_conn); _entEmail.query(null, _entEmail.EML_STATUS.cds_Equal(""), "", ""); SmtpClient client = new SmtpClient(Host); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = new NetworkCredential(Account, Passwd); while (_entEmail.read()) { _entReceiver.query(null, _entReceiver.EML_SERNO.cds_Equal(_entEmail.EML_SERNO), "", ""); _entFile.query(null, _entFile.EML_SERNO.cds_Equal(_entEmail.EML_SERNO), "", ""); SendMail(client); } client.ServicePoint.CloseConnectionGroup(client.ServicePoint.ConnectionName); }