PowerShell Cookbook

Twitter Updates

    follow me on Twitter

    Search

    Categories

     

    On this page

    Removing Deleted Items Left by the iPhone

    Archive

    Blogroll

    Disclaimer
    I work for Microsoft.

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    RSS 2.0 | Atom 1.0 | CDF

    Send mail to the author(s) E-mail

    Total Posts: 252
    This Year: 3
    This Month: 0
    This Week: 0
    Comments: 736

    Sign In

     Tuesday, October 27, 2009
    Wednesday, October 28, 2009 3:28:01 AM (Pacific Daylight Time, UTC-07:00) ( )

    One thing you might notice if you have an iPhone connecting to an Inbox via the IMAP protocol is that messages you delete tend to stick around when viewed from other devices (such as Outlook, Outlook Web Access, etc.)

    This is caused by an out-of-date view of mail management, where your Inbox handles everything. When you delete an item, some IMAP clients (such as the iPhone) mark them as deleted, but don’t actually remove the item from the server. Some clients hide these deleted items. Some show them with a line through them. Some ignore the deleted flag altogether.

    Most email clients (including iPhone) move deleted items to their own sub-folder, so marking items as deleted just ends up being an annoyance:

    http://discussions.apple.com/thread.jspa?messageID=4934598
    http://blogs.freshlogicstudios.com/Posts/View.aspx?Id=44a01293-3b32-4ee0-b23c-fac99348e1cd
    http://www.robichaux.net/blog/2007/07/iphone_vs_windows_mobile_part_3_mail.php

    The solution is the IMAP ‘EXPUNGE’ command. It permanently deletes items that have been marked for deletion (while of course leaving everything in Deleted Items untouched.) The iPhone has an option to do this once a day, but it doesn’t seem to work very well (and a day lag feels like an eternity, anyways.)

    The folks at freshlogic have a fine application to run the command against your mail server. We can improve on it in a few ways by writing it in PowerShell. Our version:

    • Enables support for securely cached credentials
    • Doesn’t require an always-running application
    • Runs in the background

    To do this, our script imports our saved password, and then uses Send-TcpRequest to simply connect to the server and send the EXPUNGE command. Finally, we use schtasks.exe to automate it.

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    # 1) Get your password into a securestring:
    # $ss = Read-Host -AsSecureString
    # 2) Export it to disk
    # $ss | ConvertFrom-SecureString |
    # Out-File (Join-Path (Split-Path $profile) Sync-MailboxDeletedState.ps1.credential)
    # 3) Create the scheduled task
    # schtasks /Create /TN MailboxUpdater /SC MINUTE /MO 5 /TR
    # "powershell -NoProfile -WindowStyle Hidden -File 'c:\path\to\Sync-MailboxDeletedState.ps1'"

    $username = "user@example.com"
    $server = "imap.example.com"
    $port = 993

    $passwordPath = Join-Path (Split-Path $profile) Sync-MailboxDeletedState.ps1.credential
    $password = Get-Content $passwordPath | ConvertTo-SecureString

    $cred = New-Object System.Management.Automation.PsCredential $username,$password

    $nc = $cred.GetNetworkCredential()
    $commands = "A1 LOGIN $($nc.UserName + '@' + $nc.Domain) $($nc.Password)",
                'A2 SELECT INBOX',
                'A3 EXPUNGE',
                'A4 LOGOUT'

    $commands | Send-TcpRequest $server $port -UseSSL
    Comments [0] | | #