public async Task Send() { var csvMedia = new MediaTransformCSV(); csvMedia.valuesToBeSent = this.valuesToBeSent; await csvMedia.Run(); var buffer = Encoding.UTF8.GetBytes(csvMedia.Result); using (var fs = new FileStream(CSVFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, buffer.Length, true)) { await fs.WriteAsync(buffer, 0, buffer.Length); } }
public async Task Send() { //Initialization Validation(); //Generate email body var mediaCSV = new MediaTransformCSV(); mediaCSV.valuesToBeSent = valuesToBeSent; await mediaCSV.Run(); //TextWriter writer = new StringWriter(); //using (var csv = new CsvWriter(writer)) //{ // foreach (var row in valuesToBeSent) // { // if (!hasHeader) // { // foreach(var item in row.Values.Keys) // { // csv.WriteField<string>(item); // } // csv.NextRecord(); // hasHeader = true; // } // foreach (var item in row.Values.Values) // { // csv.WriteField<object>(item); // } // csv.NextRecord(); // } //} this.Body = this.Body + mediaCSV.Result; //Send email //Using MailKit //https://www.stevejgordon.co.uk/how-to-send-emails-in-asp-net-core-1-0 //https://www.joeaudette.com/blog/2016/05/08/sending-smtp-email-on-aspnet-core-with-mailkit //TODO: In .Net Core 2.0 replace MailKit with MailMessage var message = new MimeMessage(); foreach (var emailAddress in this.From.Split(';')) { if (string.IsNullOrEmpty(emailAddress)) { break; } message.From.Add(new MailboxAddress(emailAddress)); } foreach (var emailAddress in this.To.Split(';')) { if (string.IsNullOrEmpty(emailAddress)) { break; } message.To.Add(new MailboxAddress(emailAddress)); } foreach (var emailAddress in this.Cc.Split(';')) { if (string.IsNullOrEmpty(emailAddress)) { break; } message.Cc.Add(new MailboxAddress(emailAddress)); } foreach (var emailAddress in this.Bcc.Split(';')) { if (string.IsNullOrEmpty(emailAddress)) { break; } message.Bcc.Add(new MailboxAddress(emailAddress)); } message.Subject = this.Subject; message.Body = new TextPart((this.IsBodyHtml ? "html" : "plain")) { Text = this.Body }; using (var client = new SmtpClient()) { await client.ConnectAsync(this.SmtpServer, this.SmtpPort, (this.EnableSsl ? SecureSocketOptions.Auto : SecureSocketOptions.None)).ConfigureAwait(false); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication if (this.RequiresAuthentication) { await client.AuthenticateAsync(this.User, this.Password).ConfigureAwait(false); } await client.SendAsync(message).ConfigureAwait(false); await client.DisconnectAsync(true).ConfigureAwait(false); } }