Posted tagged ‘.NET’

Sending email through GMail using SmtpClient

November 13, 2010

Ok so I just signed up for Google Apps primarily because I wanted to get off of GoDaddy’s email offering.  I chose Google Apps Standard because 1) it is free and 2) because Google is awesome.  Oh btw Microsoft I did try to get setup on Office Live Small Business, however, you required me to host my web site there in order to have email.  I use Azure for hosting so it sure would be nice if you could give us Small Business Azure users a hook up on Office Live Small Business for hosting email without a web site.  Anyway  I’m not made, I’m cool with Google.

So down to the code that works to send email from Google.

MailMessage message = new MailMessage();
message.From = new MailAddress(you@yourDomain.xxx);
message.To.Add(“yourFriend@someDomain.com”);
message.Subject = “TEST Message”;
message.IsBodyHtml = false;  // Could be true to.
message.Body = “TEST”;

SmtpClient smtp = new SmtpClient(“smtp.gmail.com”);
smtp.Credentials = new NetworkCredential(“you@yourDomain.xxx“, “your password”);
smtp.EnableSsl = true;
smtp.Port = 587;

smtp.Send(message);

Potential Issues

Error Message
“System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at “

Resolution
This could be a few different things.  First make sure that your user name and password are correct.  The second major thing and what I got burned on was setting the property “UseDefaultCredentials”.  I explicitly set this property to false after I set the Credentials property.  Well that is bad because, the credentials get set to null at that time.  So make sure at the time you call Send that the credentials are set.

Error Message
“The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first.”

Resolution
One reason why you might get this message is that you have not enabled SSL, so to resolve set EnableSsl = true.

Preparing for Startup Weekend with latest .NET coolness.

November 10, 2010

I’m heading to startup weekend Chicago on Friday and have setup my machine with the latest coolness from Microsoft.  They say the Ruby is King but I’m planning to show off .NET and gain some respect for us .NET developers.

My setup

  • .NET 4.0
  • ASP.NET MVC 3 Beta
  • Entity Framework 4 Code First CTP
  • SQL Server 2008 Express
  • SQL Server Compact 4.0 CTP

I started writing a small app earlier this week and all of the CTP’s and Betas seem to work well.  I am most excited about the EF 4.0 Code First CTP.  This basically takes database work out of the picture making it great for fast prototyping.  I will probably stick with SQL Server 2008 Express over the compact version just because I know that it is stable.  I also will likely use jQuery UI themes to make the site look decent without much work.  Although it would be great to get paired up with a designer.

Developer Environment Setup Instructions

Install Visual Web Developer 2010 Express

Download from: http://www.microsoft.com/express/downloads/
I assume that .NET 4.0 will get installed at the same time.  If not, you may need to download it separately.  I am personally running the Premium version but I wanted to include this incase someone wanted to get started for free.

Install ASP.NET MVC 3 Beta

Unfortunately you will need to install the ASP.NET MVC 3 Preview 1 first.  It can be downloaded from: http://www.microsoft.com/downloads/en/details.aspx?familyid=CB42F741-8FB1-4F43-A5FA-812096F8D1E8&displaylang=en

Files to install:
AspNetMVC3Setup.exe  (Preview 1)

Download the Beta from: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=0abac7a3-b302-4644-bd43-febf300b2c51&displaylang=en

Files to install:
AspNetWebPages.msi (Beta)
AspNetMVC3Setup.exe (Beta)

Microsoft ADO.NET Entity Framework Feature Community Technology Preview 4

This is used for code first development and will be the most valuable feature in rapid prototyping.  With this we won’t need to mess with configuration files or setting up a database first.
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=4e094902-aeff-4ee2-a12d-5881d4b0dd3e&displaylang=en

Microsoft SQL Server Compact 4.0 Community Technology Preview for Windows Desktop

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=0D2357EA-324F-46FD-88FC-7364C80E4FDB&displaylang=en

Files to install:
SSCERuntime_x86-ENU.exe

What follows is still a work in progress but I wanted to get the post out before the weekend.  I’ll update as time permits.

Create Project

  1. Open Visual Studio
  2. Create New Project
  3. Select Web
  4. Select ASP.NET MVC 3 Web Application
  5. Add Reference to System.Data.Entity.CTP located at “\Program Files\Microsoft ADO.NET Entity Framework Feature CTP4\Binaries”

Code First Walk through by Scott Gu
http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

NOTE: Razor Syntax Highlighting and Intellisense is missing from the Beta.  Sucks.

Even though the new syntax looks great, I’m probably going to stick with the what I’m use to for this weekend.  No Intellisense is a performance killer especially when you are learning the syntax.

Add Razor Highlighting

Option 1:   You can set the cshtml extension to use the Html Editor so that you at least get html highlighting.

  1. Go to Tools | Options | Text Editor | File Extension
  2. Enter cshtml
  3. Select Html Editor
  4. Click Add

    (You may need to close and reopen your cshtml for highlighting to start)

Option 2: Install the Razor Syntax Highlighter.  I didn’t try this yet because it is dependant on Web Matrix.

http://visualstudiogallery.msdn.microsoft.com/en-us/8dc77b9c-7c83-4392-9c46-fd15f3927a2e?SRC=Home

Calling .NET from PowerBuilder

February 22, 2010

(This is an old post that I forgot to publish)

We were working with an older version of PowerBuilder (version 8.0) and needed to call out to .NET.  There wasn’t much documented and there were a few stumbling points, but we persisted and have proven to ourselves that it can be done.  However, we did rely on the use of COM.

Things to keep in mind:
– Make sure to restart PowerBuilder after any changes to registry or copying of dll’s. (If something does not work, restart PowerBuilder)
– You will likely need to close PowerBuilder in order to delete the .NET dll.

.NET Class Library (dll)

1) Create a .NET Class Library
2) Check the “Register for COM interop” checkbox on the Build tab of the project properties.
3) Expose the assembly to COM by Setting ComVisible to true in the AssemblyInfo file.  Ex. [assembly: ComVisible(true)]
4) Create a class.  Notice I did not include a Namespace in the example below.  I assume that you could get this to work with a namespace, however, I have not tested it.

using System;
using System.Runtime.InteropServices;

[ClassInterface(ClassInterfaceType.AutoDual)]  // Needed to export the public members of the .NET Component’s class into a default Class Interface in the generated typelibrary.
public class Tester
{
public Tester()
{
// Default constructor required to allow object to be co-creatable from COM.
}

public string Test(string input)
{
return input;
}

}

5) Build your assembly.

Register your dll

You will need to use regasm.exe which can be run using the Visual Studio Command Prompt.

Register on your machine:
regasm YourAssembly.dll

To register on a machine that does not have regasm you can create a registry file as follows:
regasm YourAssembly.dll /regfile:YourAssembly.reg
Then copy both the .dll and the .reg file to the deployment machine and run the .reg file.

Verify the dll works from VBScript (Optional)

For a sanity check create a vbs file with the following code and run it.
dim obj
set obj = CreateObject(“Tester”)

MsgBox(obj.Test(“Hello World”))

PowerBuilder Code

1) Create a PowerBuilder application

2) Place the following code in a button:

OLEObject obj_test

obj_test = CREATE OLEObject
if obj_test.ConnectToNewObject( ‘Tester’ ) = 0 then
MessageBox( ‘Test Function…’, String( obj_test.Test( ‘Hello World’ ) ) )
end if

3) Run it and click the button.  You should see a message box with “Hello World” in it.  If you don’t see it, try putting YourAssembly.dll in the path of your application.  I would first put it in the same directory as the PowerBuilder executable then once it works there start putting it in other directories that are in the path such as System32.

Check out what is exposed using OleView
From the Visual Studio Command Prompt execute OleView YourAssembly.dll

Resources

Awesome Article that goes into detail on .NET and COM interoperablity.
http://www.codeproject.com/KB/COM/cominterop.aspx#PART2

Escaping a curly braces in String.Format

December 31, 2008

If you ever recieve the error “Input string was not in correct format.” check the string for curly braces.  Since String.Format uses curley braces to denote the replacement positions you need to escape them.  To do so all you need to do is replace the single curley brace ‘{‘ with a double ‘{{‘.

Example:
Bad    – String.Format(“{Somthing To Format ‘{0}’}”, something);
Good – String.Format(“{{Somthing To Format ‘{0}’}}”, something);