Tuesday, May 29, 2012

.NET 4.0 Fails When sending emails with attachments larger than 3MB



SMTPClient class throws "System.IndexOutOfRangeException: Index was outside the bounds of the array." exception while sending mails with attachment file size more than 3MB.
Recently I encounter a bug in .net while testing mail sending feature in our app. Upon little googling, found that it is a bug in SMTPClient class available in .NET 4.0.

Check below link for details of the bug:
https://connect.microsoft.com/VisualStudio/feedback/details/620785/important-bug-system-net-mail-smtpclient-send-mailmessage-causes-index-was-outside-the-bounds-of-the-array-because-of-the-content-of-an-attachment?wa=wsignin1.0

http://stackoverflow.com/questions/9703675/random-index-was-outside-the-bounds-of-the-array-as-an-smtpexception

Below is the link for Hot fix for .NET Framework to fix this issue:
https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=30226




Wednesday, May 16, 2012

Using XML path to get the emailaddress of contacts


 CREATE TABLE #Email
            (
              FKGUID UNIQUEIDENTIFIER,
              EmailAddress VARCHAR(2500)
            )  
               
        CREATE TABLE #EmailAddressTable
            (
              EmailAddress VARCHAR(2500),
              IsDefault INT DEFAULT ( 0 ),
              FKGUID UNIQUEIDENTIFIER
            )  


  INSERT  #EmailAddressTable ( EmailAddress, FKGUID )
                                SELECT   EmailAddress ,
                                        EmployeeGUID
                                FROM    dbo.Employee


 INSERT  #Email
                SELECT  fkGUId,
                        SUBSTRING(( SELECT  ',' + s.EmailAddress
                                    FROM    #EmailAddressTable s
                                    WHERE   ISNULL(s.EmailAddress, '') != ''
                                            AND a.FKGUID = s.FKGUID
                                    ORDER BY s.EmailAddress
                                  FOR
                                    XML PATH('')
                                  ), 2, 200000)
                FROM    #EmailAddressTable a
                WHERE   ISNULL(a.EmailAddress, '') != ''
                GROUP BY a.FKGUID

The temp table #Email contains all the email addresses of each employee.