Exemple #1
0
        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="emailAddresses">A string array of email addresses to send.</param>
        /// <param name="senderEmailAddress">The sender's email address</param>
        /// <param name="grid">The grid.</param>
        /// <returns>Number of email's sent</returns>
        /// <remarks>This asynchronous method are logged at Windows EventLog (Application category) if any error. </remarks>
        public static int SendEmail(string[] emailAddresses, string senderEmailAddress, Grid grid)
        {
            StringBuilder sb = new StringBuilder(string.Empty);

            sb.Append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">");

            // Render header for rows if more then 1 row.
            if (grid.Rows.Count > 1)
            {
                sb.Append("<tr>");
                foreach (Column column in grid.Columns)
                {
                    if (column.Visibility == Visibility.Both || column.Visibility == Visibility.Grid)
                    {
                        sb.AppendFormat("<td>{0}</td>", column.Title);
                    }
                }
                sb.Append("</tr>");
            }
            Mail      wgmail      = new Mail();
            ArrayList attachments = new ArrayList();

            // Render column foreach row if one row, else render all columns in one row.
            foreach (Row gridrow in grid.Rows)
            {
                if (gridrow == null)
                {
                    continue;
                }
                if (grid.Rows.Count > 1)
                {
                    sb.Append("<tr>");
                }
                foreach (Column column in gridrow.Columns)
                {
                    if (column.Visibility == Visibility.None || column.ColumnType == ColumnType.Chart ||
                        column.ColumnType == ColumnType.SystemColumn)
                    {
                        continue;
                    }

                    if (column.ColumnType == ColumnType.File && grid.Rows.Count == 1)
                    {
                        File attachcolumn = (File)column;

                        Attachment attachfile =
                            new Attachment(attachcolumn.AbsoluteDirectoryFileName(gridrow[column.ColumnId]).Replace("NULL", grid.InternalId),
                                           "UUEncode");

                        attachments.Add(attachfile);
                        continue;
                    }
                    object value = gridrow[column.ColumnId].Value;
                    if (column.ColumnType == ColumnType.ColumnTemplate)
                    {
                        if (grid.DisplayView == DisplayView.Detail && ((ColumnTemplate)column).DetailViewTemplate != null)
                        {
                            value = ((ColumnTemplate)column).RenderTemplate(gridrow[column.ColumnId]);
                        }
                        else if (grid.DisplayView == DisplayView.Grid && ((ColumnTemplate)column).GridViewTemplate != null)
                        {
                            value = ((ColumnTemplate)column).RenderTemplate(gridrow[column.ColumnId]);
                        }
                    }
                    switch (column.ColumnType)
                    {
                    case ColumnType.Foreignkey:
                    {
                        RowCollection row = ((Foreignkey)column).Table.Rows;

                        for (int i = 0; i < row.Count; i++)
                        {
                            if (row[i][column.ColumnId].Value == null ||
                                row[i].PrimaryKeyUpdateValues != row[i][column.ColumnId].Value.ToString())
                            {
                                continue;
                            }
                            value =
                                Foreignkey.BuildDisplayText(i,
                                                            ((Foreignkey)column).ValueColumn,
                                                            ((Foreignkey)column).Table);
                            break;
                        }
                    }
                    break;

                    case ColumnType.ManyToMany:
                    {
                        value = string.Empty;
                        ManyToManyCollection items = ((ManyToMany)column).LoadItems(gridrow[column.ColumnId]);

                        for (int i = 0; i < items.Count; i++)
                        {
                            if (items[i].Checked)
                            {
                                if (value == null)
                                {
                                    value = items[i].DisplayText;
                                }
                                else
                                {
                                    value += string.Format(" , {0}", items[i].DisplayText);
                                }
                            }
                        }
                    }
                    break;
                    }
                    if ((column.Visibility == Visibility.Both || column.Visibility == Visibility.Grid) &&
                        grid.Rows.Count > 1)
                    {
                        sb.AppendFormat("<td>{0}</td>", value);
                    }
                    else if ((column.Visibility == Visibility.Both || column.Visibility == Visibility.Detail) &&
                             grid.Rows.Count == 1)
                    {
                        if (!column.HideDetailTitle)
                        {
                            sb.AppendFormat("<tr><td><b>{0}</b></td></tr><tr><td>{1}</td></tr>", column.Title, value);
                        }
                        else
                        {
                            sb.AppendFormat("<tr><td></td></tr><tr><td>{0}</td></tr>", value);
                        }
                    }
                }

                sb.Append("</tr>");
            }
            sb.Append("</table>");

            string smtpserver = GridConfig.Get("WGSMTPSERVER", (string)null);

            if (grid.MailForm.SmtpServer != null)
            {
                smtpserver = grid.MailForm.SmtpServer;
            }
            string body       = sb.ToString();
            int    sentemails = 0;

            foreach (string emailaddress in emailAddresses)
            {
                if (!ValidEmail(emailaddress))
                {
                    continue;
                }
                sentemails++;
                MailMessage message = new MailMessage(new MailAddress(emailaddress), new MailAddress(emailaddress));

                for (int i = 0; i < attachments.Count; i++)
                {
                    message.Attachments.Add((Attachment)attachments[i]);
                }

                message.IsBodyHtml = true;

                message.From = string.IsNullOrEmpty(senderEmailAddress) == false
                                   ? new MailAddress(senderEmailAddress)
                                   : new MailAddress(emailaddress);
                message.Subject = grid.MailForm.Subject ?? grid.Title;
                message.Body    = HttpUtility.HtmlDecode(body);
                wgmail.SendEmail(message, smtpserver);
            }
            return(sentemails);
        }