Click here to Skip to main content
11,573,084 members (70,040 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:

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-
<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
<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 30-Nov-12 2:45am
Edited 11-Dec-12 11:00am
v12
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Zakizawa at 30-Nov-12 10:01am
   
@Tim: It did not help I changed it to:





Do I have to make any changes to cs file as well where its:
case "1": _to -> case "1": to?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Zakizawa at 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??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is also a simple method
import system.net.mail

then
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);
  Permalink  
v2
Comments
Zakizawa at 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 at 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)

  Print Answers RSS


Advertise | Privacy | Mobile
Web03 | 2.8.150624.2 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100