利用SmtpClient,smtp为服务器发送邮件.
当我把服务器名称,发送邮件的用户名和密码写到配置文件里.
通过配置文件读取的时候会出现超时的问题.当我直接写到方法里面的时候,就不会超时.为什么呢 ?
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="server">服务器stmp.163.com</param>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <param name="toaddr">发送地址</param>
/// <param name="titel">标题</param>
/// <param name="body">内容</param>
public static void sendmail(string server, string username, string password, string toaddr, string titel, string body)//,string path)
{
try
{
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = server;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(username, password);
//星号改成自己邮箱的密码
client.DeliveryMethod = SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new MailMessage(username, toaddr, titel, body);
message.BodyEncoding = System.Text.Encoding.Default;
message.IsBodyHtml = true; //添加附件
//if (path != "" || path != null)
//{
// Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
// message.Attachments.Add(data);
//}
client.Send(message);
}
catch
{ }
}
当我把服务器名称,发送邮件的用户名和密码写到配置文件里.
通过配置文件读取的时候会出现超时的问题.当我直接写到方法里面的时候,就不会超时.为什么呢 ?
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="server">服务器stmp.163.com</param>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <param name="toaddr">发送地址</param>
/// <param name="titel">标题</param>
/// <param name="body">内容</param>
public static void sendmail(string server, string username, string password, string toaddr, string titel, string body)//,string path)
{
try
{
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = server;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(username, password);
//星号改成自己邮箱的密码
client.DeliveryMethod = SmtpDeliveryMethod.Network; System.Net.Mail.MailMessage message = new MailMessage(username, toaddr, titel, body);
message.BodyEncoding = System.Text.Encoding.Default;
message.IsBodyHtml = true; //添加附件
//if (path != "" || path != null)
//{
// Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
// message.Attachments.Add(data);
//}
client.Send(message);
}
catch
{ }
}
解决方案 »
- ASP.NET 支付宝问题
- asp.net中不重新发送信息,则无法刷新网页的错,求救命~!
- 用window.open('_self')打开一个新页面后如何用代码回到母页面
- 看似简单的问题:MyClass myClass = new MyClass(); 如何让myClass.Name等于"myClass"?
- ·用Server.MapPath获取的路径会出现“\\”?
- 为什么freetextbox工具栏图片按钮没显示
- 在网页上显示PDF文件,大家帮下我,谢谢
- 请问到底是程序问题还是空间商权限问题?
- 什么是MVC模式?用ASP.net的codebehind方式开发出来的系统算是MVC模式吗?
- 求助!!! XML 文档读取问题
- 新手问XML问题..
- IIS问题?
public void MailSend(string MailFrom,string MailTo,string MailPwd,string Mailtitle,string MailCon)
{
MailMessage MyMail = new MailMessage();
MyMail.From = new MailAddress("", "");
MyMail.To.Add(new MailAddress(""));
MyMail.Subject = Mailtitle;
MyMail.Body = MailCon;
MyMail.IsBodyHtml = true;
SmtpClient smtpclient = new SmtpClient();
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Host = "";
smtpclient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
smtpclient.Send(MyMail);
}
jmail.Message Jmail = new jmail.Message();
DateTime t = DateTime.Now;
String Subject = "";
String body = "";
String FromEmail = "";
String ToEmail = "";
Jmail.Charset = "GB2312";
Jmail.ContentType = "text/html";
Jmail.AddRecipient(ToEmail, "", "");
Jmail.From = FromEmail;
Jmail.MailServerUserName = "";
Jmail.MailServerPassWord = "";
Jmail.Subject = Subject;
Jmail.ContentType="text/html";
Jmail.Body = body + t.ToString();
Jmail.Send("", false);
Jmail.Close(); message.Body ="";
client.Send(message);
可能是client.Host = server;设置的地址不对。。