2012-03-19 68 views
0

我正在开发一个使用SmtpClient发送电子邮件的应用程序,效果很好,但我有一个关于用户验证的问题,因为我看到的代码,只能知道电子邮件和电子邮件发送时密码正确。在发送邮件之前登录帐户

以前有没有办法做这个认证。我用这个作为一个例子:

public void SendMessage() 
{ 
    MailMessage mensagem = new MailMessage(); 
    mensagem.From = new MailAddress(email); 
    mensagem.To.Add(txtto.Text); 
    mensagem.Subject = txtsubject.Text; 
    mensagem.Body = richMessage.Text; 

    SmtpClient smtp = new SmtpClient(); 

    if (Form1.servidor.Equals("hotmail.com")) 
    { 
     smtp.Host = "smtp.live.com"; 
     smtp.Port = 25; 
     smtp.EnableSsl = true; 
    } 

    smtp.Credentials = new System.Net.NetworkCredential(email, Form1.password); 

    //only in this line is actually verified the existence of the user + password 
    smtp.Send(mensagem);  
} 

我不想要发送消息,知道用户名+密码是否正确。任何人都知道,也许另一个命名空间...

谢谢。

回答

0

您可以使用模拟,例如,用this the small impersonation class尝试登录Windows用户。

I.e.请将以下using块在你的代码给其他用户帐户下运行:

using (new Impersonator("myUsername", "myDomainname", "myPassword")) 
{ 
    ... 

    <code that executes under the new context> 

    ... 
} 

你可以try...catch各地使用块来检查错误的密码,或者更好的,使用Impersonator的源里面的功能登录并检查结果,即不捕捉(性能)。

+0

但我不想登录Windows用户,我想登录电子邮件。你知道另一种方式吗? – Aime 2012-03-20 12:49:20

相关问题