简体   繁体   中英

C# Cannot resolve symbol

I don't know why I'm getting this error. Seems elementary. Anyway I have a singleton class called EmailSender. The code below is short and easy. The issue is that I can't use sender in the MainWindow class. Anything I try such as sender.Send() is treated as though I've done asdafsafas.Send(). It's treated as though it's a random string of characters. Don't know why this is happening.

using System;
using System.Net.Mail;
using System.Windows.Forms;

namespace SendMail
{
    public partial class MainWindow : Form
    {
        #region Private variables
        private MailMessage msg = new MailMessage();
        private EmailSender sender = EmailSender.GetInstance();
        #endregion

        public MainWindow()
        {
            InitializeComponent();

        }

        private MailMessage PrepareMailMessage()
        {

            return msg;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

        }
    }
}

Here is the GetInstance method:

public static EmailSender GetInstance()
{
    return _instance ?? (_instance = new EmailSender());
}

This is because of the way you have this method defined (sender is a parameter). It's finding the method argument first , not your class level variable. You can qualify this:

private void btnSend_Click(object sender, EventArgs e)
{
    // sender here is the "(object sender, " paramater, so it's defined
    // as system object.

    // use this instead:
    this.sender.Send(); // The "this" will make the class find the instance level variable instead of using the "object sender" argument
}

That's because sender is not a mail object, but rather the button that triggered the event. You need the SmtpClient to send the email:

private void btnSend_Click(object sender, EventArgs e)  
{  
    SmtpClient client = new SmtpClient("192.0.0.1", 25); //host, port
    client.Send(msg);
}

Also, the MailMessage class implements IDisposable , so you need some code to dispose of the message once you're done with it.

I created a wrapper that includes everything you need to send emails, including disposal:

/// <summary>
/// Wrapper class for the System.Net.Mail objects
/// </summary>
public class SmtpMailMessage : IDisposable
{
    #region declarations

    MailMessage Message;
    SmtpClient SmtpMailClient;

    #endregion

    #region constructors

    /// <summary>
    /// Default constructor for the SmtpMailMessage class
    /// </summary>
    public SmtpMailMessage()
    {
        //initialize the mail message
        Message = new MailMessage();
        Message.Priority = MailPriority.Normal;
        Message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;            
        Message.From = new MailAddress("xxx@abc.com");           

        //initialize the smtp client
        SmtpMailClient = new SmtpClient();
        SmtpMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        SmtpMailClient.Host = "192.168.0.1";
        SmtpMailClient.Port = 25;
    }

    /// <summary>
    /// Parameterized constructor for the SmtpMailMessage class. Allows for override of the default
    /// SMTP host and port number
    /// </summary>
    /// <param name="HostIP">The IP address of the exchange server</param>
    /// <param name="PortNumber">The port number for ingoing and outgoing SMTP messages</param>
    public SmtpMailMessage(string HostIP, int PortNumber) : this()
    {
        //override the smtp host value
        SmtpMailClient.Host = HostIP;

        //override the smtp port value
        SmtpMailClient.Port = PortNumber;
    }

    #endregion

    #region subject / body

    /// <summary>
    /// The body content of the mail message
    /// </summary>
    public string Body
    {
        get
        {
            return Message.Body;
        }
        set
        {
            Message.Body = value;
        }
    }

    /// <summary>
    /// the subject of the mail message
    /// </summary>
    public string Subject
    {
        get
        {
            return Message.Subject;
        }
        set
        {
            Message.Subject = value;
        }
    }

    #endregion

    #region mail type

    /// <summary>
    /// Gets or sets a value that determines whether the mail message
    /// should be formatted as HTML or text
    /// </summary>
    public bool IsHtmlMessage
    {
        get
        {
            return Message.IsBodyHtml;
        }
        set
        {
            Message.IsBodyHtml = value;
        }
    }

    #endregion

    #region sender

    /// <summary>
    /// Gets or sets the from address of this message
    /// </summary>
    public string From
    {
        get
        {
            return Message.From.Address;
        }
        set
        {
            Message.From = new MailAddress(value);
        }
    }

    #endregion

    #region recipients

    /// <summary>
    /// Gets the collection of recipients
    /// </summary>
    public MailAddressCollection To
    {
        get
        {
            return Message.To;

        }
    }

    /// <summary>
    /// Gets the collection of CC recipients 
    /// </summary>
    public MailAddressCollection CC
    {
        get
        {
            return Message.CC;
        }
    }

    /// <summary>
    /// Gets the collection of Bcc recipients
    /// </summary>
    public MailAddressCollection Bcc
    {
        get
        {
            return Message.Bcc;
        }
    }

    #endregion

    #region delivery notification

    /// <summary>
    /// Gets or sets the delivery notification settings for this message
    /// </summary>
    public DeliveryNotificationOptions DeliveryNotifications
    {
        get
        {
            return Message.DeliveryNotificationOptions;
        }
        set
        {
            Message.DeliveryNotificationOptions = value;
        }
    }

    #endregion

    #region priority

    /// <summary>
    /// Gets or sets the Priority of this message
    /// </summary>
    public MailPriority PriorityLevel
    {
        get
        {
            return Message.Priority;
        }
        set
        {
            Message.Priority = value;
        }
    }

    #endregion

    #region send methods

    /// <summary>
    /// Sends the message anonymously (without credentials)
    /// </summary>
    public void Send()
    {
        SmtpMailClient.Send(Message);
    }

    /// <summary>
    /// Sends the message with authorization from a network account   
    /// </summary>
    /// <param name="Username">The Windows username of the authorizing user</param>
    /// <param name="Password">The Windows password of the authorizing user</param>
    /// <param name="Domain">The domain name of the network to which the authorizing user belongs</param>
    public void Send(string Username, string Password, string Domain)
    {
        //attach a network credential to this message using the information passed into the method
        SmtpMailClient.Credentials = new NetworkCredential(Username, Password, Domain);

        //send the message
        SmtpMailClient.Send(Message);
    }

    #endregion

    #region IDisposable implementation

    ~SmtpMailMessage()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);            
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (Message != null)
                Message.Dispose();
            Message = null;                
            SmtpMailClient = null;
        }
    }

    #endregion        
}

Implementation:

using (SmtpMailMessage mail = new SmtpMailMessage("192.168.0.1", 25))
{
    //set the to address to the primary email
    mail.To.Add("xxx@abc.com");

    //set the message type and subject and body
    mail.IsHtmlMessage = true;
    mail.Subject = "Foo";
    mail.Body = "Hello world!";

    //send the email
    mail.Send();
}

If you're doing it inside the scope of your btnSend_Click method, the parameter object sender takes precedence over the global EmailSender sender .

You should either rename your global variable, eg: EmailSender m_sender or specify exactly which sender you want: this.sender.Send()

I guess you call the sender.Send in the btnSend_Click function.

In that function is a param also called sender (object sender). Now you code is confused which one to use. So rename your private var sender.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM