日期:2014-05-19  浏览次数:20937 次

C#WinForm中System.Net.Mail如何带用户名及密码连接服务器,解决即给分
使用MailMessage及SmtpClient来发送邮件,但不知道怎么样连接服务器
MailMessage   mailMsg   =   new   MailMessage();
SmtpClient   smtpClient   =   new   SmtpClient( "server ",25);

这个好像不带SMTP用户名及密码,如何使其带上验证,   多谢!


------解决方案--------------------
SmtpClient sc = new SmtpClient(_mailServer, _serverPort);
sc.UseDefaultCredentials = true;
sc.Credentials = new System.Net.NetworkCredential(_mailUsername, _mailPassword);
sc.EnableSsl = EnableSSL;

------解决方案--------------------
public static bool sendMail(string strSubject,string content,string eMailAddress) {
if (eMailAddress.Length < 2)
{
return false;
}
bool hasSend = true;
string strSMTPServer = ConfigurationManager.AppSettings[ "smtpAddress "];
string strSMTPUser = ConfigurationManager.AppSettings[ "smtpUser "];
string strSMTPPassword = ConfigurationManager.AppSettings[ "smtpPassword "];
string strFrom = strSMTPUser;
SmtpClient client = new SmtpClient(strSMTPServer);
client.Credentials = new NetworkCredential(strFrom, strSMTPPassword);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage message = new MailMessage();
message.From = new MailAddress(strFrom);
message.Subject = strSubject;
message.Body = content;
message.To.Add(eMailAddress);
message.SubjectEncoding = Encoding.UTF8;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
try
{
client.Send(message);
}
catch (Exception ex)
{
logWriter.writeLog(ex.Message, " ", " ", " ");
return false;
}
return hasSend;
}
------解决方案--------------------
直接根据ESMTP协议做吧,会话就几步。