Previous Next

Thread: System.Net.Mail - multiple recipients per item using form drop down box

Last post 07-25-2008 11:38 AM by donsom. 9 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (10 items)

Sort Posts:

  • 07-20-2008, 11:50 PM

    • donsom
    • Top 500 Contributor
    • Joined on 07-19-2008, 11:52 AM
    • Posts 11

    System.Net.Mail - multiple recipients per item using form drop down box

      In the previous System.Web.Mail (V 1.1) one could string together multiple email addresses in the To.Add() [eg:mail.To.Add(aa@bb.com, bb@cc.com, cc@dd.com);]

    .NET 2.0 and above now seem to require multiple actual line entries ... see below.

    mail.To.Add("you@yourcompany.com");
    mail.To.Add("you2@yourcompany.com");
    mail.CC.Add("cc1@yourcompany.com");
    mail.CC.Add("cc2@yourcompany.com");

    I have a drop down list where some items have multiple emails attached (email addresses to be
    sent to).

    Will a C# switch statement work .... or some kind of array. It seems like an easy fix, but I can't
    seem to find examples other than the basic ones that just define multiple mail.To.Add(); multiple
    times.

  • 07-21-2008, 12:58 AM In reply to

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    Try to see if this works...on a side note, I have always been a fan of pulling email address from a config file and not hard-coding :) 

    string toList = "you@yourcompany.com, you2@yourcompany.com";
    string ccList = "cc1@yourcompany.com, cc2@yourcompany.com";
    MailMessage mail = new MailMessage();
    mail.To.Add(toList);
    mail.CC.Add(ccList);

            

  • 07-21-2008, 2:20 AM In reply to

    • ksingla
    • Top 10 Contributor
    • Joined on 06-14-2006, 3:02 AM
    • Redmond, WA
    • Posts 490

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    This page says System.Net.Mail.MailAddressCollection.Add() can take semi-colon separated multiple mail addresses.

    -Kanwal-

  • 07-21-2008, 12:06 PM In reply to

    • donsom
    • Top 500 Contributor
    • Joined on 07-19-2008, 11:52 AM
    • Posts 11

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    Thanks for the suggestion, ...

    You mean putting them in the web.config file as <appsetting> and then pulling them in using ConfigurationManager.AppSettings? ... never been down that path, but have read about it.

    Other programmers I know have used it to set permissions for user access to projects.

  • 07-21-2008, 5:35 PM In reply to

    • donsom
    • Top 500 Contributor
    • Joined on 07-19-2008, 11:52 AM
    • Posts 11

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    Here's what we came up with in the short term. A form passes the value .. the switch statement processes multiple emails based on the value passed. Since the "To.Add" is a collection, you should be able to use multiple "objMail.To.Add(new MailAddress("you1@someplace.com"));" in each switch statement.

    Note: I didn't use a ".cs" code behind, but left the code all in one page using  Import NameSpace = System.net. Mail at the top of the page.

    Any comments?  

    -----------------------

    <script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
        {
                SmtpClient smtpClient = new SmtpClient();
                MailMessage objMail = new MailMessage();
               
                objMail.From = new  MailAddress(txtFrom.Text);

                switch (txtDDL_Dept.SelectedValue)
                 {
                     case "Dept1":
                         objMail.To.Add(new MailAddress("you1@someplace.com"));
                         objMail.To.Add(new MailAddress("you2@someplace.com"));
                         objMail.To.Add(new MailAddress("you3@someplace.com"));
                        break;

                     case "Dept2":
                         objMail.To.Add(new MailAddress("you1@someplace.com"));
                         objMail.To.Add(new MailAddress("you2@someplace.com"));
                         objMail.To.Add(new MailAddress("you3@someplace.com"));
                        break;

                     case "Dept3":
                         objMail.To.Add(new MailAddress("you1@someplace.com"));
                         objMail.To.Add(new MailAddress("you2@someplace.com"));
                         objMail.To.Add(new MailAddress("you3@someplace.com"));
                        break;

                     case "Dept4":
                         objMail.To.Add(new MailAddress("you1@someplace.com"));
                         objMail.To.Add(new MailAddress("you2@someplace.com"));
                         objMail.To.Add(new MailAddress("you3@someplace.com"));
                        break;

                     case "Dept5":
                         objMail.To.Add(new MailAddress("you1@someplace.com"));
                         objMail.To.Add(new MailAddress("you2@someplace.com"));
                         objMail.To.Add(new MailAddress("you3@someplace.com"));
                        break;

                     default:
                         objMail.To.Add(new MailAddress("you1@someplace.com"));
                         objMail.To.Add(new MailAddress("you2@someplace.com"));
                         objMail.To.Add(new MailAddress("you3@someplace.com"));
                        break;
                 }//end switch statement  


                //objMail.CC.Add(new MailAddress("you8@someplace.com"));

                objMail.Subject = "Email";
                //objMail.Body = "Name: " + txtName.Text + "\r\n" + "Comments: " + txtComments.Text;
               
                objMail.IsBodyHtml = true;
                objMail.Subject = "Email Form";
                objMail.Body = "<html><head><title>Feedback Form</title></head><body>"
                    + "<p>Name : "
                    + txtName.Text + "</p>"
                    + "</p>" + "<p>Comments : "
                    + txtComments.Text + "</p>"
                    + "</body></html>";
                objMail.Priority = MailPriority.High;
       
                //smtpClient.Host = "mail.domain.com";
                //smtpClient.Credentials = new System.Net.NetworkCredential("username@domain.com","Password");
             
        try
            {
                smtpClient.Send(objMail);
                //Response.Write("Thank you for your feedback.");
                Response.Redirect("http://www.someplace.com");
            }
        catch (Exception exc)
            {
                Response.Write("Send failure: " + exc.ToString());
            }
        }
    </script>

     

  • 07-22-2008, 2:17 AM In reply to

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    I didn't test the code, but if it works go with it for now.  Here is another resource on using the System.Net namespace.

    http://systemnetmail.com/

    Steve Schofield
    Windows Server MVP - IIS
    http://weblogs.asp.net/steveschofield

    http://www.IISLogs.com
    Log archival solution
    Install, Configure, Forget
  • 07-22-2008, 9:14 AM In reply to

    • donsom
    • Top 500 Contributor
    • Joined on 07-19-2008, 11:52 AM
    • Posts 11

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    Thanks, been there and looked at that already. My requirement was to add multiple addresses based on department. That's why I needed to:

    1. Get the value of the selection in the drop down list.
    2. Use a switch statement to assign multiple email values to it (if required)

    I am however, interested in how one would do this from the web.config file so I can try that moving forward.

    I'll be testing the code in my last post some time today to verify it works.
     

    (below is the info provided on the webpage from the link you provided regarding multiple emails) 

    //since the To,Cc, and Bcc accept addresses, we can use the same technique as the From address
    //since the To, Cc, and Bcc properties are collections, to add multiple addreses, we simply call .Add(...) multple times
    mail.To.Add("you@yourcompany.com");
    mail.To.Add("you2@yourcompany.com");
    mail.CC.Add("cc1@yourcompany.com");
    mail.CC.Add("cc2@yourcompany.com");

     

  • 07-22-2008, 8:58 PM In reply to

    • donsom
    • Top 500 Contributor
    • Joined on 07-19-2008, 11:52 AM
    • Posts 11

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    Just as a note ... we've tested the code and it works properly for sending to multiple email addresses ... (where "txtDDL_Dept.SelectedValue" is what is being passed into the "switch" statement from drop down list in the form).

  • 07-24-2008, 5:02 PM In reply to

    • donsom
    • Top 500 Contributor
    • Joined on 07-19-2008, 11:52 AM
    • Posts 11

    Re: System.Net.Mail - multiple recipients per item using form drop down box

     Something strange ...!!

    In the form, we have 2 groups of radio button lists. The values in these lists do not need to be checked unless the individual wants to.

    When attempting to submit the email, we receive an error which we seem to trace back to the radio button list values being empty.

    Leaving a text field empty and submitting the form does not generate this issue.

    I thought about rejigging the form with an example I found below. In this, if a checkbox is checked, you can fill in the phone number value. However, won't this still generate the same "no value provided" as an empty radio button list value (if I change the items in the example below to radio button list select items)?? 

    protected void checkPhoneConfirmation_CheckedChanged(object sender, EventArgs e)
    {
    if(checkPhoneConfirmation.Checked)
    {
        textPhoneNumber.Enabled = true;
        validatorRequiredPhoneNumber.ValidationGroup = "AllValidators";
        validatorRegExPhoneNumber.ValidationGroup = "AllValidators";
    }
    else
    {
        textPhoneNumber.Enabled = false;
        validatorRequiredPhoneNumber.ValidationGroup = "";
        validatorRegExPhoneNumber.ValidationGroup = "";
    }

    Irregardless, if I leave the values (RBL_CallWhere.SelectedItem.Value and RBL_CallWhen.SelectedItem.Value) in the email body and there is no value... the form bombs on submission.

    objMail.Body = "<html><head><title>Feedback Form</title></head><body>"
                    + "<p>Name : "
                    + txtName.Text + "</p>"
                    + "</p>" + "<p>Comments : "
                    + txtComments.Text + "</p>"
                    + "<p>Email Address : "
                    + txtFrom.Text + "</p>"
                    + "<p>Account Number : "
                    + txtAcct.Text + "</p>"
                    + "<p>Phone Number : "
                    + txtPhone.Text + "</p>"
                    + "<p>Where to Call : "
                    + RBL_CallWhere.SelectedItem.Value + "</p>"
                    + "<p>When to Call : "
                    + RBL_CallWhen.SelectedItem.Value + "</p>"
                    + "<p>When to Call - Other Instructions: "
                    + txtOther.Text + "</p>"
                    + "</body></html>"; 

    Any ideas? Do the radio button values need to be poplulated with something? 

  • 07-25-2008, 11:38 AM In reply to

    • donsom
    • Top 500 Contributor
    • Joined on 07-19-2008, 11:52 AM
    • Posts 11

    Re: System.Net.Mail - multiple recipients per item using form drop down box

    Ended up using this to handle the null values from the email when submitted. It works. Email sends without issue after including this in the btnSubmit_Click()  and changing the values in the .Body of the email.

     string callWhen = "";
                 if (RBL_CallWhen.SelectedItem == null)
                    {
                        callWhen = "Nothing Selected";
                    }
                else
                    {
                        callWhen = RBL_CallWhen.SelectedItem.Value;
                    }    
            
                string callWhere = "";
                if (RBL_CallWhere.SelectedItem == null)
                    {
                        callWhere = "Nothing Selected";
                    }
                else
                    {
                        callWhere = RBL_CallWhere.SelectedItem.Value;
                    }

Page 1 of 1 (10 items)
Page view counter