发送邮件失败,无法加载或初始化请求的服务提供程序解决方案
发送邮件失败,无法加载或初始化请求的服务提供程序
1.错误提示:
System.Net.Mail.SmtpException: 发送邮件失败。 ---> System.Net.Sockets.SocketException: 无法加载或初始化请求的服务提供程序。
在 System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
在 System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
在 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
在 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
在 System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
在 System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
在 System.Net.Mail.SmtpClient.GetConnection()
在 System.Net.Mail.SmtpClient.Send(MailMessage message)
--- 内部异常堆栈跟踪的结尾 ---
2.使用window2088 r2操作系统;vs2008开发工具
------解决方案--------------------
public static bool SendEmail(string mailSubject, string mailContent)
{
// 设置发送方的邮件信息,例如使用网易的smtp
string smtpServer = "smtp.qq.com"; //SMTP服务器
string mailFrom = "ponse@qq.com"; //登陆用户名
string userPassword = "test123";//登陆密码
string mailTo = "test@qq.com";
// 邮件服务设置
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
// 发送邮件设置
MailMessage mailMessage = new MailMessage(mailFrom, mailTo); // 发送人和收件人
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailContent;//内容
mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = MailPriority.Low;//优先级
try
{
smtpClient.Send(mailMessage); // 发送邮件
return true;
}
catch (SmtpException ex)
{
string str=ex.ToString();
return false;
}
}
1.错误提示:
System.Net.Mail.SmtpException: 发送邮件失败。 ---> System.Net.Sockets.SocketException: 无法加载或初始化请求的服务提供程序。
在 System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
在 System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
在 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
在 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
在 System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
在 System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
在 System.Net.Mail.SmtpClient.GetConnection()
在 System.Net.Mail.SmtpClient.Send(MailMessage message)
--- 内部异常堆栈跟踪的结尾 ---
2.使用window2088 r2操作系统;vs2008开发工具
------解决方案--------------------
public static bool SendEmail(string mailSubject, string mailContent)
{
// 设置发送方的邮件信息,例如使用网易的smtp
string smtpServer = "smtp.qq.com"; //SMTP服务器
string mailFrom = "ponse@qq.com"; //登陆用户名
string userPassword = "test123";//登陆密码
string mailTo = "test@qq.com";
// 邮件服务设置
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
// 发送邮件设置
MailMessage mailMessage = new MailMessage(mailFrom, mailTo); // 发送人和收件人
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailContent;//内容
mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = MailPriority.Low;//优先级
try
{
smtpClient.Send(mailMessage); // 发送邮件
return true;
}
catch (SmtpException ex)
{
string str=ex.ToString();
return false;
}
}