2011-11-04 200 views
1

我不知道为什么我得到这个错误。似乎是初级。无论如何,我有一个称为EmailSender的单例类。下面的代码很简单。问题是我无法在MainWindow类中使用发件人。任何我尝试的东西,如sender.Send()都被视为我已经完成了asdafsafas.Send()。它被看作是随机字符串。不知道为什么会发生这种情况。C#无法解析符号

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()); 
} 
+0

什么是确切的错误信息? – Vlad

+0

无法解析符号发件人。 当我编译我得到:无效的令牌'('在类,结构或接口成员声明\t在MainWindow.cs – s5s

+0

好吧,哪一行确实产生这个错误信息? – Vlad

回答

6

这是因为你必须定义这个方法的方式(发送端是一个参数)。它找到方法参数第一个,而不是您的类级变量。你可以限定这个:

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 
} 
+0

OK我明白了哦,我忘记了发件人对象,我正在修改我的C#和谢谢你。 – s5s

+0

@用户:一个简单的工作d是遵循样式约定并使用_前缀私有变量。例如:'_sender' –

+0

@DBM或者,也可以养成遵循StyleCop指导原则的习惯,该原则建议**始终**通过'this。***'前缀成员访问的前缀。 –

1

这是因为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("[email protected]");   

     //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   
} 

实现:

using (SmtpMailMessage mail = new SmtpMailMessage("192.168.0.1", 25)) 
{ 
    //set the to address to the primary email 
    mail.To.Add("[email protected]"); 

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

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

如果你这样做你的btnSend_Click方法的范围内,参数object sender优先于全球EmailSender sender

您应该重命名全局变量,如:EmailSender m_sender或明确指定你想要的发件人:this.sender.Send()

0

我想你可以调用sender.Send在btnSend_Click函数中。

该函数是一个也称为发送者(对象发送者)的参数。现在你的代码混淆了使用哪一个。所以重命名你的私人变种发件人