Sendgrid not sending email in an on demand azure webjob running as console application - azure

I have an on-demand azure webjob running as a console application.
Objective is send emails to customers.
Issue is it does not send emails.
My code is heavily lifted from msdn section How to: Send an Email. Following is my code snippet.
class Program
{
static void Main(string[] args)
{
Execute(Environment.GetEnvironmentVariable("WEBJOBS_COMMAND_ARGUMENTS")).Wait();
}
static async Task Execute(string email)
{
DeserializedCustomer customer = JsonConvert.DeserializeObject<DeserializedCustomer>(email);
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
var client = new SendGridClient(apiKey);
SendGridMessage msg = new SendGridMessage()
{
From = new EmailAddress(customer.Email, "From Email"),
Subject = $"Inquiry by - {customer.FirstName} {customer.LastName}",
PlainTextContent = customer.Comments + Environment.NewLine + $"Phone : {customer.Phone}",
};
msg.AddTo(new EmailAddress(ToMail, ToMailName));
if (!String.IsNullOrWhiteSpace(customer.Attachment))
{
List<Attachment> attachments = new List<Attachment>()
{
new Attachment()
{
Content = customer.Attachment,
Type = customer.AttachmentType,
Filename = customer.AttachmentFileName,
Disposition = "inline",
ContentId = customer.AttachmentFileName + Guid.NewGuid()
}
};
msg.Attachments = attachments;
}
await client.SendEmailAsync(msg);
}
}
Searching I found an SO post where giving time to sendgrid to send email in a console application
Adding time did not help either.
If there are some quirkiness about sending emails from azure webjobs running as console applications then I'm unaware.
Searching further I found this SO post where they successfully tested sending emails in console application so I thought it might help but it did not send emails.
All the above examples are more than a year old and none of them work currently.
I tried using an earlier version of SendGrid library but I am stuck on
var transportWeb = new Web(credentials);
transportWeb.DeliverAsync(myMessage);
as SendGrid library has been updated.
On github I found this which clearly states that console apps only work with transportWeb.DeliverAsync(myMessage).Wait
therefore I am trying with transportWeb.
Is there an amalgamation of azure with on demand webjob running as console application?
Can anyone help?
Update
After Randy Minder's help I updated the code to the following
static async Task Execute(string email)
{
try
{
DeserializedCustomer customer = new DeserializedCustomer();
customer = JsonConvert.DeserializeObject<DeserializedCustomer>(email);
Console.WriteLine(customer);
SendGridMessage msg = new SendGridMessage();
msg.From = new MailAddress(customer.EmailAddress, "From Email");
msg.Subject = $"Inquiry by - {customer.FirstName} {customer.LastName}";
msg.Text = customer.Comments + Environment.NewLine + $"Phone : {customer.Phone}";
msg.AddTo(ToMail);
// Create a network credentials object
var credentials = new NetworkCredential(Environment.GetEnvironmentVariable("UserName"), Environment.GetEnvironmentVariable("Password"));
var transportWeb = new SendGrid.Web(credentials);
transportWeb.DeliverAsync(msg).Wait();
}
catch (Exception ex)
{
DateTime now = DateTime.Now;
Trace.TraceError($"{now.ToLongDateString()} {now.ToLongTimeString()}" + Environment.NewLine + new ExceptionSerializer(ex));
}
}
I'm using SendGrid 6.1.0
<package id="Sendgrid" version="6.1.0" targetFramework="net47" />
I do not get any exception and my webjob runs to success
[09/06/2017 17:48:42 > 1a8d37: SYS INFO] Run script 'SaSRizqTechCloudWhizEngineering.Backgr.exe' with script host - 'WindowsScriptHost'
[09/06/2017 17:48:42 > 1a8d37: SYS INFO] Status changed to Running
[09/06/2017 17:48:42 > 1a8d37: INFO] SerializedEmail - {"FirstName":"adsfkh","LastName":"adfkjladf","EmailAddress":"jamilakhtar#gmail.com","Phone":"","Comments":"lkjadf ","AttachmentType":"","AttachmentFileName":"","Attachment":null}
[09/06/2017 17:48:42 > 1a8d37: INFO] FirstName adsfkh - LastName adfkjladf - EmailAddress jamilakhtar#gmail.com - Phone - Comments lkjadf - Attachment - AttachmentType - - AttachmentFileName
[09/06/2017 17:48:44 > 1a8d37: SYS INFO] Status changed to Success
However I do not get any email

When attempting to send an email via SendGrid in a console app, you have to do it a bit differently. Here is a method I have that works in a console app:
/// <summary>
/// Send the email async in a console app.
/// </summary>
public async Task SendAsync()
{
// Create a network credentials object
var credentials = new NetworkCredential(azureUserName, azurePassword);
// Create an Web transport for sending the email
var transportWeb = new Web(credentials);
transportWeb.DeliverAsync(this._email).Wait();
}
This is what I use in a non-console app:
/// <summary>
/// Send the email async in backend or MVC code.
/// </summary>
public async Task SendAsync()
{
// Create a network credentials object
var credentials = new NetworkCredential(azureUserName, azurePassword);
// Create an Web transport for sending the email
var transportWeb = new Web(credentials);
await transportWeb.DeliverAsync(this._email).ConfigureAwait(false);
}
The actual email object is contained in this._email.

According to your description, I have also create a test demo on my side, it works well. I used the sendgird SDK is 9.90.
The details codes is like as below:
class Program
{
static void Main(string[] args)
{
string serializedEmail = args[0];
Console.WriteLine($"SerializedEmail - {serializedEmail}");
Customer customer = JsonConvert.DeserializeObject<Customer>(args[0]);
//Customer customer = new Customer() { Name = "aaaa" };
Execute(customer).Wait();
Console.WriteLine(customer.Name);
Console.ReadLine();
}
static async Task Execute(Customer customer)
{
var client = new SendGridClient("apikey");
var from = new EmailAddress("sendgird#xxxxxx.com", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("sendgird#xxxxxxx.com", "Example User");
var plainTextContent = $"Name : {customer.Name}";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
}
}
public class Customer
{
public string Name { get; set; }
}
However I do not get any email
I suggest you could firstly access this url to check the sendgird has already send the email to right email address.
The result is like this:

Related

.net 6 Blazor WebAssembly Deployed to Azure 400 Error when trying to send email via SendGrid

I have a basic Blazor WebAssembly project that is using SendGrid to send form data via email. Locally it works fine. I have deployed to Azure App Service and setup API Management as well.
Locally I set an EnvironmentVariable to hold my SendGrid Api key.
This is the EmailService code.
public async Task<ServiceResponse<Contact>> SendEmail(Contact info)
{
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var toEmail = _config.GetSection("SendGrid:EmailTo").Value;
var toName = _config.GetSection("SendGrid:EmailName").Value;
var _subject = _config.GetSection("SendGrid:EmailSubject").Value;
var client = new SendGridClient(apiKey);
var from = new EmailAddress($"{info.Email}", $"{info.Name}");
var subject = _subject;
var to = new EmailAddress(toEmail, toName);
var plainTextContent = info.Message;
var htmlContent = info.Message;
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
if (response.IsSuccessStatusCode)
{
return new ServiceResponse<Contact>
{
Data = info,
Success = true,
Message = "Message has been sent."
};
}
else
{
return new ServiceResponse<Contact> { Success = false };
}
}
In Azure App Service I created an Application setting named "SENDGRID_API_KEY" and it holds the SendGrid API key. I also tried modifying the request before it is sent and added the Authorization header there as well.
When the form submits, it returns a 400 (Bad Request) error.
I do not have a secure certificate on this site yet.
Any ideas? Please let me know if you need more info.

Configuring Twilio SMS from Azure Functions v2

I have some code where I'm reading messages off of an Azure Event Hub that I want to either send an email or send an SMS.
The email is working through send grid, but I'm not sure how to configure the SMS part though.
I think I'd want to use Twilio and here's a sample of what my code's like. The "messageCollector" works for sending Email since there's some configuration for SendGrid in the local json. How do I configure Twilio?
[FunctionName("SendAlert")]
public static async Task Run(
[EventHubTrigger("v1-email-hub", Connection = "EventHubConnection")] EventData[] events,
[SendGrid] IAsyncCollector<SendGridMessage> messageCollector,
[TwilioSms] IAsyncCollector<CreateMessageOptions> smsCollector,
[Inject] NotificationEventLogic eventLogic,
ILogger log)
{
foreach (EventData eventData in events)
{
string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
var notificationEvents = JsonConvert.DeserializeObject<List<NotificationEvent>>(messageBody);
foreach (var ev in notificationEvents)
{
if (ev.NotificationEventType == NotificationEventType.Email)
{
var message = new SendGridMessage();
// ... ... make message and add it
await messageCollector.AddAsync(message);
}
else if (ev.NotificationEventType == NotificationEventType.SMS)
{
// Not sure how to get this to work
var mobileMessage = new CreateMessageOptions(new PhoneNumber(ev.Data))
{
Body = $"Notification {ev.NotificationId}"
};
await smsCollector.AddAsync(mobileMessage);
}
// await smsCollector.AddAsync()
await eventLogic.CreateEventAsync(ev);
}
}
}
You will need to configure it in attribute
[TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX")]
as it mentioned in documentation
TwilioAccountSid This value must be set
to the name of an app setting that holds your Twilio Account Sid e.g.
TwilioAccountSid. If not set, the default app setting name is
"AzureWebJobsTwilioAccountSid".
TwilioAuthToken This value must be set to
the name of an app setting that holds your Twilio authentication token
e.g. TwilioAccountAuthToken. If not set, the default app setting name
is "AzureWebJobsTwilioAuthToken".

Azure service bus message delivery count is not increasing or is reset when topic subscription disabling/enabling

I have the following workflow:
Service bus receives messages.
Azure function triggers and tries to deliver this messages via HTTP to some service.
If delivery failed - function throws exception (custom) and disables topic subscription via code below:
The other function in parallel pings special health check endpoint of the service, and if it gets 200 - it tries to enable subscription and make the flow work again.
The steps could be reproduced N times, cause health check will return 200, thus the delivery url of point 2 - 4xx code.
After the next attempt to enable subscription and deliver the message, I expect that delivery count will be increased and in the end (after 10 deliveries attempt) it will get to dead-letter.
Actual - it equals 1.
I assume, that it may reset when I call CreateOrUpdate with status changed.
If yes - what is the other way to manage subscription status instead of Microsoft.Azure.Management package so that the messages delivery count will not be reset?
UPDATE: Function code
public static class ESBTESTSubscriptionTrigger
{
private static readonly HttpClient Client = new HttpClient();
private static IDatabase redisCache;
[FunctionName("ESBTESTSubscriptionTrigger")]
[Singleton]
public static async Task Run([ServiceBusTrigger("Notifications", "ESBTEST", AccessRights.Listen, Connection = "NotificationsBusConnectionString")]BrokeredMessage serviceBusMessage, TraceWriter log, [Inject]IKeyVaultSecretsManager keyVaultSecretsManager)
{
var logicAppUrl = await keyVaultSecretsManager.GetSecretAsync("NotificationsLogicAppUrl");
if (redisCache == null)
{
redisCache = RedisCacheConnectionManager.GetRedisCacheConnection(
keyVaultSecretsManager.GetSecretAsync("RedisCacheConnectionString").GetAwaiter().GetResult());
}
if (string.IsNullOrWhiteSpace(logicAppUrl))
{
log.Error("Logic App URL should be provided in Application settings of function App.");
throw new ParameterIsMissingException("Logic App URL should be provided in Application settings of function App.");
}
var applicaitonId = serviceBusMessage.Properties["applicationId"].ToString();
var eventName = serviceBusMessage.Properties.ContainsKey("Event-Name") ? serviceBusMessage.Properties["Event-Name"].ToString() : string.Empty;
if (string.IsNullOrWhiteSpace(applicaitonId))
{
log.Error("ApplicationId should be present in service bus message properties.");
throw new ParameterIsMissingException("Application id is missing in service bus message.");
}
Stream stream = serviceBusMessage.GetBody<Stream>();
StreamReader reader = new StreamReader(stream);
string s = reader.ReadToEnd();
var content = new StringContent(s, Encoding.UTF8, "application/json");
content.Headers.Add("ApplicationId", applicaitonId);
HttpResponseMessage response;
try
{
response = await Client.PostAsync(logicAppUrl, content);
}
catch (HttpRequestException e)
{
log.Error($"Logic App responded with {e.Message}");
throw new LogicAppBadRequestException($"Logic App responded with {e.Message}", e);
}
if (!response.IsSuccessStatusCode)
{
log.Error($"Logic App responded with {response.StatusCode}");
var serviceBusSubscriptionsSwitcherUrl = await keyVaultSecretsManager.GetSecretAsync("ServiceBusTopicSubscriptionSwitcherUri");
var sbSubscriptionSwitcherResponse = await Client.SendAsync(
new HttpRequestMessage(HttpMethod.Post, serviceBusSubscriptionsSwitcherUrl)
{
Content =
new
StringContent(
$"{{\"Action\":\"Disable\",\"SubscriptionName\":\"{applicaitonId}\"}}",
Encoding.UTF8,
"application/json")
});
if (sbSubscriptionSwitcherResponse.IsSuccessStatusCode == false)
{
throw new FunctionNotAvailableException($"ServiceBusTopicSubscriptionSwitcher responded with {sbSubscriptionSwitcherResponse.StatusCode}");
}
throw new LogicAppBadRequestException($"Logic App responded with {response.StatusCode}");
}
if (!string.IsNullOrWhiteSpace(eventName))
{
redisCache.KeyDelete($"{applicaitonId}{eventName}DeliveryErrorEmailSent");
}
}
}

Azure log showing: "The supplied notification payload is invalid" for official Xamarin.Android sample

So I tried running this Push notification sample for Xamarin.Android http://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-android-get-started-push/ and after following instructions from the docs - I got it up and running. The insertion of items work absolutely fine however push notification refuses to work.
This is the error I get on Azure for push: Error: 400 - The supplied notification payload is invalid.
Anyone else tried running this sample on their device and tried push notifications? The error isn't doing much to help my case.
The sample is using PushSharp.
I'd appreciate any help. Thanks a bunch!
This is how I send push notification to Google Cloud Messaging from the back-end server.
public async Task<bool> SendNotification(int id, int index, string from, string text, string tag)
{
try
{
var payload = new
{
data = new
{
message = new
{
// this part can be anything you want
id,
index,
from,
text,
when = DateTime.UtcNow.ToString("s") + "Z"
}
}
};
var json = JsonConvert.SerializeObject(payload);
await _hubClient.SendGcmNativeNotificationAsync(json, tag);
return true;
}
catch (ArgumentException ex)
{
// This is expected when an APNS registration doesn't exist.
return false;
}
Then in your app Intent Service, you can parse the JSON "message":
protected override void OnMessage(Context context, Intent intent)
{
var message = intent.Extras.GetString("message");
// message is JSON payload
// { "id":"someid", "index":"1", "text":"some text","from"... }
var json = JObject.Parse(message);
var id = json["id"].ToString();
var index = json["index"].ToString();
var text = json["text"].ToString();
var from = json["from"].ToString();
var when = DateTime.Parse(json["when"].ToString());
// do whatever you want with your values here
}

Why Send Email Plugin is not working?

I have created a plugin (using Developer's tool kit) which will send a mail to Participants, which is converted Contacts, whenever a Participants is created.
But this plugin is not Working. When I was trying to debug it this following message came in plugin registration tool(SDK)
Profiler> Plug-in AppDomain Created
Profiler> Parsed Profiler File Successfully.
Profiler> Constructor Execution Started: XXXXXXXX
Profiler> Constructor Execution Completed Successfully (Duration = 8ms).
Profiler> Profiler Execution Started: XXXXXXXXXXX
Plug-in> Entered CRMEmailToParticipantsPackage.EmailPlugins.PostParticipantCreate.Execute(),
Plug-in> Exiting CRMEmailToParticipantsPackage.EmailPlugins.PostParticipantCreate.Execute(),
Profiler> Profiler Execution Completed Successfully (Duration = 57ms).
Profiler> Profiler AppDomain Unloaded
. Pipeline Stage is Pre-Validation on Create Message.
And this is my Code:
protected void ExecutePostParticipantCreate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
try
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "new_participant")
{
Guid Contact_id = ((EntityReference)entity.Attributes["new_participantscontact"]).Id;
Guid trip_Id = ((EntityReference)entity.Attributes["new_tripparticipants"]).Id;
ColumnSet col1 = new ColumnSet("new_name", "new_date", "new_destination");
Entity trip = service.Retrieve("new_trip", trip_Id, col1);
var Trip_name = trip.Attributes["new_name"];
var Trip_date = trip.Attributes["new_date"];
var Trip_destination = trip.Attributes["new_destination"];
string emailBody = "Hi, your " + Trip_name.ToString() + "is booked on : " + Trip_date.ToString() + "; Destination : " + Trip_destination.ToString();
Guid _userId = context.UserId;
ActivityParty fromParty = new ActivityParty
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)
};
ActivityParty toParty = new ActivityParty
{
PartyId = new EntityReference("new_participantscontact", Contact_id)
};
Email email = new Email
{
To = new ActivityParty[] { toParty },
From = new ActivityParty[] { fromParty },
Subject =Trip_name.ToString()+ " :Trip Details",
Description = emailBody,
DirectionCode = true
};
Guid emailID = service.Create(email);
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailID;
req.TrackingToken = "";
req.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)service.Execute(req);
}
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
Have I done any thing wrong here?
Thank you.
i think the first thing is discover if your plugin is fired or not. Try debug or use ITracingService to know that. If you discover that is your plugin isn't fired the code is unnecessary. Be careful that the deploy of a plugin in CRM Online has a limitation of running just in sandbox mode. Try see this step-by-step deploy of a plugin in CRM Online.
Are you able to debug using the steps listed here: http://microsoftcrmworld.blogspot.com/2013/01/debug-plugins-on-your-local-pc-without.html ?
If not, you can add some localContext.trace() calls and then throw an exception at the very end of your execute method. Download and view the log file to see the outputs from localContext.trace(). This will give you an idea of what's going on.
Your code seems to be fine...
I observed that you are sending mail to a custom entity...
Have you enabled "Sending Email" option during customization?
Check this link for enabling it

Resources