Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a kind of form in asp.net. I want to send the form to email depending upon which city is selected from menu choice.
Here's the code in cs file:

C#
htbl["@HtmlPage"] = sbTableData.ToString();
    string _From = ConfigurationManager.AppSettings["MailFrom"].ToString();

      string _to = string.Empty;

switch(ddlRegion.SelectedItem.Value)
{
case "1": _to = ConfigurationManager.AppSettings["MailToA"]; break;
case "2": _to = ConfigurationManager.AppSettings["MailToB"]; break;
case "3": _to = ConfigurationManager.AppSettings["MailToC"]; break;
case "4": _to = ConfigurationManager.AppSettings["MailToD"]; break;
}


and in the webconfig file I have:

At present which is working-
XML
<appSettings>
    <add key="SMTP_HOST_ADDRESS" value="mail.company.com"></add>
    <add key="CREDENTIAL_USER" value="yourcredentialuser"></add>
    <add key="CREDENTIAL_PASSWORD" value="credentialpassword"></add>
    <add key="MailFrom" value="Admin@company.com"></add>
    <add key="MailTo" value="admin1@company.com"></add>



  </appSettings>


After I changed the webconfig file
XML
<appSettings>
    <add key="SMTP_HOST_ADDRESS" value="mail.company.com"></add>
    <add key="CREDENTIAL_USER" value="yourcredentialuser"></add>
    <add key="CREDENTIAL_PASSWORD" value="credentialpassword"></add>
    <add key="MailTo_A" value="A@company.com"></add>
    <add key="MailTo_B" value="B@company.com"></add>
    <add key="MailTo_C" value="C@company.com"></add>
    <add key="MailTo_D" value="D@company.com"></add>
    <add key="MailFrom" value="Admin@company.com"></add>


  </appSettings>


Now when I click on submit on the form I am getting an error message
Object reference not set to an instance of an object.

If you require any more info please let me know.
Posted
Updated 11-Dec-12 11:00am
v12

It looks like you have an issue here with your naming. In your config file, you call it MailTo_A but when you call it in code you call it MailToA. The one is missing the underscore.
 
Share this answer
 
Comments
Zakizawa 30-Nov-12 10:01am    
@Tim: It did not help I changed it to:
<add key="MailToA" value="A@company.com">
<add key="MailToB" value="B@company.com">
<add key="MailToC" value="C@company.com">
<add key="MailToD" value="D@company.com">

Do I have to make any changes to cs file as well where its:
case "1": _to -> case "1": to?
 
Share this answer
 
Comments
Zakizawa 1-Dec-12 11:31am    
tnx ProgramFOX. But which string shall i use. I got more confused now. The strings which I am using above are not correct??
There is also a simple method
C#
import system.net.mail

then
C#
MailMessage mail = new MailMessage();
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
                        smtp.Credentials = new System.Net.NetworkCredential
                        ("Username", "password");//Or your Smtp Email ID and Password
                        smtp.EnableSsl = true;
                        
                        mail.From = new MailAddress("mail address");
                        mail.Subject = "mail subject";
                        mail.Body = "mail body";
                        mail.IsBodyHtml = true;
                        mail.To.Add("mail id seperated with comma");
                        mail.Bcc.Add("mail addresses"); 
//if you want that each and every get mail as individual
                        smtp.Send(mail);
 
Share this answer
 
v2
Comments
Zakizawa 3-Dec-12 6:54am    
But again here how do i define the values where if cityA is selected email should go to A's email address.
007alok 3-Dec-12 7:03am    
You can check that if in dropdownlist city A is selected then use the query to select the user's email where city_column =selected city column
**Hope that you are using sql for fetching the user's email data

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900