Thursday, May 10, 2007

Receive and parse email in .NET

How to: Receive email through pop3 and parse mime message into From, To, Subject and Body parts

This task is not as trivial as it seemed to be.
Reasons:
1) .NET framework does not have classes which handle POP3
2) Email is sent in MIME format, which is quite tricky. In particular, email can have recursive attachments embedded into raw MIME email.

So far my research recommends two approaches:

Approach 1: Use an open source project.

It seems that the best open source MIME project is
LumiSoft .NET MIME parser.
You may browse all content here
Lumisoft Forum

Pop3 client retrieval & parsing sample
using(POP3_Client c = new POP3_Client()){
c.Connect("ivx",110);
c.Authenticate("test","test",true);

POP3_MessagesInfo mInf = c.GetMessagesInfo();

// Get first message if there is any
if(mInf.Count > 0){
byte[] messageData = c.GetMessage(mInf[0]);

// Parse message
Mime m = Mime.Parse(messageData);
string from = m.MainEntity.From;
string subject = m.MainEntity.Subject;
// ...
}
}



Approach 2: Buy a component

It seems that the best .NET Mime component available on the market so far is:
MIME4NET "True .Net Mime Parser".
The cost: $50

Tuesday, March 06, 2007

This report requires a default or user-defined value for the report parameter

I just spent couple of hours fighting "This report requires a default or user-defined value for the report parameter" error.
Report worked fine in report designer, but refused to properly render in serverReport.Render(...) method.

I tried Google Search and all voodoo magic, including:
- Renaming my parameter;
- Set default value for my parameter;
- Restarting VS.NET;
- Re-creating my report from scratch.
Nothing helped.
I even though about manual rebuilding OLAP cube in Analysis Server ...

But then I simply changed "Available values" [for my parameter] from "From query" to "Non-queried".

That helped.

Enjoy!
:-)


Keywords: VS 2005, VS2005, Visual Studio .NET 2005, OLAP, Microsoft Reporting Services, Microsoft Analysis Services, Microsoft SQL Server 2005, C#, VB.NET.

Friday, February 23, 2007

Google Desktop can be hacked

This very interesting article explains in details how cross site scripting can be used to hack google desktop (if new version of Google desktop in not installed yet):
Overtaking-Google-Desktop.pdf

This demo shows how potential hacker can operate:
Demonstration of Google Desktop vulnerability

The article is pretty impressive and made me think about XSS vulnerability of my own postjobfree.com web site.


Keywords: XSS, Web site security, javascript, hacker attack, XSS, RegEx, hack, Google Desktop.

Thursday, February 22, 2007

Geeks Rule and MBAs

Eric Sink (SourceGear) recommends to start software company with developers only.
====
Geeks Rule and MBAs Drool

It is common to see software companies starting out with two founders, a geek and an MBA. Do you really need the MBA?

If I were to oversimplify the message of this article, I would make two statements:

Developers add more value to a software company than anybody else.
The truth of statement 1 is inversely correlated with the size of the company.
When a company is very small or just getting started, nobody can add value as well as a developer because there isn't really much other stuff that needs to be done. You don't have customers yet.
====

Thursday, January 25, 2007

Health Monitoring in ASP.NET 2.0

Health Monitoring in ASP.NET 2.0 helps monitor problems in your application. Here's nice MSDN article about it:
How To: Use Health Monitoring in ASP.NET 2.0
In particular,
System.Web.Management.SqlWebEventProvider works quite good out of the box.

Note, that Health Monitoring designed for web forms applications and does not target winforms applications.
For example, if you want logging Windows Services, Log4Net 3rd party component, or through embedded System.Diagnostics.EventLog component are the ways to go.


If you want to get error messages from your site through email, read this:
How to: Send E-mail for Health Monitoring Notifications

Note, however, that SimpleMailWebEventProvider has some unpleasant limitations:
1) You cannot send emails using SSL SMTP, so Gmail's SMTP is not available for you. The reason here is that uses standard SMTP ASP.NET provider, and standard SMTP ASP.NET provider is not fully configurable through web.config.
There is no way for you to specify:
smtpClient.EnableSsl = true;
As a result, emails sent through Google's SMTP are simply dissappear.

2) You cannot override SimpleMailWebEventProvider provider, because it's sealed.
So, you have to write your own mail provider from scratch.

Implementing your own isn't very hard.
I tried to inherit my EmailEventProvider from MailWebEventProvider, but I couldn't even make the code compile. It seems that MailWebEventProvider if poorly written (yeap, not every developer at MS is good).

But inheriting from BufferedWebEventProvider worked like a charm.
Here's the C# code:
=======================
using System;
using System.Text;
using System.Web;
using System.Web.Management;
using System.Configuration;
using System.Collections.Specialized;

namespace MyNameSpace
{
sealed class EmailEventProvider : BufferedWebEventProvider
{
private string _to;
private string _subject;

public override void Initialize(string name, NameValueCollection config)
{
GetAndRemoveStringAttribute(config, "to", ref this._to);
GetAndRemoveStringAttribute(config, "subject", ref this._subject);
if (string.IsNullOrEmpty(this._to))
{
throw new ConfigurationErrorsException(string.Format("Recipient must be defined for {0}provider", name));
}
base.Initialize(name, config);
}

private static void GetAndRemoveStringAttribute(NameValueCollection config, string attrib, ref string val)
{
val = config.Get(attrib);
config.Remove(attrib);
}

public override void ProcessEventFlush(WebEventBufferFlushInfo flushInfo)
{
StringBuilder sb = new StringBuilder();
// Write flushInfo.Events:
foreach (WebBaseEvent wbe in flushInfo.Events)
{
sb.AppendFormat("{0}\r\n", wbe.ToString(true, true));
}

SendMail(DateTime.Now.ToString(), _to, _subject, sb.ToString());
}
}
}
=======================


Here's web.config's code:
----------------
<healthMonitoring enabled="true" heartbeatInterval="0" >
  <providers>
    <add name="MyEmailEventProvider" type="MyNameSpace.EmailEventProvider" buffer="false"
         to="xxx@gmail.com"
         subject="Crash in MyAspNet app" />
  </providers>
  <rules>
    <add name="All Errors by Email" eventName="All Errors" provider="MyEmailEventProvider" />
  </rules>
</healthMonitoring>
----------------

Thursday, December 28, 2006

Intelligent Job Search

www.ijsearch.com allows searching for jobs from several major job portals simultaneously.

Monday, December 11, 2006

ReportViewer Control

Reporting Made Easy is a nice article about new ReportViewer Control in Visual Studio 2005.
ReportViewer Control allows to create good-looking and robust reports in ASP.NET 2.0.

Also article discusses relationships between ReportViewer Control and SQL Server 2005 Reporting Services.

Technologies: ASP.NET 2.0, C#, VB.NET, SQL Server.

Followers

About Me

My photo
Email me: blog@postjobfree.com