Wednesday, 30 January 2013

C#: Send a simple E-Mail via. Google Gmail SMTP Server



Here's a code snippet which allows you to send an E-Mail using Gmails SMTP server...



Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Mail;
  6. using System.Text;
  7.  
  8. /// <summary>
  9. /// Simple Emailer class for sending a simple email.
  10. /// </summary>
  11. public class Emailer
  12. {
  13.     /// <summary>
  14.     /// Takes a users email and item name and generates an email
  15.     /// </summary>
  16.     /// <param name="recipient">Recipients e-mail address</param>
  17.     public static void SendMail(string recipient)
  18.     {
  19.         try
  20.         {
  21.             // Setup mail message
  22.             MailMessage msg = new MailMessage();
  23.             msg.Subject = "Email Subject";
  24.             msg.Body = "Email Body";
  25.             msg.From = new MailAddress("FROM Email Address");
  26.             msg.To.Add(recipient);
  27.             msg.IsBodyHtml = false; // Can be true or false
  28.  
  29.             // Setup SMTP client and send message
  30.             using (SmtpClient smtpClient = new SmtpClient())
  31.             {
  32.                 smtpClient.Host = "smtp.gmail.com";
  33.                 smtpClient.EnableSsl = true;
  34.                 smtpClient.Port = 587; // Gmail uses port 587
  35.                 smtpClient.UseDefaultCredentials = false; // Must be set BEFORE Credentials below...
  36.                 smtpClient.Credentials = new NetworkCredential("Gmail username", "Gmail Password");
  37.                 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
  38.                 smtpClient.Send(msg);
  39.             }
  40.         }
  41.         catch (SmtpFailedRecipientsException sfrEx)
  42.         {
  43.             // TODO: Handle exception
  44.             // When email could not be delivered to all receipients.
  45.             throw sfrEx;
  46.         }
  47.         catch (SmtpException sEx)
  48.         {
  49.             // TODO: Handle exception
  50.             // When SMTP Client cannot complete Send operation.
  51.             throw sEx;
  52.         }
  53.         catch (Exception ex)
  54.         {
  55.             // TODO: Handle exception
  56.             // Any exception that may occur during the send process.
  57.             throw ex;
  58.         }
  59.     }
  60. }
End of Code Snippet

Thursday, 22 November 2012

Facebook Developer: Debugger


Not sure how many of you know about this, but check out the Facebook Debugger!

You can enter an Input URL, Access Token, or Open Graph Action ID!

View Here: http://developers.facebook.com/tools/debug

Error when saving a table in SQL Server: "Saving changes is not permitted"


Error Message
Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created.

Why?
This problem occurs when the Prevent saving changes that require the table re-creation option is enabled, and you make one or more of the following changes to the table:
  • You change the Allow Nulls setting for a column.
  • You reorder columns in the table.
  • You change the column data type.
  • You add a new column.
When you change a table so that you alter the metadata structure of the table, and then you save the table, the table must be re-created based on these changes. This may result in the loss of metadata and in a direct loss of data during the re-creation of the table.

Fixing the Problem

OPTION 1 - Using the ALTER SQL keyword to change the table definition.

Code Snippet
  1. ALTER TABLE NameOfTable ALTER COLUMN ColumnToAlter INT NOT NULL
End of Code Snippet

Option 2 - Turning off the "Prevent saving changes that require table re-creation" option. NOT RECOMMENDED!. This may cause data loss due to changes with the meta structure of the table. A good example of potential data loss is if you enable Change Tracking.

If you only have the table structure set up and/or data loss isn't an issue (Test Data for example), then here are the following steps to turn off this data protection feature.

1. Open SQL Server Management Studio (SSMS).
2. On the Tools menu, click Options.
3. In the navigation pane of the Options window, click Designers.
4. Select or clear the Prevent saving changes that require the table re-creation check box, and then click OK.




Joining MP4 Files together without Re-Encoding (Windows and MAC)


OPTION 1 - MP4Join (Cross Platform) [RECOMMENDED!]

VERY simple cross platform solution to join MP4 files. Just add them to the lit, re-order them, and it will simply copy the next video on to the previous at the end of the last frame. It will output the final MP4 without loss of quality or re-encoding.

Download Here: http://www.mp4joiner.org/en


OPTION 2 - Quicktime (7 or X at time of writing). Windows users must use Quicktime PRO ONLY.

1. Open the two H264 videos in Quicktime 7. Let's suppose they are named video1.mp4 and video2.mp4.

2. Working on video1.mp4, Select All, then Copy.

3. Switch to video2.mp4, make sure the "cursor" is at the beginning of the video, then Paste. This inserts the content of video1.mp4 at the front of video2.mp4.

4. This step is probably optional. "Save As" the modified video2.mp4. You will be able to save in .mov format only. I saved as a self-contained movie, but I suppose a reference movie would work too. I used the filename "video.mov".

5. Select "Export" in the file menu to export video.mov to the joined H264 file. The "Export" to select is "Movie to MPEG-4". The preset is not important, I chose "LAN/Intranet". The preset is not important because we override it in the next step.

6. Click on "Options" to reach the settings dialog. In the Video pane, "Video Format" is selected to be "H.264". Click on the menu, select "Pass Through". As expected, all the other options will get disabled.

7. Click OK, then Save, and watch your joined file be saved at the speed of your hard disk.


OPTION 3 - MAC Users Only!

SimpleMovieX, a shareware video editor which exactly advertises this as a feature. Requires OS X 10.6 or later.

Download Here: http://simplemoviex.com/SimpleMovieX/download.html

Thursday, 26 July 2012

iPhone - Free 3G tethering


Applications like MyWi and PDANet are not allowed on the Apple App Store due to getting around user's data network data usage plans (Which networks charge a lot for!)

Check out this app, very simple, lightweight and free!

http://www.junefabrics.com/desktop/

Tuesday, 17 July 2012

JDBC - Calling an Oracle Stored Procedure with CURSOR output


This article explains how to call an Oracle Stored Procedure that returns a Cursor as an output parameter with Java using JDBC.


Things to note:
- JDBC begins it's index's at 1, not 0. I.e. The CallableStatement object's methods such as: getObject must have a parameter with a value of at least 1.
- Once the Connection or CallableStatement objects are closed, the ResultSet will be empty. This is because it only exists as long as the database connection is open. Ensure you take your results before these objects are destroyed.
- oracle.jdbc.OracleCallableStatement and java.sql.CallableStatement offer the same outcome in this example. Therefore, I have opted to use the java.sql library.
- The '?' within the prepareCall parameter is a place holder for a parameter. This is populated (in this example) using the registerOutParameter method, declaring it's expected type.


Oracle Stored Procedure
Code Snippet
  1. CREATE OR REPLACE PROCEDURE PACKAGENAME.GET_ACCOUNTS
  2. (
  3.     l_cursor OUT sys_refcursor
  4. )
  5.     OPEN l_cursor FOR SELECT ACCOUNT_NAME FROM Accounts ORDER BY ACCOUNT_NAME;
  6. END GET_ACCOUNTS;
End of Code Snippet


Java Code
Code Snippet
  1. // Establishes and returns a new connection to the database
  2. private Connection GetConnection() throws SQLException
  3. {
  4.     // Load and register Oracle driver
  5.     DriverManager.registerDriver(new OracleDriver());
  6.    
  7.     // Establish a connection
  8.     return DriverManager.getConnection("jdbc:oracle:thin:@SERVER:PORT:SID", "USERNAME", "PASSWORD");
  9. }
  10.  
  11.  
  12. // Gets a collection of all accounts within the database
  13. public Collection<Account> GetAccounts() throws SQLException
  14. {  
  15.     Connection conn = null;
  16.     CallableStatement cstmt   = null;
  17.     Collection<Account> accounts = new ArrayList<Account>();
  18.  
  19.     try
  20.     {
  21.         // Connect
  22.         conn = this.GetConnection();
  23.  
  24.         // Get ResultSet
  25.         cstmt = conn.prepareCall("{call PACKAGENAME.GET_ACCOUNTS(?)}");   // '?' is a template for our Cursor OUT parameter
  26.         cstmt.registerOutParameter(1, OracleTypes.CURSOR);
  27.         cstmt.execute();
  28.  
  29.         // Index starts at 1, so we want our first result, which is the Cursor
  30.         ResultSet rs = (ResultSet) cstmt.getObject(1);
  31.  
  32.         // process results one row at a time
  33.         while(rs.next())
  34.         {
  35.             Account acc = new Account(rs.getString(1)); // Account name is the first (and only) result in our Cursor.
  36.             accounts.add(acc); // Add account to our collection of accounts to return
  37.         }
  38.     }
  39.     catch (SQLException e)
  40.     {
  41.         System.err.println(e.getErrorCode() + e.getMessage());
  42.     }
  43.         catch(Exception e)
  44.         {
  45.             e.printStackTrace();
  46.         }
  47.     finally
  48.     {
  49.         conn.close();
  50.         cstmt.close();
  51.     }
  52.    
  53.     return accounts; // Return collection of accounts to caller
  54. }
  55.  
  56.  
  57.  
  58. // Account POJO
  59. public class Account
  60. {
  61.     private String accountName;
  62.  
  63.     public Account(String accName)
  64.     {
  65.         this.accountName = accName;
  66.     }
  67.    
  68.     public String getAccountName() {
  69.         return this.accountName;
  70.     }
  71.  
  72.     public void setAccountName(String accountName) {
  73.         this.accountName = accountName;
  74.     }
  75. }
End of Code Snippet

java.lang.IllegalAccessError: oracle/jdbc/driver/OracleCallableStatement


I received this error when attempting to cast the result of a Connection object's prepareCall method.

Solution
Try altering the application to not refer to oracle.jdbc.driver.OracleCallableStatement. It should be oracle.jdbc.OracleCallableStatement.

Reason
For Oracle 9i onwards you should use oracle.jdbc.OracleDriver rather than oracle.jdbc.driver.OracleDriver as Oracle have stated that oracle.jdbc.driver.OracleDriver is deprecated and support for this driver class will be discontinued in the next major release.

Tuesday, 29 May 2012

iPhone - iOS 5.1.1 untethered jailbreak with Absinthe 2.0.2



Summary
After copious amounts of work and many sleepless nights Absinthe 2.0 is finally here to jailbreak your device. This release has been a large collaborative effort between Chronic-Dev Team and iPhone Dev Teams (Jailbreak Dream Team)

This jailbreak supports firmware 5.1.1 ONLY and is again one of the most easiest jailbreaks to use (so easy your grandma could do it ;D)


Press Release
http://conference.hitb.org/hitbsecconf2012ams/ios-jailbreak-dream-team-releases-absinthe-2-0-ios-5-1-1-jailbreak/


Absinthe 2.0 supports the following devices
iPad1,1 – 2,1 – 2,2 – 2,3 – 3,1 – 3,2 – 3,3

iPhone2,1 – 3,1 – 3,3 – 4,1

iPod3,1 – 4,1

(Support for iPad2,4 will be added at a later date)



How To Use Absinthe 2.0 (if you're unjailbroken)
1. Make a backup of your device in iTunes by right clicking on your device name under the ‘Devices’ menu and click ‘Back Up’.

2. Once your backup is complete return to your device and go to Settings – General – Reset – Erase all Content and Settings. This will make the jailbreak process much faster.

3. Open Absinthe and be sure you are still connected via USB cable to your computer.

4. Click ‘Jailbreak’ and wait…. just be patient and do not disconnect your device.

5. Once jailbroken return to iTunes and restore your backup from earlier. Right click on your device name under the ‘Devices’ menu in the left panel of iTunes and click ‘Restore from Back Up…’ then select the latest backup you created before. (restoring this backup will return all the content previously on your device ie, apps, photos, etc.)

6. Thanks for using Absinthe, enjoy your jailbroken iDevice!


How To Untether 5.1.1 (if you're already jailbroken)
Just search for and install "Rocky Racoon 5.1.1 Untether" from Cydia.


Download Links
Absinthe v2.0.2 MacOSX (10.5, 10.6, 10.7)
Absinthe v2.0.2 Windows (XP/Vista/Win7)
Absinthe v2.0.2 Linux (x86/x86_64)


Source: http://greenpois0n.com

iPhone - What are SHSH Blobs and how to downgrade your iOS firmware



Summary
An SHSH Blob is a signature file which is verified against Apple Server to verify the the iPhone is running the latest version of iOS. If for some reason, when you are try to restore to a previous version, Apple will not allow you to restore it becuase, you are trying load a older version of iOS. To make this possible, we need to send a request to a different server (Local or designated) which sends a SHSH blob (which was saved by you) back to iTunes faking that its the current version.


This applies to all apple based devices
iPhone, iPad, iPod Touch etc...


An SHSH Blob CANNOT be shared across multiple devices
I.e. you can't use a friends! This is because the ECID (Exclusive Chip ID - Unique ID for the device) is saved alongside it and must match.


Why and when to save these SHSH Blobs?
You should save these SHSH Blobs everytime your device is updated to a new firmware version. This will allow you to downgrade the firmware for the device regardless of Apple unsigning the firmware or not. So keep a collection, and you can downgrade to whichever you wish.


Why should I downgrade?
- Your generally unhappy with the new firmware
- The new jailbreak has problems, and you would like to revert to a previous firmware where your previous jailbreak was running smoothly.
- The new firmware only has a Tethered jailbreak available.

Teathered vs. Unteathered?
This simply means, weather you need a computer to start your phone. If a release of redsn0w offers a Teathered solution, then !everytime! your phone turns off (out of battery, new app install etc.) you will need to plug it into your computer and use redsn0w to boot the phone! The batteries are pretty decent, so you probably don't even care or it's no big deal.


I don't have my SHSH Blobs for a previous firmware version, but I still want to downgrade...
A utility called "An SHSH Blobs Extractor" is being worked on by iH8sn0w... This allows you to extract blobs for a previous firmware version from the device. Stay tuned though, it's not out yet!


How do I save an SHSH Blob for my device
NOTE: Do you not need a jail broken device to save your SHSH Blob.

You have a number of options here, but the way that works for jailbroken and non-jailbroken devices is by using TinyUmbrella (For Windows and MAC).

TinyUmbrella Download
TinyUmbrella-5.11.01.pkg (MAC)
TinyUmbrella-5.11.01.exe (Win)
View the TinyUmbrella blog to get the latest version here

1. Open TinyUmbrella
2. Click "Start TSS Server"
3. Save SHSH
4. The SHSH files for your device will now be stored on your local system.

NOTE:
If your device is jailbroken, you can store these on the Cydia server. This will allow you to revert back to a previous firmware without worrying about keeping them safe. Also, if you buy an Apple device from ebay, the seller may have already done this, so its good to check Cydia if this is the case!


Downgrading your device with SHSH Blobs
Once you have the SHSH Blobs for the firmware you would like to downgrade to, then you can use iFaith to achieve this! More information here

Monday, 28 May 2012

Shell Script: NSLookup DNS entry to see which server is LIVE


Here is a simple script which allows you to perform an NSLookup on a DNS entry to find out if the current host is Live or not.

This is useful if you have a mirrored backup server that shouldnt process any data unless its currently live. If a failover occurs, the backup server will then realise it's live and process the information which the other server was currently processing.


Code Snippet
  1. #!/bin/sh
  2. ################################################################################
  3. # Does an NSLookup on a server DNS entry to determine which server is currently LIVE.
  4. ################################################################################
  5. #
  6. # Get LIVE server and query current server
  7. liveServer=`eval nslookup mgs | grep "Name:"`
  8. currentServer=`eval hostname`
  9.  
  10.  
  11. # Nslookup to see if this server is LIVE
  12. echo "$liveServer" | grep $currentServer >/dev/null 2>&1
  13.  
  14. if [ "$?" -eq "0" ]; then
  15.     echo "This IS the Live server"
  16. else
  17.     echo "This IS NOT the Live server"
  18. fi
  19.  
  20.  
  21.  
  22. exit 0
End of Code Snippet