Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
<appSettings>
    <add key="fromemailid" value="development@relone.com"/>
  </appSettings>
on codebehind i am using the following code to get frommailid
MailAddress from = new MailAddress(ConfigurationSettings.AppSettings("fromemailid"));
but iam getting
"non-invocable member 'System.Configuration.ConfigurationSettings.AppSettings' cannot used like a method".
What should I do to overcome this error....Please help me.
on default.aspx.cs page
if (conn.send_mail(tomailid)) 
Error	1	Member 'WorkflowSystem.DataAccess.connection1.send_mail(string)' cannot be accessed with an instance reference; qualify it with a type name instead	C:\Users\user\Desktop\WebApplication1\WebApplication1\Default.aspx.cs	73	25	WebApplication1
on connection.cs page
public static bool send_mail(string toemailid)
        {
            MailAddress from = new MailAddress(ConfigurationSettings.AppSettings["fromemailid"].ToString());
        }
Error	2	'WorkflowSystem.DataAccess.connection1.send_mail(string)': not all code paths return a value	C:\Users\user\Desktop\WebApplication1\WebApplication1\connection.cs	110	28	WebApplication1
What is to be done now...Please help me.
Posted 19-Feb-13 1:12am
v3
Comments
Tadit Dash at 19-Feb-13 9:11am
   
What is the "conn" in if (conn.send_mail(tomailid)) ?
Member 8701813 at 19-Feb-13 12:13pm
   
on default.aspx.cs page:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
WorkflowSystem.DataAccess.connection1 conn = new WorkflowSystem.DataAccess.connection1();
protected void btnSubmit_Click(object sender, EventArgs e)
{

 
try
{
// Get the HttpFileCollection
 
HttpFileCollection hfc = Request.Files;
fn = new string[hfc.Count];
documentlocation = new string[hfc.Count];
docloc = new string[hfc.Count];
for (int i = 0; i < hfc.Count; i++)
{
 
HttpPostedFile hpf = hfc[i];
 
if (hpf.ContentLength > 0)
{
fn[i] = Path.GetFileName(hpf.FileName);
docloc[i] = Path.GetFullPath(hpf.FileName);
hpf.SaveAs(@"D:\testfiles\" + fn[i]);
documentlocation[i] = location + fn[i];
}
}
int j = conn.insert_update_request("Insert", 6, Convert.ToString(docloc[0]),Convert.ToString(docloc[1]),Convert.ToString(docloc[2]), txtdescription.Text.ToString(), 1, txtApplicationFormId.Text.ToString());
if (j > 0)
{
dt = conn.select_mailid(6);
if (dt.Rows.Count > 0)
{
tomailid = (string)dt.Rows[0][0];
}
if (conn.send_mail(tomailid))
{
}
}
}

catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
on connection.cs page:
namespace WorkflowSystem.DataAccess
{
public class connection1
{
public SqlConnection connection;
public connection1()
{
try
{
string cwfs = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["WorkflowSystem"].ConnectionString;
connection = new SqlConnection (cwfs);
}
catch(Exception objex)
{
Console.WriteLine(objex.Message);
}
}
public DataTable select_mailid(int usertypeid)
{
DataTable dt_select_mailid = new DataTable();
try
{
connection.Open();
SqlCommand cmd = new SqlCommand("sp_select_mailid_by_usertypeid", connection);
cmd.Connection = connection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@usertypeid", usertypeid);
SqlDataAdapter da = new SqlDataAdapter(cmd.ToString(), connection);
da.SelectCommand = cmd;
da.Fill(dt_select_mailid);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return dt_select_mailid;
}
public int insert_update_request(string type, int usertypeid, string document1location, string document2location, string document3location, string descr, int statusid, string appid)
{
int i;
try
{
connection.Open();
SqlCommand cmd = new SqlCommand("sp_insert_update_request", connection);
cmd.Connection = connection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@type", type);
cmd.Parameters.AddWithValue("@usertypeid", usertypeid);
cmd.Parameters.AddWithValue("@doc1location", document1location);
cmd.Parameters.AddWithValue("@doc2location", document2location);
cmd.P
Member 8701813 at 19-Feb-13 12:21pm
   
continuation....
cmd.Parameters.AddWithValue("@description", descr);
cmd.Parameters.AddWithValue("@statusid", statusid);
cmd.Parameters.AddWithValue("@applicationid", appid);
i = cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
i = 0;
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return i;
}
public static bool send_mail(string toemailid)
{
MailAddress from = new MailAddress(ConfigurationSettings.AppSettings["fromemailid"].ToString());
MailAddress to = new MailAddress(toemailid);
MailMessage mm = new MailMessage(from, toemailid);

}
this is my code...i think now u might have an idea why i am conn.send_mail(tomailid)...
still i have to write ...any help is appreciated..
Member 8701813 at 19-Feb-13 12:43pm
   
continuation..
public static bool send_mail(string toemailid)..here i am getting toemailid from database.
{
MailAddress from = new MailAddress(ConfigurationSettings.AppSettings["fromemailid"].ToString());
MailAddress to = new MailAddress(toemailid);..is this statement correct?
MailMessage mm = new MailMessage(from, toemailid);...here i am facing pbm
mm.Subject = "mail sending";
mm.Body = "mail sending";
SmtpClient client = new SmtpClient();
try
{
client.Send(mm);
return true;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return false;
}

}
Tadit Dash at 20-Feb-13 1:01am
   
Please check my Updated answer under heading "Update".
 
Thanks...
Member 8701813 at 20-Feb-13 8:13am
   
Error 2 The best overloaded method match for 'System.Net.Mail.MailMessage.MailMessage(System.Net.Mail.MailAddress, System.Net.Mail.MailAddress)' has some invalid arguments C:\Users\user\Desktop\WebApplication1\WebApplication1\connection.cs 114 30 WebApplication1
Error 3 Argument 2: cannot convert from 'string' to 'System.Net.Mail.MailAddress' C:\Users\user\Desktop\WebApplication1\WebApplication1\connection.cs 114 52 WebApplication1
i am getting the above 2 errors
pls suggest me any solution...
Member 8701813 at 20-Feb-13 8:32am
   
MailMessage mm = new MailMessage(from, to);
i did a small modification...could u tell me why "static" should be removed
Tadit Dash at 20-Feb-13 11:47am
   
The static methods can only be called within its own class, not from other classes.
 
And I guess after writing
MailMessage mm = new MailMessage(from, to);
the above errors are fixed, right ?
 
As MailMessage Class has constructors like below...(you can see there)
 
1. MailMessage Constructor (MailAddress, MailAddress)
where you should pass two MailAddress objects.
 
2. MailMessage Constructor (String, String)
where you should pass two strings.
 
3. MailMessage Constructor (String, String, String, String)
where you should pass four string.
 
-> Previously, you have written...
 
MailMessage mm = new MailMessage(from, toemailid);
 
where from is a object of MailAddress Class, but toemailid was a string.
 

-> Now you have done correct like...
 
MailMessage mm = new MailMessage(from, to);
 
where both from and to are objects of MailAddress Class.
Member 8701813 at 20-Feb-13 13:42pm
   
thanks..yes now i understood....in ur reply u didn't point out this....i tried by looking into the statements line by line....then i noticed this mistake...even then u gave me valuable suggestion which helped me a lot.thaanks once again Tadit dash
Tadit Dash at 21-Feb-13 1:27am
   
Welcome.
Actually, you have not mentioned about these two errors. So, I gave suggestions for the previous two errors.
 
After you posted the errors, I suggested what to do.
 
I am now updating my answer by adding these two errors, so that my answer will be a complete one.
 
Please accept my answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...
 
Thanks,
Tadit
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are using wrong brackets.
Rewrite your code as
MailAddress from = new MailAddress(ConfigurationSettings.AppSettings["fromemailid"].toString());
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

1.

"non-invocable member 'System.Configuration.ConfigurationSettings.AppSettings' cannot used like a method"

Do like below (as suggested by @CodeMaster_Noob).
MailAddress from = new MailAddress(ConfigurationSettings.AppSettings["fromemailid"].toString());
 
2.

"not all code paths return a value"

The method signature of "send_mail" is...
public static bool send_mail(string toemailid)
where return type is "bool" not void.
But you have not returned anything. You should return either true or false according to your logic.
 

Update
Version-1
Ok, so do two things.
 
1. Change function signature.
public static bool send_mail(string toemailid)
Remove "static" from here.
So, it will be...
public bool send_mail(string toemailid)
The static methods can only be called within its own class, not from other classes.
 
2. Put a break point in this function and see whether execution comes to this function, when you are sending mail.
 

Version-2
1.
Quote:
Error 2 The best overloaded method match for 'System.Net.Mail.MailMessage.MailMessage(System.Net.Mail.MailAddress, System.Net.Mail.MailAddress)' has some invalid arguments C:\Users\user\Desktop\WebApplication1\WebApplication1\connection.cs 114 30 WebApplication1
Error 3 Argument 2: cannot convert from 'string' to 'System.Net.Mail.MailAddress' C:\Users\user\Desktop\WebApplication1\WebApplication1\connection.cs 114 52 WebApplication1

After writing like below
MailMessage mm = new MailMessage(from, to);
the above errors will be fixed.
 
As MailMessage Class has constructors like below...(you can see there)
 
1. MailMessage Constructor (MailAddress, MailAddress)
where you should pass two MailAddress objects.
 
2. MailMessage Constructor (String, String)
where you should pass two strings.
 
3. MailMessage Constructor (String, String, String, String)
where you should pass four string.
 
-> Previously, you have written...
MailMessage mm = new MailMessage(from, toemailid);
where from is a object of MailAddress Class, but toemailid was a string.
 

-> Now you have done correct like...
MailMessage mm = new MailMessage(from, to);
where both from and to are objects of MailAddress Class.
 

Thanks...
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Afzaal Ahmad Zeeshan 82
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
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