protected void Page_Command(object sender, CommandEventArgs e) { try { sbTrace = new StringBuilder(); if ( e.CommandName == "Mailbox.CheckBounce" ) { EmailUtils.CheckInbound(Application, gID, true); } if ( e.CommandName == "Mailbox.CheckMail" || e.CommandName == "Mailbox.CheckBounce" ) { string sSERVER_URL = Sql.ToString (ViewState["SERVER_URL" ]); string sEMAIL_USER = Sql.ToString (ViewState["EMAIL_USER" ]); string sEMAIL_PASSWORD = Sql.ToString (ViewState["EMAIL_PASSWORD"]); int nPORT = Sql.ToInteger(ViewState["PORT" ]); string sSERVICE = Sql.ToString (ViewState["SERVICE" ]); bool bMAILBOX_SSL = Sql.ToBoolean(ViewState["MAILBOX_SSL" ]); // 01/08/2008 Paul. Decrypt at the last minute to ensure that an unencrypted password is never sent to the browser. Guid gINBOUND_EMAIL_KEY = Sql.ToGuid(Application["CONFIG.InboundEmailKey"]); Guid gINBOUND_EMAIL_IV = Sql.ToGuid(Application["CONFIG.InboundEmailIV" ]); sEMAIL_PASSWORD = Security.DecryptPassword(sEMAIL_PASSWORD, gINBOUND_EMAIL_KEY, gINBOUND_EMAIL_IV); dtMain = new DataTable(); dtMain.Columns.Add("From" , typeof(System.String )); dtMain.Columns.Add("Sender" , typeof(System.String )); dtMain.Columns.Add("ReplyTo" , typeof(System.String )); dtMain.Columns.Add("To" , typeof(System.String )); dtMain.Columns.Add("CC" , typeof(System.String )); dtMain.Columns.Add("Bcc" , typeof(System.String )); dtMain.Columns.Add("Subject" , typeof(System.String )); dtMain.Columns.Add("DeliveryDate", typeof(System.DateTime)); dtMain.Columns.Add("Priority" , typeof(System.String )); dtMain.Columns.Add("Size" , typeof(System.Int32 )); dtMain.Columns.Add("ContentID" , typeof(System.String )); dtMain.Columns.Add("MessageID" , typeof(System.String )); dtMain.Columns.Add("Headers" , typeof(System.String )); dtMain.Columns.Add("Body" , typeof(System.String )); Pop3.Pop3MimeClient pop = new Pop3.Pop3MimeClient(sSERVER_URL, nPORT, bMAILBOX_SSL, sEMAIL_USER, sEMAIL_PASSWORD); try { pop.Trace += new Pop3.TraceHandler(this.Pop3Trace); pop.ReadTimeout = 60 * 1000; //give pop server 60 seconds to answer pop.Connect(); int nTotalEmails = 0; int mailboxSize = 0; pop.GetMailboxStats(out nTotalEmails, out mailboxSize); List<int> arrEmailIds = new List<int>(); pop.GetEmailIdList(out arrEmailIds); foreach ( int i in arrEmailIds ) { int nEmailSize = pop.GetEmailSize(i); if ( nEmailSize < 1 * 1024 * 1024 ) { Pop3.RxMailMessage mm = null; #if DEBUG pop.IsCollectRawEmail = true; #endif pop.GetHeaders(i, out mm); if ( mm == null ) { sbTrace.Append("Email " + i.ToString() + " cannot be displayed." + ControlChars.CrLf); } else { DataRow row = dtMain.NewRow(); dtMain.Rows.Add(row); row["From" ] = Server.HtmlEncode(Sql.ToString(mm.From )); row["Sender" ] = Server.HtmlEncode(Sql.ToString(mm.Sender )); row["ReplyTo" ] = Server.HtmlEncode(Sql.ToString(mm.ReplyTo)); row["To" ] = Server.HtmlEncode(Sql.ToString(mm.To )); row["CC" ] = Server.HtmlEncode(Sql.ToString(mm.CC )); row["Bcc" ] = Server.HtmlEncode(Sql.ToString(mm.Bcc )); row["Subject" ] = Server.HtmlEncode(mm.Subject); // 01/23/2008 Paul. DateTime in the email is in universal time. row["DeliveryDate"] = T10n.FromUniversalTime(mm.DeliveryDate); row["Priority" ] = mm.Priority.ToString(); row["Size" ] = nEmailSize ; row["ContentId" ] = mm.ContentId ; row["MessageId" ] = mm.MessageId ; row["Headers" ] = "<pre>" + Server.HtmlEncode(mm.RawContent) + "</pre>"; //row["Body" ] = mm.Body ; } } } } catch(Exception ex) { SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex); lblError.Text = ex.Message; } finally { pop.Disconnect(); } ViewState["Inbox"] = dtMain; vwMain = new DataView(dtMain); grdMain.DataSource = vwMain; grdMain.DataBind(); } } catch(Exception ex) { SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex); lblError.Text = ex.Message; } finally { #if DEBUG RegisterClientScriptBlock("Pop3Trace", "<script type=\"text/javascript\">sDebugSQL += '" + Sql.EscapeJavaScript(sbTrace.ToString()) + "';</script>"); #endif } }
static void Main(string[] args) { // TODO: Remove the comment signs from the next line, if you want to create some sample emails. // SendTestmail(); //If you get a run time error her: SmtpFailedRecipientException, 'Mailbox unavailable. The server response was: 5.7.1 Unable to relay for', //then you need to change the settings of the local IIS/SMTP server. Console.WriteLine("POP3 MIME Client Demo"); Console.WriteLine("====================="); Console.WriteLine(); //prepare pop client // TODO: Replace server address, username and password with your own credentials. Pop3.Pop3MimeClient DemoClient = new Pop3.Pop3MimeClient("pop.gmail.com", 995, true, "*****@*****.**", "password"); DemoClient.Trace += new TraceHandler(Console.WriteLine); DemoClient.ReadTimeout = 60000; //give pop server 60 seconds to answer //establish connection DemoClient.Connect(); //get mailbox stats int numberOfMailsInMailbox, mailboxSize; DemoClient.GetMailboxStats(out numberOfMailsInMailbox, out mailboxSize); //get at most the xx first emails RxMailMessage mm; int downloadNumberOfEmails; int maxDownloadEmails = 99; if (numberOfMailsInMailbox<maxDownloadEmails) { downloadNumberOfEmails = numberOfMailsInMailbox; } else { downloadNumberOfEmails = maxDownloadEmails; } for (int i = 1;i <= downloadNumberOfEmails;i++) { DemoClient.GetEmail(i, out mm); if (mm==null) { Console.WriteLine("Email " + i.ToString() + " cannot be displayed."); } else { Console.WriteLine(mm.MailStructure()); } } ////uncomment the following code if you want to write the raw text of the emails to a file. //string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //string Email; //DemoClient.IsCollectRawEmail = true; //string fileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Emails From POP3 Server.TXT"; //using (StreamWriter sw = File.CreateText(fileName)) { // for (int i = 1;i <= maxDownloadEmails;i++) { // sw.WriteLine("Email: " + i.ToString() + "\n===============\n\n"); // DemoClient.GetRawEmail(i, out Email); // sw.WriteLine(Email); // sw.WriteLine(); // } // sw.Close(); //} //close connection DemoClient.Disconnect(); Console.WriteLine(); Console.WriteLine("======== Press Enter to end program"); Console.ReadLine(); }
protected void Page_Command(object sender, CommandEventArgs e) { try { sbTrace = new StringBuilder(); if (e.CommandName == "Mailbox.CheckBounce") { EmailUtils.CheckInbound(Application, gID, true); } if (e.CommandName == "Mailbox.CheckMail" || e.CommandName == "Mailbox.CheckBounce") { string sSERVER_URL = Sql.ToString(ViewState["SERVER_URL"]); string sEMAIL_USER = Sql.ToString(ViewState["EMAIL_USER"]); string sEMAIL_PASSWORD = Sql.ToString(ViewState["EMAIL_PASSWORD"]); int nPORT = Sql.ToInteger(ViewState["PORT"]); string sSERVICE = Sql.ToString(ViewState["SERVICE"]); bool bMAILBOX_SSL = Sql.ToBoolean(ViewState["MAILBOX_SSL"]); // 01/08/2008 Paul. Decrypt at the last minute to ensure that an unencrypted password is never sent to the browser. Guid gINBOUND_EMAIL_KEY = Sql.ToGuid(Application["CONFIG.InboundEmailKey"]); Guid gINBOUND_EMAIL_IV = Sql.ToGuid(Application["CONFIG.InboundEmailIV"]); sEMAIL_PASSWORD = Security.DecryptPassword(sEMAIL_PASSWORD, gINBOUND_EMAIL_KEY, gINBOUND_EMAIL_IV); dtMain = new DataTable(); dtMain.Columns.Add("From", typeof(System.String)); dtMain.Columns.Add("Sender", typeof(System.String)); dtMain.Columns.Add("ReplyTo", typeof(System.String)); dtMain.Columns.Add("To", typeof(System.String)); dtMain.Columns.Add("CC", typeof(System.String)); dtMain.Columns.Add("Bcc", typeof(System.String)); dtMain.Columns.Add("Subject", typeof(System.String)); dtMain.Columns.Add("DeliveryDate", typeof(System.DateTime)); dtMain.Columns.Add("Priority", typeof(System.String)); dtMain.Columns.Add("Size", typeof(System.Int32)); dtMain.Columns.Add("ContentID", typeof(System.String)); dtMain.Columns.Add("MessageID", typeof(System.String)); dtMain.Columns.Add("Headers", typeof(System.String)); dtMain.Columns.Add("Body", typeof(System.String)); Pop3.Pop3MimeClient pop = new Pop3.Pop3MimeClient(sSERVER_URL, nPORT, bMAILBOX_SSL, sEMAIL_USER, sEMAIL_PASSWORD); try { pop.Trace += new Pop3.TraceHandler(this.Pop3Trace); pop.ReadTimeout = 60 * 1000; //give pop server 60 seconds to answer pop.Connect(); int nTotalEmails = 0; int mailboxSize = 0; pop.GetMailboxStats(out nTotalEmails, out mailboxSize); List <int> arrEmailIds = new List <int>(); pop.GetEmailIdList(out arrEmailIds); foreach (int i in arrEmailIds) { int nEmailSize = pop.GetEmailSize(i); if (nEmailSize < 1 * 1024 * 1024) { Pop3.RxMailMessage mm = null; #if DEBUG pop.IsCollectRawEmail = true; #endif pop.GetHeaders(i, out mm); if (mm == null) { sbTrace.Append("Email " + i.ToString() + " cannot be displayed." + ControlChars.CrLf); } else { DataRow row = dtMain.NewRow(); dtMain.Rows.Add(row); row["From"] = Server.HtmlEncode(Sql.ToString(mm.From)); row["Sender"] = Server.HtmlEncode(Sql.ToString(mm.Sender)); row["ReplyTo"] = Server.HtmlEncode(Sql.ToString(mm.ReplyTo)); row["To"] = Server.HtmlEncode(Sql.ToString(mm.To)); row["CC"] = Server.HtmlEncode(Sql.ToString(mm.CC)); row["Bcc"] = Server.HtmlEncode(Sql.ToString(mm.Bcc)); row["Subject"] = Server.HtmlEncode(mm.Subject); // 01/23/2008 Paul. DateTime in the email is in universal time. row["DeliveryDate"] = T10n.FromUniversalTime(mm.DeliveryDate); row["Priority"] = mm.Priority.ToString(); row["Size"] = nEmailSize; row["ContentId"] = mm.ContentId; row["MessageId"] = mm.MessageId; row["Headers"] = "<pre>" + Server.HtmlEncode(mm.RawContent) + "</pre>"; //row["Body" ] = mm.Body ; } } } } catch (Exception ex) { SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex); lblError.Text = ex.Message; } finally { pop.Disconnect(); } ViewState["Inbox"] = dtMain; vwMain = new DataView(dtMain); grdMain.DataSource = vwMain; grdMain.DataBind(); } } catch (Exception ex) { SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex); lblError.Text = ex.Message; } finally { #if DEBUG RegisterClientScriptBlock("Pop3Trace", "<script type=\"text/javascript\">sDebugSQL += '" + Sql.EscapeJavaScript(sbTrace.ToString()) + "';</script>"); #endif } }
static void Main(string[] args) { // TODO: Remove the comment signs from the next line, if you want to create some sample emails. // SendTestmail(); //If you get a run time error her: SmtpFailedRecipientException, 'Mailbox unavailable. The server response was: 5.7.1 Unable to relay for', //then you need to change the settings of the local IIS/SMTP server. Console.WriteLine("POP3 MIME Client Demo"); Console.WriteLine("====================="); Console.WriteLine(); //prepare pop client // TODO: Replace server address, username and password with your own credentials. Pop3.Pop3MimeClient DemoClient = new Pop3.Pop3MimeClient("pop.gmail.com", 995, true, "*****@*****.**", "password"); DemoClient.Trace += new TraceHandler(Console.WriteLine); DemoClient.ReadTimeout = 60000; //give pop server 60 seconds to answer //establish connection DemoClient.Connect(); //get mailbox stats int numberOfMailsInMailbox, mailboxSize; DemoClient.GetMailboxStats(out numberOfMailsInMailbox, out mailboxSize); //get at most the xx first emails RxMailMessage mm; int downloadNumberOfEmails; int maxDownloadEmails = 99; if (numberOfMailsInMailbox < maxDownloadEmails) { downloadNumberOfEmails = numberOfMailsInMailbox; } else { downloadNumberOfEmails = maxDownloadEmails; } for (int i = 1; i <= downloadNumberOfEmails; i++) { DemoClient.GetEmail(i, out mm); if (mm == null) { Console.WriteLine("Email " + i.ToString() + " cannot be displayed."); } else { Console.WriteLine(mm.MailStructure()); } } ////uncomment the following code if you want to write the raw text of the emails to a file. //string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //string Email; //DemoClient.IsCollectRawEmail = true; //string fileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Emails From POP3 Server.TXT"; //using (StreamWriter sw = File.CreateText(fileName)) { // for (int i = 1;i <= maxDownloadEmails;i++) { // sw.WriteLine("Email: " + i.ToString() + "\n===============\n\n"); // DemoClient.GetRawEmail(i, out Email); // sw.WriteLine(Email); // sw.WriteLine(); // } // sw.Close(); //} //close connection DemoClient.Disconnect(); Console.WriteLine(); Console.WriteLine("======== Press Enter to end program"); Console.ReadLine(); }
public static void CheckInbound(HttpApplicationState Application, Guid gID, bool bBounce) { if ( !bInsideCheckInbound ) { bInsideCheckInbound = true; try { bool bEMAIL_INBOUND_SAVE_RAW = Sql.ToBoolean(Application["CONFIG.email_inbound_save_raw"]); Guid gINBOUND_EMAIL_KEY = Sql.ToGuid (Application["CONFIG.InboundEmailKey" ]); Guid gINBOUND_EMAIL_IV = Sql.ToGuid (Application["CONFIG.InboundEmailIV" ]); DataView vwINBOUND_EMAILS_Inbound = null; if ( bBounce ) vwINBOUND_EMAILS_Inbound = new DataView(SplendidCache.InboundEmailBounce()); else vwINBOUND_EMAILS_Inbound = new DataView(SplendidCache.InboundEmailMonitored()); if ( !Sql.IsEmptyGuid(gID) ) vwINBOUND_EMAILS_Inbound.RowFilter = "ID = '" + gID.ToString() + "'"; DbProviderFactory dbf = DbProviderFactories.GetFactory(Application); using ( IDbConnection con = dbf.CreateConnection() ) { con.Open(); string sSQL; sSQL = "select count(*) " + ControlChars.CrLf + " from vwEMAILS " + ControlChars.CrLf + " where MESSAGE_ID = @MESSAGE_ID" + ControlChars.CrLf; using ( IDbCommand cmdExistingEmails = con.CreateCommand() ) { cmdExistingEmails.CommandText = sSQL; IDbDataParameter parMESSAGE_ID = Sql.AddParameter(cmdExistingEmails, "@MESSAGE_ID", String.Empty, 1000); foreach ( DataRowView rowInbound in vwINBOUND_EMAILS_Inbound ) { // 01/13/2008 Paul. The MAILBOX_ID is the ID for the INBOUND_EMAIL record. Guid gMAILBOX_ID = Sql.ToGuid (rowInbound["ID" ]); Guid gGROUP_ID = Sql.ToGuid (rowInbound["GROUP_ID" ]); string sMAILBOX_TYPE = Sql.ToString (rowInbound["MAILBOX_TYPE" ]); string sSERVER_URL = Sql.ToString (rowInbound["SERVER_URL" ]); string sEMAIL_USER = Sql.ToString (rowInbound["EMAIL_USER" ]); string sEMAIL_PASSWORD = Sql.ToString (rowInbound["EMAIL_PASSWORD"]); int nPORT = Sql.ToInteger(rowInbound["PORT" ]); string sSERVICE = Sql.ToString (rowInbound["SERVICE" ]); bool bMAILBOX_SSL = Sql.ToBoolean(rowInbound["MAILBOX_SSL" ]); bool bMARK_READ = Sql.ToBoolean(rowInbound["MARK_READ" ]); bool bONLY_SINCE = Sql.ToBoolean(rowInbound["ONLY_SINCE" ]); // 01/08/2008 Paul. Decrypt at the last minute to ensure that an unencrypted password is never sent to the browser. sEMAIL_PASSWORD = Security.DecryptPassword(sEMAIL_PASSWORD, gINBOUND_EMAIL_KEY, gINBOUND_EMAIL_IV); Pop3.Pop3MimeClient pop = new Pop3.Pop3MimeClient(sSERVER_URL, nPORT, bMAILBOX_SSL, sEMAIL_USER, sEMAIL_PASSWORD); try { pop.ReadTimeout = 60 * 1000; //give pop server 60 seconds to answer pop.Connect(); int nTotalEmails = 0; int mailboxSize = 0; pop.GetMailboxStats(out nTotalEmails, out mailboxSize); List<int> arrEmailIds = new List<int>(); pop.GetEmailIdList(out arrEmailIds); foreach ( int i in arrEmailIds ) { Pop3.RxMailMessage mm = null; try { pop.IsCollectRawEmail = bEMAIL_INBOUND_SAVE_RAW; pop.GetHeaders(i, out mm); if ( mm != null ) { // 01/13/2008 Paul. Bounce processing only applies if sent by the mailer daemon. // 01/13/2008 Paul. MS Exchange Server uses postmaster. bool bMailerDaemon = mm.From.Address.IndexOf("mailer-daemon@") >= 0 || mm.From.Address.IndexOf("postmaster@") >= 0; if ( (bBounce && bMailerDaemon) || (!bBounce && !bMailerDaemon) ) { // 01/12/2008 Paul. Lookup the message to see if we need to import it. // SugarCRM: The uniqueness of a given email message is determined by a concatenationof 2 values, // SugarCRM: the messageID and the delivered-to field. This allows multiple To: and B/CC: destination // SugarCRM: addresses to be imported by Sugar without violating the true duplicate-email issues. // 01/20/2008 Paul. mm.DeliveredTo can be NULL. parMESSAGE_ID.Value = mm.MessageId + ((mm.DeliveredTo != null && mm.DeliveredTo.Address != null) ? mm.DeliveredTo.Address : String.Empty); if ( Sql.ToInteger(cmdExistingEmails.ExecuteScalar()) == 0 ) { mm = null; pop.GetEmail(i, out mm); // 01/13/2008 Paul. Pull POP3 logic out of import function so that it can be reused by IMAP4 driver. ImportInboundEmail(Application, con, mm, gMAILBOX_ID, sMAILBOX_TYPE, gGROUP_ID); if ( !bMARK_READ ) pop.DeleteEmail(i); } } } } finally { // 01/13/2008 Paul. We may need to be more efficient about garbage cleanup as an email can contain a large attachment. mm = null; } } } finally { pop.Disconnect(); } } } } } catch(Exception ex) { SplendidError.SystemMessage(Application, "Error", new StackTrace(true).GetFrame(0), Utils.ExpandException(ex)); } finally { bInsideCheckInbound = false; } } }