19 Januar 2016

How to Send Mails in ASP .NET 5 on Azure

Unfortunately neither SendGrid nor System.Net.Mail is supported in the actual version of ASP .NET 5 (RC).

So how to send mails then?
One solution is to use MailKit https://github.com/jstedfast/MailKit

To use MailKit just add "MailKit": "1.3.0-beta5" to your project.json

What you need is an SMTP server. On Azure you can create a SendGrid account and use the host name and the credentials provided in the Configurations section. SendGrid on Azure is free for up to 25'000 mails per month!

The code to send a mail is straight forward.

private void SendEmail()
{
  var message = new MimeMessage();
  message.From.Add(new MailboxAddress("Peter Muster", "peter@muster.ch"));
  message.To.Add(new MailboxAddress("Petra Muster", "petra@muster.ch"));
  message.Subject = "Some Subject";

  message.Body = new TextPart("plain")
  {
    Text = string.Format(@"Hello,

Just a little message for you"
  };

  using (var client = new SmtpClient())
  {
    client.Connect("smtp.sendgrid.net", 587, false);
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    client.Authenticate("your_id@azure.com", "password");

    client.Send(message);
    client.Disconnect(true);
  }
}


18 Januar 2016

Logging in ASP .NET 5 on Azure

Currently ASP .NET 5 is not able to log on Azure.

Fortunately there is a workaround described here https://github.com/aspnet/Home/issues/773

The trick is to use Serilog an open source logging framework for the .NET platform.

12 Januar 2016

ASP .NET 5 Continuous Deployment from Visual Studio Online to Azure

ASP .NET 5 uses DNX and the standard build in Visual Studio Code does not work.

You have to create your on build as describe in this article:
https://msdn.microsoft.com/Library/vs/alm/Build/azure/deploy-aspnet5

But in the tutorial there is missing one important step:
In the Visual Studio Build Step you have to uncheck "Restore NuGet Packages"

Without this you will get

##[error]Error parsing solution file at C:\a\1\s\src\simashr\simashr.xproj: Exception has been thrown by the target of an invocation.
##[error]Unexpected exit code 1 returned from tool NuGet.exe