I am having an issue referencing my namespance for sending an email in a contact codebehind event call. I can't seem to get EmailHelper to work, keep getting a context error
protected void Button1_Click(object sender, EventArgs e)
{
EmailHelper comm = new EmailHelper();
comm.SendMailMessage(txtEmail.Text, txtNotes);
}
The namespace Comm is supposed to handle all things communication related where the EmailHelper method SendMailMessage resides
using System.Net.Mail;
namespace Comm
{
public class EmailHelper
{
public static void SendMailMessage(string email, string message)
{
MailMessage emessage = new MailMessage();
emessage.From = new MailAddress(email);
emessage.To.Add(new MailAddress("scott@hopefestaz.com"));
emessage.Body = message;
emessage.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Send(emessage);
}
}
}
It seems like that the class EmailHelperis in the namespace Comm. In order to use the method
SendMailMessage in the code behind, you can add the namespace and modify the code as follows:
protected void Button1_Click1(object sender, EventArgs e)
{
Comm.EmailHelper comm = new Comm.EmailHelper();
comm.SendMailMessage(txtEmail.Text, txtNotes);
}
Or add the namespace in the code behind as follows:
using Comm;
In addition, you need to delete the static for your method.
You can also follow aamirha's advice to call the method directly.
Best wishes,
Please mark the replies as answers if they help or unmark if not.
Feedback to us
fredarator
Member
9 Points
15 Posts
Web form event referencing a class
Apr 23, 2012 06:53 PM|LINK
protected void Button1_Click(object sender, EventArgs e) { EmailHelper comm = new EmailHelper(); comm.SendMailMessage(txtEmail.Text, txtNotes); }using System.Net.Mail; namespace Comm { public class EmailHelper { public static void SendMailMessage(string email, string message) { MailMessage emessage = new MailMessage(); emessage.From = new MailAddress(email); emessage.To.Add(new MailAddress("scott@hopefestaz.com")); emessage.Body = message; emessage.Priority = MailPriority.High; SmtpClient smtp = new SmtpClient(); smtp.Send(emessage); } } }adamturner34
Contributor
3890 Points
971 Posts
Re: Web form event referencing a class
Apr 23, 2012 07:03 PM|LINK
if it's static, you don't need a new instance. You should be able to access it directly.
Try removing the static keyword.
fredarator
Member
9 Points
15 Posts
Re: Web form event referencing a class
Apr 23, 2012 07:14 PM|LINK
Still getting a could not find issue, the "am I using an assembly or reference?" hint
aamirha
Member
677 Points
170 Posts
Re: Web form event referencing a class
Apr 24, 2012 12:40 AM|LINK
I have check below script code, it is working,
protected void Button1_Click(object sender, EventArgs e) { EmailHelper.SendMailMessage(txtEmail.Text, txtNotes); }Fill ASP.NETDropdownlist through JSON data using jQuery
Catherine Sh...
All-Star
23372 Points
2490 Posts
Microsoft
Re: Web form event referencing a class
Apr 25, 2012 07:27 AM|LINK
Hi fredarator,
It seems like that the class EmailHelper is in the namespace Comm. In order to use the method SendMailMessage in the code behind, you can add the namespace and modify the code as follows:
protected void Button1_Click1(object sender, EventArgs e) { Comm.EmailHelper comm = new Comm.EmailHelper(); comm.SendMailMessage(txtEmail.Text, txtNotes); }Or add the namespace in the code behind as follows:
In addition, you need to delete the static for your method.
You can also follow aamirha's advice to call the method directly.
Best wishes,
Feedback to us
Develop and promote your apps in Windows Store