C#无法解析符号
我不知道为什么会出现这个错误。看起来很简单。无论如何,我有一个名为EmailSender的单例类。下面的代码简单明了。问题是我不能在MainWindow类中使用sender。我尝试的任何东西,如sender.Send(),都会被视为我做了asdafsafas.Send()。它被视为一个随机字符串。我不知道为什么会这样C#无法解析符号,c#,.net,singleton,symbols,resolve,C#,.net,Singleton,Symbols,Resolve,我不知道为什么会出现这个错误。看起来很简单。无论如何,我有一个名为EmailSender的单例类。下面的代码简单明了。问题是我不能在MainWindow类中使用sender。我尝试的任何东西,如sender.Send(),都会被视为我做了asdafsafas.Send()。它被视为一个随机字符串。我不知道为什么会这样 using System; using System.Net.Mail; using System.Windows.Forms; namespace SendMail {
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)
{
}
}
}
下面是GetInstance方法:
public static EmailSender GetInstance()
{
return _instance ?? (_instance = new EmailSender());
}
这是因为
sender
不是邮件对象,而是触发事件的按钮。您需要SmtpClient
发送电子邮件:
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient("192.0.0.1", 25); //host, port
client.Send(msg);
}
另外,MailMessage
类实现了IDisposable
,因此在处理完消息后,您需要一些代码来处理它
我创建了一个包装器,其中包含发送电子邮件所需的所有内容,包括处理:
/// <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
}
这是因为
sender
不是邮件对象,而是触发事件的按钮。您需要SmtpClient
发送电子邮件:
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient("192.0.0.1", 25); //host, port
client.Send(msg);
}
另外,MailMessage
类实现了IDisposable
,因此在处理完消息后,您需要一些代码来处理它
我创建了一个包装器,其中包含发送电子邮件所需的所有内容,包括处理:
/// <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
}
如果在
btnSend\u单击方法的范围内执行此操作,则参数object sender
优先于全局EmailSender
您应该重命名您的全局变量,例如:EmailSender m\u sender
,或者指定您想要的发件人:this.sender.Send()
,如果您是在btnSend\u单击
方法的范围内执行此操作,参数对象发送者
优先于全局电子邮件发送者
您应该重命名您的全局变量,例如:EmailSender m\u sender
,或者确切地指定所需的发件人:this.sender.Send()
这是因为您定义此方法的方式(sender是一个参数)。它首先查找方法参数,而不是类级变量。您可以将其限定为:
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
}
这是因为您定义此方法的方式(sender是一个参数)。它首先查找方法参数,而不是类级变量。您可以将其限定为:
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
}
我想你应该给发件人打电话。使用BTN发送功能发送
该函数中有一个参数,也称为sender(objectsender)。现在,您的代码不知道使用哪一个。因此,请重命名您的私人var发送者。我想您可以呼叫发送者。请使用BTN发送功能发送
该函数中有一个参数,也称为sender(objectsender)。现在,您的代码不知道使用哪一个。因此,请重命名您的私人var发件人。确切的错误消息是什么?无法解析符号发件人。编译时,我得到:无效令牌'('在MainWindow.csOK的类、结构或接口成员声明中,哪一行产生此错误消息?您的单例不是线程安全的。@DBM:在简单的Windows窗体程序中,这很可能不是问题。确切的错误消息是什么?无法解析符号发送器。编译时,我得到:无效令牌'('在MainWindow.csOK的类、结构或接口成员声明中,哪一行会产生此错误消息?您的单例不是线程安全的。@DBM:嗯,在一个简单的Windows窗体程序中,这很可能不是问题。好的,我明白了。哦,我忘了sender对象。我正在尝试修改我的C#,谢谢。@User:一个简单的解决方法是s遵循样式约定,并在私有变量前面加上前缀uu。例如:\u sender
@DBM,或者,养成遵循StyleCop准则的习惯,该准则建议始终通过this.**
为成员访问添加前缀。好的,我明白了。哦,我忘记了sender对象。我正在尝试修改我的C,谢谢。@User:简单的工作karound将遵循样式约定,并在私有变量前面加上uu.Ex:\u sender
@DBM,或者养成遵循StyleCop指南的习惯,该指南建议始终通过this.***
为成员访问设置前缀。