Karatas, FERHAT
I would love to change the world, but they won't give me the source code!
ChangePassword control with Gmail Apps
5.09.2009 18:07:08 - Filed under : C# | Asp.net
BUG: ASP.net 2.0 PasswordRecovery/ChangePassword Web Control cannot send emails to SSL enabled SMTP Servers

This is a known issue or you can also call it as product limitation. PasswordRecovery or ChangePassword control read most of the settings from web.config file. Internally it uses System.Net.Mail to send out email, which does not support reading EnableSSL setting from web.config. This bring us into a situation where PasswordRecovery control cannot send emails to SSL enabled smtp servers.

So when a user tries to send a email to SSL enable server using PasswordRecovery or ChangePassword control, he get the following error.

Error: 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. 32sm15616652wfa.13
Cause :
There is no setting in Web.Config for System.Net.Mail (.net 2.0) that maps to EnableSSL property of System.Net.Mail.SmtpClient.
Resolution :
Step 1 : Add SendingMail event to control;

<asp:ChangePassword ID="ChangePassword1" runat="server" onsendingmail = "ChangePassword1_SendingMail">


Step 2 : Add codebehind codes;
protected void ChangePassword1_SendingMail(object sender, MailMessageEventArgs e)
        {
            e.Message.Body.SendMail(e.Message.To[0].ToString(), e.Message.Subject.ToString(), string.Empty);
            e.Cancel = true;
        }

Step 3 : SendMail (extention) method;
public static void SendMail(this string body, string to, string subject, string attachFilename)
        {
            Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
            string from = mailSettings.Smtp.From;

            MailMessage message = new MailMessage();
            message.Priority = MailPriority.Normal;
            message.From = new MailAddress(from, ConfigurationManager.AppSettings["SiteName"]);
            message.To.Add(new MailAddress(to));
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.SubjectEncoding = Encoding.UTF8;
            message.BodyEncoding = Encoding.UTF8;
            message.Body = body + "<br><br>" + ConfigurationManager.AppSettings["SiteDomain"] + "<br>" + DateTime.Now.ToString();

            if (attachFilename != string.Empty)
            {
                message.Attachments.Add(new Attachment(attachFilename));
            }

            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(message);
        }

Step 4 : Add smtp node to web.config;
<system.net>
  <mailSettings>
    <smtp from="sample@yourdomain.com" deliveryMethod="Network">
    <network host="smtp.gmail.com" port="587" userName="sample@yourdomain.com" password="xxxx" defaultCredentials="false"/>
    </smtp>
  </mailSettings>
</system.net>
<system.web>...

Keywords : Gmail apps with ChangePassword control in asp.net
with 0 comments

X-UA-Compatible
30.08.2009 16:43:40 - Filed under : C# | Asp.net | CSS | AJAX
Implementing the HTTP header is beneficial if a site owner wants most of their site to render as it did in IE7 or if there are no plans to update site content. Inclusion of this header honors any Quirks mode pages that belong to the site. Use below statement at top of your page :
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
or use it in web.config :
<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="X-UA-Compatible" value="IE=EmulateIE7" />
        </customHeaders>
    </httpProtocol>
</system.webServer>
Using the meta-tag on a per-page basis is beneficial when the publisher wants to opt-in specific pages to render as they did in IE7.

NOTE: The X-UA-Compatible tag and header override any existing DOCTYPE. Also, the mode specified by the page takes precedent over the HTTP header. For example, you could add the EmulateIE7 HTTP header to a site, and set specific pages to display in IE8 mode (by using the meta-tag with content=”IE8”).

Using the IE=EmulateIE7 compatibility tag is a simple way for users to continue their current experience when browsing your site until you can update with more standards-compliant content.  Although adding this tag will prevent most display issues, you may also need to update your site to properly detect IE8.

Keywords : Emulate IE7
with 0 comments

No buffer space available Fix
29.08.2009 12:08:04 - Filed under : C# | Asp.net | Misc | SQL
Error : An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full xxxx.xxxx.xxxx:80

Solution 1 :

I think I've found a reason for it: My anti-virus scanner, NOD32 v2.51.12 x64, with enabled HTTP scanning. If the HTTP scanning on port 80,8080,3128 and "Auto Detect HTTP communication on other ports" is enabled, both versions will crash. After I disabled the IMON (Internet Monitor) and EMON (MS Outlook E-Mail Monitor) features in NOD32 everything seems to work fine.

Solution 2 :

Editting the registry is not for beginners, if you don't know what you're doing I suggest you don't try this, basically it's use at your own risk. Anytime you want to edit the registry it is a good idea to back it up first..

If you are using Windows 95/98/Me follow these steps:

First step is to launch the registry editor. To do this go to Start, Run and type regedit. In the left pane navigate to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP once there, look for an entry called MaxConnections. If it exists highlight it by clicking it and then right click it and select modify. Increase the value (recommended value is to double the current value).

If the MaxConnections entry does not exist you must create it. To do this, right click in the right pane and choose new from the menu and select String Value. Give it the name MaxConnections. Then right click it and select modify and enter a value of 800.

Restart your computer, if all goes well then you fixed the problem, if not, revert the changes by restoring the registry. (You may have to reboot to safe mode to do this).

If you are running Windows NT/2000/XP follow these steps:
First step is to launch the registry editor. To do this go to Start, Run and type regedit. In the left pane navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters once there, you must create the entry TcpNumConnections. To do this, right click in the right pane and choose new from the menu and select DWORD Value. Give it the name TcpNumConnections. Then right click it and select modify and enter a value of 800.

Restart your computer, if all goes well then you fixed the problem, if not, revert the changes by restoring the registry. (You may have to reboot to safe mode to do this).

Consideration :

TIP 1 : I have seen NOD32 cause WSAENOBUFS (10055) on many systems. They seem to do something which lowers the resources available to networking applications.
TIP 2 : Problem is caused by a recent windows patch.  If you remove patch KB948609 the client side deserialization will except large datasets again.
TIP 3 : If an application performs many network connections simultaneously or within a short window of time, the application developer might need to increase this number and reduce the TcpTimedWaitDelay value to avoid receiving a WSAENOBUFS (10055) error: "An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full." (http://msdn.microsoft.com/en-us/library/ms819739.aspx)


Keywords : WSAENOBUFS, 10055
with 0 comments

The remote certificate is invalid
24.06.2009 22:54:49 - Filed under : C# | Asp.net
If I enable EnableSsl for smtpclient am getting an exception: "The remote certificate is invalid according to the validation procedure."

It means the server certificate isn't valid by regular validation rules. You can simply use this snippet to by-pass it:
public static bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; }

public static void SendMail(string body, string to, string subject, string attachFilename)
{
  MailMessage message = new MailMessage();
  message.Priority = MailPriority.Normal;
  message.To.Add(new MailAddress(to));
  message.Subject = subject;
  message.IsBodyHtml = true;
  message.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
  message.Body = body;

  if (attachFilename != string.Empty)
  {
    message.Attachments.Add(new Attachment(attachFilename));
  }

  ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
  SmtpClient client = new SmtpClient();
  client.EnableSsl = true;
  client.Send(message);
}

Keywords : Smtpclient, asp.net, system.net.mail, EnableSSL, validation.
with 0 comments

Lambda Expression
23.05.2009 13:39:41 - Filed under : C# | LINQ
Lambda Expressions provide a more concise,  functional syntax for writing anonymous methods.  They end up being  super useful when writing LINQ query expressions - since they provide a  very compact and type-safe way to write functions that can be passed as  arguments for subsequent evaluation. 
 
Example:
 
Sample 1 : 
public class Person
{
  public string FirstName;
  public string LastName;
  public int Age;
}

List<person> people = new List<person>
{
    new Person{FirstName = "Ferhat", LastName = "Karatas", Age=10},
    new Person{FirstName = "Elif", LastName = "Sila", Age=5},
    new Person{FirstName = "Sevim", LastName = "Karatas", Age=15}
}; 
 
// return all Karatas
IEnumerable<person> results = people.Where(p => p.LastName == "Karatas");
double averageAge = people.Average(p => p.Age);
Label1.Text = results.Count().ToString() + "-" + averageAge.ToString();

Sample 2 :

var numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var evennumber = numbers.FindAll(p => p % 2 == 0);
foreach (int item in evennumber)
{
    Label1.Text += item + "-";

 
The p => expressions highlighted above  in red are Lambda expressions.  In the sample above I'm using the first  lambda to specify the filter to use when retrieving people, and the  second lambda to specify the value from the Person object to use when  computing the average.

Keywords : Linq, lambda expression
with 0 comments

Previous | Next
Current Page: 1