62,072
社区成员
发帖
与我相关
我的任务
分享
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.53.109:25
/// <summary>
/// 用于发邮件的方法
/// </summary>
/// <param name="username">显示的发件人名称</param>
/// <param name="subject">显示的主题名称</param>
/// <param name="toname">收件人邮箱号码</param>
/// <param name="body">内容</param>
public static void SendMessage(string username, string subject, string toname, string body)
{
MailAddress from = new MailAddress("wen.2.0.0.8@163.com", username);
MailMessage mail = new MailMessage();
mail.Subject = subject;
mail.From = from;
//设置邮件的收件人
string address = "";
string displayName = "";
string[] mailNames = (toname + ";").Split(';');
foreach (string name in mailNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
displayName = name.Substring(0, name.IndexOf('<'));
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
displayName = string.Empty;
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
mail.To.Add(new MailAddress(address, displayName));
}
}
//设置邮件的内容
mail.Body = body;
//设置邮件的格式
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
//设置邮件的发送级别
mail.Priority = MailPriority.Normal;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
SmtpClient client = new SmtpClient();
//设置用于 SMTP 事务的主机的名称,填IP地址也可以了
client.Host = "smtp.163.com";
//设置用于 SMTP 事务的端口,默认的是 25
//client.Port = 25;
client.UseDefaultCredentials = false;
//这里才是真正的邮箱登陆名和密码,比如我的邮箱地址是 hbgx@hotmail, 我的用户名为 hbgx ,我的密码是 xgbh
client.Credentials = new System.Net.NetworkCredential("邮箱帐号", "密码");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//都定义完了,正式发送了,很是简单吧!
client.Send(mail);
}
string strMailFrom = ConfigurationManager.AppSettings["mailFrom"];
string strMailFromPass = ConfigurationManager.AppSettings["MailFromPass"];
string strSmtpServer = ConfigurationManager.AppSettings["SmtpServer"];
SmtpClient client = new SmtpClient(strSmtpServer); //邮件发送类
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(strMailFrom, strMailFromPass);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
string strSubject = string.Empty;
System.Text.StringBuilder strBody = new System.Text.StringBuilder();
strBody.Append("From: " + context.Request["frm_name"] + " <br />");
strBody.Append("EMail: " + context.Request["frm_email"] + " <br />");
//strBody.Append("Subject: " + context.Request["frm_subject"] + " <br />");
strSubject = context.Request["frm_subject"];
strBody.Append("Message: " + context.Request["frm_message"] + " <br />");
MailMessage message = null;
message = new MailMessage(strMailFrom, strMailTo, strSubject, strBody.ToString());
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
try
{
client.Send(message);
}
catch (Exception ex)
{
context.Response.Write(ex.ToString());
}