C#中用SMTP发邮件怎么写服务验证这块呀?
我用C#写了一个服务,在发信时发现:SMTP服务器需要SMTP验证,但 SmtpClient()类好像没有用户ID和密码和属性,MSDN说:
可以使用应用程序或计算机配置文件来指定所有 SmtpClient 对象的默认主机、端口和凭据值。有关更多信息,请参见 mailSettings 元素(网络设置)。
但我写的是Windows 服务,没有配置文件呀,应该怎么办?
------解决方案--------------------我已经找到了
SmtpClient client = new SmtpClient(smtpserver);
client.Credentials = new NetworkCredential(fromMail, password);
这样就可以了
揭帖吧。哈
------解决方案--------------------public class MailHelper
{
String EmployeeAlias = String.Empty;
public MailHelper(Object Alias)
{
EmployeeAlias = Alias.ToString();
}
/// <summary>
/// Fill the MailMessage Entity
/// </summary>
/// <returns> </returns>
private MailMessage InitMailEntity()
{
MailMessage Message = new MailMessage();
MailAddress mailFrom = new MailAddress(ConfigurationSettings.AppSettings[ "mailform "].ToString());
Message.From = mailFrom;
Message.To.Add(new MailAddress(EmployeeAlias + ConfigurationSettings.AppSettings[ "mail "].ToString()));
Message.Subject = ConfigurationSettings.AppSettings[ "mailsubject "].ToString();
Message.Body = ConfigurationSettings.AppSettings[ "mailbody "].ToString();
Message.IsBodyHtml = true;
return Message;
}
/// <summary>
/// Send Mail
/// </summary>
public void SendMail()
{
try
{
SmtpClient client = new SmtpClient(ConfigurationSettings.AppSettings[ "smtpclient "].ToString(), 25);
client.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings[ "user "].ToString(), ConfigurationSettings.AppSettings[ "password "].ToString(), ConfigurationSettings.AppSettings[ "domain "].ToString());
client.Send(InitMailEntity());
}
catch (SmtpException e)
{
throw e;
}
}