在vs 2003中,我欲实现发送邮件功能,现在出现下面的问题:我用163邮箱可以实现正常发送邮件,但是我用126邮箱就会报错。
下面是 163邮箱时的代码:
//设置mailMsg服务器地址,用户名,密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码"); //下面设置一下发送邮件的服务器
try
{
SmtpMail.SmtpServer = "smtp.163.com";
SmtpMail.Send(mailMsg);
Label1.Text="发送成功!";
}
catch
{
Label1.Text = "发送失败";
}下面是 126邮箱时的代码:
//设置mailMsg服务器地址,用户名,密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码1"); //下面设置一下发送邮件的服务器
try
{
SmtpMail.SmtpServer = "smtp.126.com";
SmtpMail.Send(mailMsg);
Label1.Text="发送成功!";
}
catch
{
Label1.Text = "发送失败";
}用126邮箱时,出现下面的错误“发送失败未能访问“CDO.Message”对象。”,请问这是什么原因啊?
下面是 163邮箱时的代码:
//设置mailMsg服务器地址,用户名,密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码"); //下面设置一下发送邮件的服务器
try
{
SmtpMail.SmtpServer = "smtp.163.com";
SmtpMail.Send(mailMsg);
Label1.Text="发送成功!";
}
catch
{
Label1.Text = "发送失败";
}下面是 126邮箱时的代码:
//设置mailMsg服务器地址,用户名,密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码1"); //下面设置一下发送邮件的服务器
try
{
SmtpMail.SmtpServer = "smtp.126.com";
SmtpMail.Send(mailMsg);
Label1.Text="发送成功!";
}
catch
{
Label1.Text = "发送失败";
}用126邮箱时,出现下面的错误“发送失败未能访问“CDO.Message”对象。”,请问这是什么原因啊?
解决方案 »
- String="3jfjfiid99999999lklkjopi8"怎样截取opi8
- 安装SQL sever出现的问题
- 为什么使用AJAX后台代码只执行一次
- asp.net ajax的速度问题????最后10分了!
- 防页面后退 在线等
- 输出到EXCELL的格式问题
- 在datagrid中,如何把cancel按钮取消
- 最近在学习asp.net,请ZZ们进来。厚道散分中。。。。。。。
- 问谁有样式表的详细说明?
- sql server 语句错误?为什么删除不了纪录?
- 为CreateUserWizard控件增加注册内容 ,第一步<Wizardsteps> 第2步<CreateUserStep> 如何给第一步的继续按钮添加一个客户端验证js?
- asp.net前端得到代码函数的返回值问题
using System.Net;
using System.Net.Mail;
方法:
public bool ShowPost()//gmail的发送方法
{
string from = "[email protected]";
string pwd = "7420861467wang";
string to = TxtTo.Text.Trim();
MailMessage mail = new MailMessage(from,to);
mail.Subject = TxtHead.Text;
mail.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
mail.Body = TxtBody.Text;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
mail.IsBodyHtml = true;
string server = "";
int port = 25;
if (from.IndexOf("@gpres")!=-1 || from.IndexOf("@gmail")!=-1)
{
port = 587;
server = "smtp.gmail.com";
}
else
{
port = 25;
string temp;
temp=from.Substring(from.LastIndexOf('@')+1);
server = "smtp." + temp;
}
SmtpClient sc = new SmtpClient(server, port);
if (port!=25)
{
sc.EnableSsl = true;
}
sc.Credentials = new NetworkCredential(from, pwd);
try
{
sc.Send(mail);
mail.Dispose();
return true;
}
catch(Exception ex)
{
//System.Web.HttpContext.Current.Response.Write(e.Message);
mail.Dispose();
return false;
}
}
首先你要看你拿到的126的这个smtp服务器支不支持用程序发送邮件。很多邮件箱的服务器是禁止的,担心你利用它的邮箱群发垃圾邮箱。如果你能用163的发,代码应该没有问题。查下126的邮箱可不可以发吧。
你只能再注册一个其他邮件商的邮箱地址测试了,如果能发,那说明126的服务器不能发。像21cn,sohu.com,sina.com,这些邮箱好像都可以发,只是一天不能发太多,发太多它就认为你发的是垃圾邮件了。