troubleshooting Question

Problom sending E-mail with my windows application.

Avatar of Adnan
AdnanFlag for Norway asked on
.NET ProgrammingC#Java EE
21 Comments1 Solution324 ViewsLast Modified:
*I can not switch to my application while i execute e-mail to send, i have to close the window or send the mail and then i can swith to my application.

Do any one njo how to fix this problom?
private void SendTransactionByEmail(Operator myOper)
        {
                try
                {
                   string operatorEmail= myOper.Email;
                    Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
                    Microsoft.Office.Interop.Outlook.MailItem oMsg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
                    Microsoft.Office.Interop.Outlook.Recipient oRecip;
 
                    if (operatorEmail.Length > 0)
                    {
                        oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oMsg.Recipients.Add(operatorEmail.ToString());
                    }
                    else
                    {
                        oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oMsg.Recipients.Add("<adress@adress.com>");
                    }
 
                    //oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oMsg.Recipients.Add("<adress@adress.com>");
                    oRecip.Resolve();
                    oMsg.Subject = "AMA Send Transactions";
                    //oMsg.Attachments.Add(ExcelTransactions());
                    oMsg.BodyFormat = OlBodyFormat.olFormatHTML;
 
                    myRelationRows = AMEngine.AmEngine.Instance.GetRelation(myOper);
 
                    DsOperatorAccess.ClientRow[] clientrows = AMEngine.AmEngine.Instance.GetClients(myOper);
                    DsOperatorAccess.ClientRow clie;
 
                    StringBuilder sHtml = new StringBuilder();
 
                    sHtml.Append("<HTML>");
                    sHtml.Append("<HEAD>");
                    sHtml.Append("<TITLE style=font-family:arial;font-size:10pt>AMA Transactions</TITLE>");
                    sHtml.Append("</HEAD>");
                    sHtml.Append("<BODY bgcolor=#FEFEFE>");
                    sHtml.Append("<h2><P style=font-family:arial;font-size:15pt><Font Color=#0098c1>" + GetValue("transactionssentfromadramatchaccounts") + "</Font></h2></P>\n");
 
                    foreach (DsOperatorAccess.ClientRow cRow in clientrows)
                    {
                        if (cRow.Client_ID == myOper.CurrentClient_ID)
                        {
                            clie = cRow;
                            sHtml.Append("<p style=font-family:arial;font-size:11pt><b>" + GetValue("client") + ":" + "</b>" + " " + "</b>" + " " + clie.ClientName + "</p>");
                        }
                    }
 
                    sHtml.Append("<p style=font-family:arial;font-size:11pt><b>" + GetValue("accountgroup") + "</b>" + " " + myRelationRows.Name + "</p>");
                    sHtml.Append("<table cellpadding=0 cellspacing=0 style=width: 357px border=1 >");
                    //relationSelector1.Operator = myOper;
 
                    sHtml.Append("<tr font-size= 10 font-family= Arial>");
 
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("vdate") + "</th>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("bdate") + "</th>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("reference") + "</th>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 80 style=font-family:arial;font-size:10pt>" + GetValue("amount") + "</th>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("text") + "</th>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("accountno") + "</th>");
                    
                    DsReconItems.ExtraColumnNameDataTable ecTable = AmEngine.Instance.GetExtraFields(myOper);
                    //Dictionary<String, int> columnPosition = new Dictionary<string, int>();
                    foreach (DsReconItems.ExtraColumnNameRow nameRow in ecTable.Rows)
                    {
                        //if (nameRow.Name == "Comment")
 
                        //{
                        //    //columnPosition.Add(nameRow.Name, 6);
                        //}
                        //else
                        //{
                            sHtml.AppendLine("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + nameRow.Name + "</th>");
                        //}
 
                    }
                    sHtml.Append("</tr>");
 
                    int strTemp = (int)ReconManuallyGridView.Rows[i].Cells["Type"].Value;
                    decimal totalamount = 0;
                    int transactionLimit = 101;
                    int countTransactionsChecked = checkedMatchItems.Count;
                    if (countTransactionsChecked >= transactionLimit)
                    {
                        DialogResult cannotsend = MessageBox.Show(GetValue("cannotsendtransactionsonemail"), GetValue("info"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                        switch (cannotsend)
                        {
                            case DialogResult.OK:
                                {
                                    return;
                                }
                        }
 
                    }
                    foreach (Guid matchItem_ID in checkedMatchItems)
                    {
 
                        DsReconItems.MatchItemRow currentAddMIRow = dsReconItems1.MatchItem.FindByMatchItem_ID(matchItem_ID);
                        decimal amnt = (decimal)currentAddMIRow.Amount;
                        totalamount += currentAddMIRow.Amount;
                        if (currentAddMIRow != null)
                        {
 
                            if (currentAddMIRow.Type >= 128)
                            {
                                sHtml.Append("<tr bgcolor=\"#" + colorRight.Name.Substring(2, 6) + "\">");
                                sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.VDate.ToShortDateString().Trim() + "</td>");
                                sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.Bdate.ToShortDateString().Trim() + "</td>");
                                sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.Ref.ToString().Trim() + "</td>");
                                //sHtml.Append("<td >" + currentAddMIRow.Amount.ToString() + "</td>");
                                if (amnt < 0)
                                {
                                    //ReconManuallyGridView.Rows[i].Cells["Amount"].Style.ForeColor = colorAmountCredit;
                                    sHtml.Append("<td align=right style=font-family:arial;font-size:10pt><font color=\"#" + colorAmountCredit.Name.Substring(2, 6) + "\">" + currentAddMIRow.Amount.ToString().Trim() + "</td>");
                                }
                                else
                                {
                                    sHtml.Append("<td align=right style=font-family:arial;font-size:10pt><font color=#000000>" + currentAddMIRow.Amount.ToString().Trim() + "</td>");
                                }
 
                                // Text felt
                                //String text = currentAddMIRow.AllText.ToString();
                                if (currentAddMIRow["AllText"] != DBNull.Value)
                                {
                                    sHtml.AppendLine("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.AllText.ToString().Trim() + "</td>");
                                }
                                else
                                {
                                    sHtml.AppendLine("<td style=font-family:arial;font-size:10pt></td>");
                                }
                                
                                //AccountNo
                                if (currentAddMIRow.AccountNo.Length <= 0)
                                {
                                    sHtml.Append("<td style=font-family:arial;font-size:10pt></td>");
                                }
                                else
                                {
                                    sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.AccountNo.Trim() + "</td>");
                                }
 
                                DsReconItems.ExtraRow[] extraRows = (DsReconItems.ExtraRow[])currentAddMIRow.GetExtraRows();
                                foreach (DsReconItems.ExtraColumnNameRow eRow in ecTable)
                                {
                                    DsReconItems.ExtraRow exTempRow = null;
                                    foreach (DsReconItems.ExtraRow exRow in extraRows)
                                    {
                                        //DsReconItems.ExtraColumnNameRow[] dr = (DsReconItems.ExtraColumnNameRow[])ecTable.Select("Type_ID = " + exRow.Type_ID);
 
                                        //sHtml.AppendLine("<td style=font-family:arial;font-size:10pt>" + exRow.Content.Trim() + "</td>");
                                        if (eRow.Type_ID == exRow.Type_ID)
                                        {
                                            exTempRow = exRow;
                                            break;
                                        }
                                    }
 
                                    if (exTempRow == null)
                                    {
                                        sHtml.Append("<td style=font-family:arial;font-size:10pt></td>");
                                    }
                                    else
                                    {
                                        sHtml.AppendLine("<td style=font-family:arial;font-size:10pt>" + exTempRow.Content.Trim() + "</td>");
                                    }
 
                                }
                               
                            }
                            else
                            {
                                sHtml.Append("<tr bgcolor=\"#" + colorLeft.Name.Substring(2, 6) + "\">");
                                sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.VDate.ToShortDateString().Trim() + "</td>");
                                sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.Bdate.ToShortDateString().Trim() + "</td>");
                                sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.Ref.ToString().Trim() + "</td>");
                                //sHtml.Append("<td >" + currentAddMIRow.Amount.ToString() + "</td>");
                                if (amnt < 0)
                                {
                                    //ReconManuallyGridView.Rows[i].Cells["Amount"].Style.ForeColor = colorAmountCredit;
                                    sHtml.Append("<td align=right style=font-family:arial;font-size:10pt><font color=\"#" + colorAmountCredit.Name.Substring(2, 6) + "\">" + currentAddMIRow.Amount.ToString().Trim() + "</td>");
                                }
                                else
                                {
                                    sHtml.Append("<td align=right style=font-family:arial;font-size:10pt><font color=#000000>" + currentAddMIRow.Amount.ToString().Trim() + "</td>");
                                }
 
                                //Text Felt
                                //String text = currentAddMIRow.AllText.ToString();
                                //sHtml.AppendLine("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.AllText.ToString() + "</td>");
                                if (currentAddMIRow["AllText"] != DBNull.Value)
                                {
                                    sHtml.AppendLine("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.AllText.ToString().Trim() + "</td>");
                                }
                                else
                                {
                                    sHtml.AppendLine("<td style=font-family:arial;font-size:10pt></td>");
                                }
 
                                //AccountNO
                                 if (currentAddMIRow.AccountNo.Length <= 0)
                                 {
                                     sHtml.Append("<td style=font-family:arial;font-size:10pt></td>");
                                 }
                                 else
                                 {
                                     sHtml.Append("<td style=font-family:arial;font-size:10pt>" + currentAddMIRow.AccountNo.Trim() + "</td>");
                                 }
                                //Comment og extra felter
 
                                DsReconItems.ExtraRow[] extraRows = (DsReconItems.ExtraRow[])currentAddMIRow.GetExtraRows();
                                foreach (DsReconItems.ExtraColumnNameRow eRow in ecTable)
                                {
                                    DsReconItems.ExtraRow exTempRow = null;
                                    foreach (DsReconItems.ExtraRow exRow in extraRows)
                                    {
                                        //DsReconItems.ExtraColumnNameRow[] dr = (DsReconItems.ExtraColumnNameRow[])ecTable.Select("Type_ID = " + exRow.Type_ID);
 
                                        //sHtml.AppendLine("<td style=font-family:arial;font-size:10pt>" + exRow.Content.Trim() + "</td>");
                                        if (eRow.Type_ID == exRow.Type_ID)
                                        {
                                            exTempRow = exRow;
                                            break;
                                        }
                                    }
 
                                    if (exTempRow == null)
                                    {
                                        sHtml.Append("<td style=font-family:arial;font-size:10pt></td>");
                                    }
                                    else
                                    {
                                        sHtml.AppendLine("<td style=font-family:arial;font-size:10pt>" + exTempRow.Content.Trim() + "</td>");
                                    }
 
                                }
                            }
 
                        }
                    }
                    sHtml.Append("</tr>");
                    sHtml.Append("</table><br><br><br>");
                    //Table 2
                    sHtml.Append("<table cellpadding=0 cellspacing=0 style=width: 357px border=1>");
 
                    // Header
                    sHtml.Append("<tr>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("accountno") + "</th>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("count") + "</th>");
                    sHtml.Append("<th bgcolor=#F0EFED width= 60 style=font-family:arial;font-size:10pt>" + GetValue("amount") + "</th>");
                    sHtml.Append("</tr>");
                    //sHtml.Append("<tr>");
 
                    DsAccountSetting dsaccount = (DsAccountSetting)AmEngine.Instance.GetAllAccountSetting(myOper);
 
                    System.Data.DataTable tbl = dsaccount.Tables[0];
                    foreach (DsAccountSetting.AccountRow dr in dsaccount.Account.Rows)
                    {
                        if (dr.Type < 128)
                        {
                            sHtml.Append("<tr bgcolor=\"#" + colorLeft.Name.Substring(2, 6) + "\">");
                        }
                        else
                        {
                            sHtml.Append("<tr bgcolor=\"#" + colorRight.Name.Substring(2, 6) + "\">");
                        }
 
                        sHtml.Append("<td style=font-family:arial;font-size:10pt>" + dr["AccountNo"].ToString() + "</td>");
 
                        decimal amnt = 0;
                        int accountCount = 0;
                        foreach (Guid matchItem_ID in checkedMatchItems)
                        {
                            DsReconItems.MatchItemRow currentAddMIRow = dsReconItems1.MatchItem.FindByMatchItem_ID(matchItem_ID);
                            if (dr["AccountNo"].ToString() == currentAddMIRow.AccountNo)
                            {
                                amnt += currentAddMIRow.Amount;
                                accountCount++;
                            }
                        }
                        sHtml.Append("<td style=font-family:arial;font-size:10pt>" + accountCount.ToString() + "</td>");
                        //sHtml.Append("<td>" + amnt.ToString() + "</td>");
                        if (amnt < 0)
                        {
                            sHtml.Append("<td align=right style=font-family:arial;font-size:10pt><font color=\"#" + colorAmountCredit.Name.Substring(2, 6) + "\">" + amnt.ToString() + "</td>");
                        }
                        else
                        {
                            sHtml.Append("<td align=right style=font-family:arial;font-size:10pt>" + amnt.ToString() + "</td>");
                        }
 
                    }
 
                    sHtml.Append("</tr>");
                    sHtml.Append("<tr>");
                    sHtml.Append("<td style=font-family:arial;font-size:10pt></td>");
                    sHtml.Append("<td bold=true style=font-family:arial;font-size:10pt><b>Total :</b></td>");
                    sHtml.Append("<td style=font-family:arial;font-size:10pt align=right>" + totalamount.ToString() + "</td>");
                    sHtml.Append("</tr>");
                    //sHtml.Append("</tr>");
                    sHtml.Append("</table>");
                    sHtml.Append("</BODY>");
                    sHtml.Append("</HTML>");
                    oMsg.HTMLBody = sHtml.ToString();
                    oMsg.Display(true);
                   
                    //Finally, remember to set all objects to null once you are done:
                    oRecip = null;
                    oMsg = null;
                    oApp = null;
                    
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(GetValue("cannotopenemail")+ "\n" + ex.Message, GetValue("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }    
        }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 21 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 21 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros