0

I have a class file I'd like to use on my Web Forms Application. This is one that I found in the Internet. I have placed it in the App_Code folder and am trying to use it on the event

protected void sendBtn_Click(object sender, EventArgs e)
        {

        }

Class File:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class SendEmail
{
    public void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();

        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));

        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }      // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;

        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        // Send the mail message
        mSmtpClient.Send(mMailMessage);
    }
}

I can't access the methods and have tried the following :

SendEmail send = new SendEmail();

var send = new SendEmail();

SendEmail.SendMailMessage

using SendEmail

All of which to no avail.

I am an absolute beginner

2
  • What are the errors that you get? Commented Mar 1, 2013 at 17:08
  • I get red lines on the codes that I write to access them. Commented Mar 1, 2013 at 17:09

5 Answers 5

1

Before you do anything else you will need to add the System.Net.Mail namespace to your SendEmail class file.

This will leave you with-

using System.Net.Mail;

public class SendEmail
{
    public void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();

        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));

        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }      // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;

        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        // Send the mail message
        mSmtpClient.Send(mMailMessage);
    }
}

None of the other "usings" in there are required, these are added by default by Visual Studio but aren't actually used in the class.

Then you can simply use the following in your page-

protected void sendBtn_Click(object sender, EventArgs e)
{
    SendEmail send = new SendEmail();

    send.SendMailMessage("[email protected]", "[email protected]", null, null, "Test e-mail", "Test e-mail");
}

I suspect the fact your class wasn't compiling was what was tripping you up. The above code all built without problems in a sample project.

Although it is beyond the scope of the question I'd be tempted to make the helper classes static so you don't have to instantiate a new copy of SendEmail all the time.

See http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx and http://msdn.microsoft.com/en-us/library/sf0df423.aspx for more details on the using directive.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd like to recommend these:

  1. Add the namespace to your class file. using System.Net.Mail;

  2. Provide arguments when you call the function (SendMailMessage).

  3. Make class SendMail as a static class, SendMailMessage as static function.

Comments

0

Put your class file in the same folder where your code that calls sendBtn_Click is located. If your code with the handler has a line somewhere at the top that says namespace SomeNamespace {, make sure that the class file you copied also has the same line. Your curly braces may vary.

3 Comments

Did you try compiling the code? It will tell you more exactly what the problem is. Or, if you hover your mouse over the red line it should give you more details. What are they?
The namespace SendEmail could not be found. Something along those lines
Are you calling the method like this? send.SendMailMessage("[email protected]", "[email protected]", null, null, "my subject", "my email body");
0

Generally what I would do is create a separate "Library" project in your visual studio solution and reference that from your main website project.

Your new SendMail class will go in it.

Sometimes it seems like you'll just need a few helper classes but the amount tends to grow over time. Separating things out also helps with compile speed/organization as the project grows.

Comments

0

Change your class file to:

namespace MyCompany
{
     public class SendEmail
     {
          //Class properties and methods here.
     }
}

Then in your web form code behind, add:

using MyCompany;

Here is some more information about using namespaces which you might find helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.