how to send e-mail to list of recipients e-mail addresses instead of store owner/admin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
in my nopCommerce based website, i created a form in which

any member/visitor can type his/her e-mail
upload file
and write addition information and send.

My page form works fine and it sends the information to admin mailbox set in admin section > mail settings

Now i don't want to send e-mail to admin mailbox instead of that i want to define a list of recipients  e-mail addresses in the code so that information goes to them instead of admin mailbox. Please guide me

This is my code:

careers.aspx page


<td style="height: 26px; width: 93px; text-align : left">
                        <b><span style="font-size: 8pt">Your Email:</span></b></td>
                    <td style="height: 26px; width: 239px;">
                       <asp:TextBox ID="UsersEmail" runat="server" Columns="30" Height="17px" Width="193px"></asp:TextBox><span
                            style="font-size: 8pt">*</span>

<td style="height: 24px; width: 239px;">
                        <asp:FileUpload ID="AttachmentFile" runat="server" Height="26px" Width="239px" />
                    </td>

<td colspan="1" style="width: 93px">
                        <strong><span style="font-size: 8pt">Comments:</span></strong></td>
                    <td>
                        <strong><span style="font-size: 8pt"></span></strong>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <span style="font-size: 8pt"></span>
                       <asp:TextBox ID="Body" runat="server" Columns="55" Height="130px" Rows="30" TextMode="MultiLine"
                            Width="342px"></asp:TextBox>

                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="2" style="height: 26px">
                        <asp:Button ID="SendEmail" runat="server" OnClick="SendEmail_Click" Text="Submit" />
                    </td>
                </tr>




Careers.aspx.cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using NopSolutions.NopCommerce.BusinessLogic.Messages;

namespace NopSolutions.NopCommerce.Web
{
    public partial class Careers : BaseNopPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //On the first page load, hide the EmailSentForm Panel
                EmailSentForm.Visible = false;
            }
        }

        protected void SendEmail_Click(object sender, EventArgs e)
        {
            //'Make sure a file has been uploaded
            if (!AttachmentFile.HasFile)
            {
                LBLSuccErr.Text = "Please select a file to upload";
                LBLSuccErr.Visible = true;
                LBLSuccErr.ForeColor = System.Drawing.Color.Red;
            }
            else //Upload file to server
            {
                try
                {
                    MailAddress from = new MailAddress(MessageManager.AdminEmailAddress, MessageManager.AdminEmailDisplayName);
                    MailAddress to = new MailAddress(MessageManager.AdminEmailAddress, MessageManager.AdminEmailDisplayName);

                    //'(1) Create the MailMessage instance
                    MailMessage mm = new MailMessage(from, to);

                    //'(2) Assign the MailMessage's properties
                    mm.Subject = "Message Careers";
                    mm.Body = Body.Text;
                    mm.IsBodyHtml = false;

                    //'Attach the file
                    mm.Attachments.Add(new Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName));

                    MessageManager.SendEmail(mm);

                    //'Show the EmailSentForm Panel and hide the EmailForm Panel
                    LBLSuccErr.Visible = true;
                    LBLSuccErr.ForeColor = System.Drawing.Color.Green;
                    LBLSuccErr.Text = "Your file has been submitted successfully";
                }
                catch (SystemException err)
                {
                    LBLSuccErr.Visible = true;
                    LBLSuccErr.ForeColor = System.Drawing.Color.Red;
                    LBLSuccErr.Text = err.Message;
                }
            }

        }

    }
}



13 years ago
anyone ?
13 years ago
??
13 years ago
Will this list be the same list every time or will it be dynamic and change all the time?
13 years ago
thanks Barry for replying back...

it will remain same but could be change in some period of time

i was trying something like this but it is not working:

MailAddress from = new MailAddress(MessageManager.AdminEmailAddress, MessageManager.AdminEmailDisplayName);
                var to1 = new MailAddress("[email protected]");
                var to2 = new MailAddress("[email protected]");
                var to3 = new MailAddress("[email protected]");
                List<MailAddress> recipients = new List<MailAddress>();
                recipients.Add(to1);
                recipients.Add(to2);
                recipients.Add(to3);
13 years ago
foreach (var recipient in someRecipientListHardCodeOrLinq)
{
    MessageManager.InsertQueuedEmail(arguments);
}

That will send the email to each recipient seperately and is how Nop sends newsletters.

If the recipients all know each other then just use a cc string!
13 years ago
I have tried this but it doesn't work...please correct me if i am wrong in the code...

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using NopSolutions.NopCommerce.BusinessLogic.Messages;

namespace NopSolutions.NopCommerce.Web
{
    public partial class Careers : BaseNopPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //On the first page load, hide the EmailSentForm Panel
                EmailSentForm.Visible = false;
            }
        }

        protected void SendEmail_Click(object sender, EventArgs e)
        {
            //'Make sure a file has been uploaded
            if (!AttachmentFile.HasFile)
            {
                LBLSuccErr.Text = "Please select a file to upload";
                LBLSuccErr.Visible = true;
                LBLSuccErr.ForeColor = System.Drawing.Color.Red;
            }
            else //Upload file to server
            {
                try
                {
                    MailAddress from = new MailAddress(MessageManager.AdminEmailAddress, MessageManager.AdminEmailDisplayName);
                var to1 = new MailAddress("[email protected]");
                var to2 = new MailAddress("[email protected]");
                var to3 = new MailAddress("[email protected]");
                List<MailAddress> recipients = new List<MailAddress>();
                recipients.Add(to1);
                recipients.Add(to2);
                recipients.Add(to3);
                foreach (MailAddress to in recipients)
                {
//'(1) I am not sure what should come here
                   MessageManager.InsertQueuedEmail(5, from, to, string.Empty, string.Empty, "Subject", mailContent, DateTime.Now, 0, null);
                 }

                    //'(1) Create the MailMessage instance
                    MailMessage mm = new MailMessage(from, to);

                    //'(2) Assign the MailMessage's properties
                    mm.Subject = "Message Careers";
                    mm.Body = Body.Text;
                    mm.IsBodyHtml = false;

                    //'Attach the file
                    mm.Attachments.Add(new Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName));

                    MessageManager.SendEmail(mm);

                    //'Show the EmailSentForm Panel and hide the EmailForm Panel
                    LBLSuccErr.Visible = true;
                    LBLSuccErr.ForeColor = System.Drawing.Color.Green;
                    LBLSuccErr.Text = "Your file has been submitted successfully";
                }
                catch (SystemException err)
                {
                    LBLSuccErr.Visible = true;
                    LBLSuccErr.ForeColor = System.Drawing.Color.Red;
                    LBLSuccErr.Text = err.Message;
                }
            }

        }

    }
}

13 years ago
What I was getting at was something like:
var recipients = new List<string> { "[email protected]", "[email protected]", "[email protected]"}; // this could be static readonly, or you could get from a linq query
            foreach (string to in recipients)
            {
                MessageManager.InsertQueuedEmail(5, MessageManager.AdminEmailAddress, MessageManager.AdminEmailDisplayName, to, to, string.Empty, string.Empty, "Subject", mailContent, DateTime.UtcNow, 0, null);
            }

...but sorry I hadn't noticed you need to add an attachment. Give me a few minutes and I'll see if I can knock something up for you.
13 years ago
thanks Stephen, i am waiting for your reply...
13 years ago
MessageManager.SendEmail(mm);

How is that going to work? There's no overload which accepts an argument of type MailMessage!

Suggest you copy the code from SendEmail() and send the mail via SMTP yourself. Or ask Andrei to add an overload which accepts a MailMessage argument.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.