<HTML>
<HEAD><TITLE>Check Your Mail</TITLE></HEAD>
<BODY>

<!--- Simple CSS-based formatting styles --->
<STYLE>
  BODY {font-family:sans-serif;font-size:12px}
  TH   {font-size:12px;background:navy;color:white}
  TD   {font-size:12px;background:lightgrey;color:navy}
</STYLE>
<H2>Check Your Mail</H2>


<!--- If user is logging out --->  
<CFIF IsDefined("URL.Logout")>
  <CFSET StructDelete(SESSION, "Mail")>
</CFIF>

<!--- If we don't have a username/password --->
<CFIF NOT IsDefined("SESSION.Mail")>
  <!--- Show "mail server login" form --->
  <CFINCLUDE TEMPLATE="CheckMailLogin.cfm">
</CFIF>


<!--- If we need to contact server for list of messages --->
<!--- (if just logged in, or if clicked "Refresh" link) --->
<CFIF NOT IsDefined("SESSION.Mail.GetMessages") OR IsDefined("URL.Refresh")>
  <!--- Flush page output buffer --->
  <CFFLUSH>

  <CFTRY>
    <!--- Contact POP Server and retieve messages --->
    <CFPOP
      ACTION="GetHeaderOnly"
      NAME="SESSION.Mail.GetMessages"
      SERVER="#SESSION.Mail.POPServer#"
      USERNAME="#SESSION.Mail.Username#"
      PASSWORD="#SESSION.Mail.Password#"
      MAXROWS="50">

      
    <!--- If the username/password were rejected by mail server --->  
    <CFCATCH TYPE="COM.Allaire.ColdFusion.POPAuthFailure">
      <!--- Discard login information from SESSION scope --->
      <!--- This will force user to re-provide credentials --->
      <CFSET StructDelete(SESSION, "Mail")>
      <!--- Explain what happened, with link to try again --->
      <CFOUTPUT>
        <P>The username and password you provided were not accepted.<BR>
        <P><A HREF="#CGI.SCRIPT_NAME#">Click Here To Try Again</A><BR>
        <CFABORT>
      </CFOUTPUT>
    </CFCATCH>  
    
    <!--- If the connection to the mail server failed --->  
    <CFCATCH TYPE="COM.Allaire.ColdFusion.POPConnectionFailure">
      <!--- Discard login information from SESSION scope --->
      <!--- This will force user to re-provide credentials --->
      <CFSET StructDelete(SESSION, "Mail")>
      <!--- Explain what happened, with link to try again --->
      <CFOUTPUT>
        <P>The mail server you specified could not be reached.<BR>
        <P><A HREF="#CGI.SCRIPT_NAME#">Click Here To Try Again</A><BR>
        <CFABORT>
      </CFOUTPUT>
    </CFCATCH>  

  </CFTRY>
</CFIF>  

  
<!--- If no messages were retrieved... --->
<CFIF SESSION.Mail.GetMessages.RecordCount EQ 0>
  <P>You have no mail messages at this time.<BR>
  
<!--- If messages were retrieved... --->  
<CFELSE>
  <!--- Display Messages in HTML Table Format --->  
  <TABLE BORDER="0" CELLSPACING="2" CELLSPACING="2" COLS="3" WIDTH="550">
    <!--- Column Headings for Table --->
    <TR>
      <TH WIDTH="100">Date Sent</TH>
      <TH WIDTH="200">From</TH>
      <TH WIDTH="200">Subject</TH>
    </TR>
    <!--- Display info about each message in a table row --->
    <CFOUTPUT QUERY="SESSION.Mail.GetMessages">
      <CFTRY>
        <!--- Parse Date from the "date" mail header --->
        <CFSET d = ParseDateTime(Date, "POP")>
        <CFSET MsgDate = "<B>#DateFormat(d)#</B><BR>#TimeFormat(d)#">
        <!--- If the date can't be parsed, just use raw date header --->
        <CFCATCH TYPE="Expression">
          <CFIF CFCATCH.ErrNumber EQ 16394>
            <CFSET MsgDate = Date>
          <CFELSE>
            <CFRETHROW>  
          </CFIF>
        </CFCATCH>
      </CFTRY>
      <!--- Let user click on Subject to read full message --->
      <CFSET LinkURL = "CheckMailMsg2.cfm?MsgNum=#MessageNumber#">

      <TR VALIGN="baseline">
        <!--- Show parsed Date and Time for message--->
        <TD>
          #MsgDate#
        </TD>
        <!--- Show "From" address, escaping brackets --->
        <TD>#HTMLEditFormat(From)#</TD>
        <TD><STRONG><A HREF="#LinkURL#">#Subject#</A></STRONG></TD>
      </TR>  
    </CFOUTPUT>
  </TABLE>
  
  <!--- "Refresh" link to get new list of messages  --->
  <STRONG><A HREF="CheckMail.cfm?Refresh=Yes">Refresh Message List</A></STRONG><BR>
  <!--- "Log Out" link to discard SESSION.Mail info --->
  <A HREF="CheckMail.cfm?Logout=Yes">Log Out</A><BR>
</CFIF>


</BODY>
</HTML>