public async Task SendEmailAsync() { EmailClient client = CreateEmailClient(); #region Snippet:Azure_Communication_Email_SendAsync // Create the email content var emailContent = new EmailContent("This is the subject"); emailContent.PlainText = "This is the body"; // Create the recipient list var emailRecipients = new EmailRecipients( new List <EmailAddress> { new EmailAddress( //@@ email: "<recipient email address>" //@@ displayName: "<recipient displayname>" /*@@*/ email: TestEnvironment.ToEmailAddress, /*@@*/ displayName: "Customer Name") }); // Create the EmailMessage var emailMessage = new EmailMessage( //@@ sender: "<Send email address>" // The email address of the domain registered with the Communication Services resource /*@@*/ sender: TestEnvironment.AzureManagedFromEmailAddress, emailContent, emailRecipients); SendEmailResult sendResult = await client.SendAsync(emailMessage); Console.WriteLine($"Email id: {sendResult.MessageId}"); #endregion Snippet:Azure_Communication_Email_SendAsync Assert.False(string.IsNullOrEmpty(sendResult.MessageId)); }
public void SendEmail_InvalidParams_Throws() { EmailClient emailClient = CreateEmailClient(); if (IsAsync) { Assert.ThrowsAsync <ArgumentNullException>(async() => await emailClient.SendAsync(null)); } else { Assert.Throws <ArgumentNullException>(() => emailClient.Send(null)); } }
public async Task SendEmailWithAttachmentAsync() { EmailClient client = CreateEmailClient(); var emailContent = new EmailContent("This is the subject"); emailContent.PlainText = "This is the body"; var emailRecipients = new EmailRecipients( new List <EmailAddress> { new EmailAddress( //@@ email: "<recipient email address>" //@@ displayName: "<recipient displayname>" /*@@*/ email: TestEnvironment.ToEmailAddress, /*@@*/ displayName: "Customer Name") }); #region Snippet:Azure_Communication_Email_Send_With_AttachmentsAsync // Create the EmailMessage var emailMessage = new EmailMessage( //@@ sender: "<Send email address>" // The email address of the domain registered with the Communication Services resource /*@@*/ sender: TestEnvironment.AzureManagedFromEmailAddress, emailContent, emailRecipients); #if SNIPPET var filePath = "<path to your file>"; var attachmentName = "<name of your attachment>" EmailAttachmentType attachmentType = EmailAttachmentType.Txt; #endif // Convert the file content into a Base64 string #if SNIPPET byte[] bytes = File.ReadAllBytes(filePath); string attachmentFileInBytes = Convert.ToBase64String(bytes); #else string attachmentName = "Attachment.txt"; EmailAttachmentType attachmentType = EmailAttachmentType.Txt; var attachmentFileInBytes = "VGhpcyBpcyBhIHRlc3Q="; #endif var emailAttachment = new EmailAttachment(attachmentName, attachmentType, attachmentFileInBytes); emailMessage.Attachments.Add(emailAttachment); SendEmailResult sendResult = await client.SendAsync(emailMessage); #endregion Snippet:Azure_Communication_Email_Send_With_AttachmentsAsync }
public void InvalidEmailMessage_Throws_ArgumentException(EmailMessage emailMessage, string errorMessage) { EmailClient emailClient = CreateEmailClient(HttpStatusCode.BadRequest); ArgumentException?exception = null; if (IsAsync) { exception = Assert.ThrowsAsync <ArgumentException>(async() => await emailClient.SendAsync(emailMessage)); } else { exception = Assert.Throws <ArgumentException>(() => emailClient.Send(emailMessage)); } Assert.IsTrue(exception?.Message.Contains(errorMessage)); }
private async Task <Response <SendEmailResult> > SendEmailAsync(EmailClient emailClient) { var emailContent = new EmailContent("subject"); emailContent.PlainText = "Test"; var emailMessage = new EmailMessage( TestEnvironment.AzureManagedFromEmailAddress, emailContent, new EmailRecipients(new List <EmailAddress> { new EmailAddress(TestEnvironment.ToEmailAddress) { DisplayName = "ToAddress" } })); Response <SendEmailResult>?response = await emailClient.SendAsync(emailMessage); return(response); }
private async Task GenerateNewsletterAsync() { var appSettings = _configuration.Get <AppSettings>(); _logger.LogInformation("Generating newsletter."); const string title = "Weekly Reddit"; var redditOptions = new RedditOptions { Username = appSettings.Reddit.Username, Password = appSettings.Reddit.Password, ClientId = appSettings.Reddit.ClientId, ClientSecret = appSettings.Reddit.ClientSecret }; _logger.LogInformation("Fetching content..."); using var redditClient = await RedditClient.CreateAsync(redditOptions); var trendings = await redditClient.GetTrendings(); var subreddits = await redditClient.GetSubredditsTopPosts(); var subredditBatches = subreddits.Batch(25).ToList(); for (int i = 0; i < subredditBatches.Count; i++) { var subredditBatch = subredditBatches[i]; var countString = subredditBatches.Count < 2 ? string.Empty : $"({i + 1}/{subredditBatches.Count}) "; _logger.LogInformation($"Generating email {countString}..."); var html = DataFormatter.GenerateHtml(new FormatterOptions { Subreddits = subredditBatch, Title = title, IssueDate = DateTime.Today, Trendings = i == 0 ? trendings : Enumerable.Empty <RedditPost>() }); var emailOptions = new EmailOptions { Password = appSettings.SmtpSettings.Password, Username = appSettings.SmtpSettings.Username, SmtpServer = appSettings.SmtpSettings.Server, SmtpPort = appSettings.SmtpSettings.Port }; _logger.LogInformation($"Sending email {countString}..."); using var emailClient = new EmailClient(emailOptions); await emailClient.SendAsync(new EmailContent { Content = html, Subject = $"{title} for {redditOptions.Username} {countString}// {DateTime.Today.ToLongDateString()}", FromName = title, FromAddress = appSettings.EmailSettings.FromAddress, To = appSettings.EmailSettings.ToAddress }); await Task.Delay(TimeSpan.FromSeconds(1)); // Prevent emails from arriving out of order. } _logger.LogInformation("Newsletter sent!"); }
public Task SendAsync(IdentityMessage message) { return(EmailClient.SendAsync(message.Destination, message.Subject, message.Body)); }
public void BadRequest_ThrowsException() { EmailClient emailClient = CreateEmailClient(HttpStatusCode.BadRequest); EmailMessage emailMessage = DefaultEmailMessage(); RequestFailedException?exception = null; if (IsAsync) { exception = Assert.ThrowsAsync <RequestFailedException>(async() => await emailClient.SendAsync(emailMessage)); } else { exception = Assert.Throws <RequestFailedException>(() => emailClient.Send(emailMessage)); } Assert.AreEqual((int)HttpStatusCode.BadRequest, exception?.Status); }