1
Answer

Export PDF then send PDF attachment to email

Mohan Chandra

Mohan Chandra

8y
334
1
Hi i am exporting grid view in PDF then I want attach PDF in email, so I am getting error,
 
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. 
 
my code
if (IsEmail)                    
{                        
PdfWriter.GetInstance(pdfDoc, ms);                    
}
MemoryStream ms = new MemoryStream(); 
pdfDoc = new Document(PageSize.A4_LANDSCAPE.Rotate(), 10, 10, 45, 45);   
if (IsEmail) 
{     
PdfWriter.GetInstance(pdfDoc, ms); 
} 
else 
{     
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream); 
}
 
if (IsEmail)                     
{                         
ms = new MemoryStream();                         
byte[] bytes = ms.ToArray();                         
Session["Stream"] = bytes;                         
ms.Write(bytes, 0, bytes.Length);                         
SendEmail(ms, Date, true);                         
ms.Flush();                         
ms.Close();                     
}
public void SendEmail(MemoryStream ms, DateTime UpToDate, bool PDF)        
{            
try            
{                
#region Clear Fields                
hdnEmailBody.Value = string.Empty;                
hdnEmailSubject.Value = string.Empty;                
hdnEmailTo.Value = string.Empty;                
hdnEmailType.Value = string.Empty;                
hdnEmailFileName.Value = string.Empty;                
#endregion                 
if (Session[cGlobalUI.gsEmailBody] != null)                
{                    
Session.Remove(cGlobalUI.gsEmailBody);                
}                  
string From = ConfigurationManager.AppSettings[cGlobalUI.gpUserId];                
string CustName = txtCustomer.Text;//cmbCustomerName.SelectedItem.Text.Trim();                CustName = CustName.Replace(" ", "_");                string To = hfEmailId.Value;                //string Subject = "Statement Of Outstanding Invoices As Of Date " + Date.ToString("dd-MMM-YY");                string Type = "SOOI";                string FileName;                if (PDF)                {                     FileName = CustName.Trim() + "_Outstanding_Invoices_" + UpToDate.ToString("ddMMyyyy") + ".pdf";                }                else                {                    FileName = CustName.Trim() + "_Outstanding_Invoices_" + UpToDate.ToString("ddMMyyyy") + ".xls";                }                  //Mohan: fetch email templates from DB instead of making manually, if its null then default template                #region Generate Email Matter For Outstanding Statement Attachment                //Mohan: fetch email template subject from web and if its null then default email template                eMailTemplate emailtemplate = new eMailTemplate();                emailtemplate = emailtemplate.getEmailTemplates(Session, "Outstanding Invoice Template");                  string Subject;                StringBuilder StrBody = new StringBuilder();                if (emailtemplate != null)                {                    Subject = emailtemplate.Subject + " " + UpToDate.ToString("dd-MMM-yyyy");                    StrBody.Append("<div style='font-family:Arial; font-size:10pt'>");                    StrBody.Append("Dear Mr/Mrs " + txtCustomer.Text + "," + "<br/>");                    string Msg = emailtemplate.Message;                    StrBody.Append(Msg);                    StrBody.Append("</div>");                }                else                {                    Subject = "Statement of Outstanding Invoices as of " + UpToDate.ToString("dd-MMM-yyyy");                    StrBody.Append("<div style='font-family:Arial; font-size:10pt'>");                    StrBody.Append("Dear Mr/Mrs " + txtCustomer.Text + "," + "<br/>" + "<br/>");                    StrBody.Append("Please find enclosed the statement of outstanding invoice(s) as of " + UpToDate.ToString("dd-MMM-yyyy") + ". As per our records we are yet to receive payment of the invoices listed in the enclosed file.");                    StrBody.Append("</div>");                }                  #endregion                 Session[cGlobalUI.gsEmailBody] = StrBody.ToString();                string Body = "Body";                string StrURL = "../ar_users/resend_email.aspx?Subject=" + Subject + "&Body=" + Body + "&To=" + To + "&Type=" + Type + "&Fname=" + FileName;                  hdnEmailBody.Value = Body;                hdnEmailSubject.Value = Subject;                hdnEmailTo.Value = To;                hdnEmailType.Value = Type;                hdnEmailFileName.Value = FileName;                  // For open E-mail window                if (string.IsNullOrEmpty(To))                {                    To = string.Empty;                      ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW_DIALOG", "Myemail.click();", true);                }                else                {                    ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW_DIALOG", "Myemail.click();", true);                }            }            catch (Exception Ex)            {                cGlobalUI.handleException(Page, System.Reflection.MethodBase.GetCurrentMethod().Name, Ex);            }        }

please help me 

Answers (1)