asp.net 实现发邮件出现如下异常

jmbkeyes168 2005-11-19 09:14:48
System.Web.HttpException: 未能访问“CDO.Message”对象。 ---> System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---> System.Runtime.InteropServices.COMException (0x80040220): “SendUsing”配置值无效。 --- 内部异常堆栈跟踪的结尾 --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args) at System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type, Object obj, String methodName, Object[] args) at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) --- 内部异常堆栈跟踪的结尾 --- at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at MySite.mail.Image2_ServerClick(Object sender, ImageClickEventArgs e) in d:\visual studio.net 2003\aspnet\mysite\mail.aspx.cs:line 68
后台代码如下:
private void Image2_ServerClick(object sender, System.Web.UI.ImageClickEventArgs e)
{
MailMessage myEmailMessage=new MailMessage();
myEmailMessage.From=Request.Form["fayoujianzhe"].ToString();
myEmailMessage.To=Request.Form["shouyoujianzhe"].ToString();
myEmailMessage.Subject=Request.Form["youjianzhuti"].ToString();
myEmailMessage.Body=Request.Form["youjianneirong"].ToString();
myEmailMessage.BodyFormat=MailFormat.Text;
myEmailMessage.Priority=MailPriority.High;
SmtpMail.SmtpServer="smtp.mail.com";//发送电子邮件的SMTP服务器的IP地址或域名 :68行
try
{
SmtpMail.Send(myEmailMessage);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}

}
...全文
146 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
brightview 2005-11-20
  • 打赏
  • 举报
回复
一个通过web.Mail发送邮件的类

using System;
using System.Web;
using System.Web.Mail;
using Dottext.Framework;
using Dottext.Framework.Configuration;

namespace YourNamespace.Email
{
/// <summary>
/// Default implementation of the IMailProvider
/// </summary>
public class SystemMail : IMailProvider
{
public SystemMail(){}

#region
private string _to;
public string To
{
get{return _to;}
set{_to = value;}
}

private string _from;
public string From
{
get{return _from;}
set{_from = value;}
}

private string _subject;
public string Subject
{
get{return _subject;}
set{_subject = value;}
}

private string _body;
public string Body
{
get{return _body;}
set{_body = value;}
}
#endregion

private string _adminEmail;
public string AdminEmail
{
get{return _adminEmail;}
set{_adminEmail = value;}
}

private string _smtpServer = "localhost";
public string SmtpServer
{
get{return _smtpServer;}
set{_smtpServer = value;}
}

private string _password;
public string Password
{
get{return _password;}
set{_password = value;}
}

private string _userName;
public string UserName
{
get{return _userName;}
set{_userName = value;}
}

public bool Send(string to, string from, string subject, string message)
{
try
{
MailMessage em = new MailMessage();
em.To = to;
em.From = from;
em.Subject = subject;
em.Body = message;

//Found out how to send authenticated email via System.Web.Mail at http://SystemWebMail.com (fact 3.8)
if(this.UserName != null && this.Password != null)
{
em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.UserName); //set your username here
em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.Password); //set your password here

}

SmtpMail.SmtpServer = this.SmtpServer;
SmtpMail.Send(em);

return true;
}
catch
{
return false;
}
}


}
}

lsqkeke 2005-11-20
  • 打赏
  • 举报
回复
Follow your program's step,you fail to send this mail.
Because you wouldn't pass the smtp server's validation.
You must write some more code to attain this job
da18a 2005-11-20
  • 打赏
  • 举报
回复
MailMessage myEmailMessage=new MailMessage();
myEmailMessage.From=Request.Form["fayoujianzhe"].ToString();
myEmailMessage.To=Request.Form["shouyoujianzhe"].ToString();
myEmailMessage.Subject=Request.Form["youjianzhuti"].ToString();
myEmailMessage.Body=Request.Form["youjianneirong"].ToString();
myEmailMessage.BodyFormat=MailFormat.Text;
myEmailMessage.Priority=MailPriority.High;
try
{
SmtpMail.Send(myEmailMessage);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
jmbkeyes168 2005-11-20
  • 打赏
  • 举报
回复
Yes,I want to use my local smtp server,but that Exception has happened.
Could you tell me how to deal with it???
活靶子哥哥 2005-11-20
  • 打赏
  • 举报
回复
http://aspxboy.com/11/category.aspx
活靶子哥哥 2005-11-20
  • 打赏
  • 举报
回复
引用CDO组件 CDOSYS.DLL
using System;
using System.Web.Mail;
using CDO;
namespace Greystar.Base.Office
{
/// <summary>
/// Email 的摘要说明。
/// </summary>
public class Email
{
public Email()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

#region SendEmailValidate (string Emailfrom,string Emailto,string subject,string mailbody)
/// <summary>
/// 要求有帐号
/// </summary>
/// <param name="Emailfrom"></param>
/// <param name="Emailto"></param>
/// <param name="subject"></param>
/// <param name="mailbody"></param>
public void SendEmailValidate (string Emailfrom,string Emailto,string subject,string mailbody)
{
string ErrString="";
try
{
Configuration conf=new Configuration();
//以下部分设置参数(服务器,端号,用户名,密码,发送邮件帐号,回复邮箱帐号)
conf.Fields[CdoConfiguration.cdoSendUsingMethod].Value=CdoSendUsing.cdoSendUsingPort;
conf.Fields[CdoConfiguration.cdoSMTPServer].Value="server-mail";
conf.Fields[CdoConfiguration.cdoSMTPServerPort].Value=25;
conf.Fields[CdoConfiguration.cdoSMTPAccountName].Value="greystar";
conf.Fields[CdoConfiguration.cdoSendUserReplyEmailAddress].Value="\"greystar\" <greystar@oceansoft.com.cn>";
conf.Fields[CdoConfiguration.cdoSendEmailAddress].Value="\"greystar\" <greystar@oceansoft.com.cn>";
conf.Fields[CdoConfiguration.cdoSMTPAuthenticate].Value=CdoProtocolsAuthentication.cdoBasic;
conf.Fields[CdoConfiguration.cdoSendUserName].Value="greystar";
conf.Fields[CdoConfiguration.cdoSendPassword].Value="liaoliao";

conf.Fields.Update();
MessageClass msg=new MessageClass();

msg.Configuration=conf;

msg.To=Emailto;
msg.Subject= subject;
msg.HTMLBody=mailbody;
msg.From=Emailfrom;

msg.Send();
ErrString="确认成功";
}
catch(System.Runtime.InteropServices.COMException Ee)
{
ErrString=Ee.Message;
throw new Exception(ErrString);
}
}
#endregion

#region SendMail(string MailFrom,string MailTo,string MailSubject,string MailBody,string MailServer )
/// <summary>
/// 匿名访问
/// </summary>
/// <param name="MailFrom"></param>
/// <param name="MailTo"></param>
/// <param name="MailSubject"></param>
/// <param name="MailBody"></param>
/// <param name="MailServer"></param>
public void SendMail(string MailFrom,string MailTo,string MailSubject,string MailBody,string MailServer )

{

MailMessage Msg = new MailMessage();

Msg.To = MailTo;

Msg.Subject = MailSubject;

Msg.Body = MailBody;

Msg.From =MailFrom;

SmtpMail.SmtpServer=MailServer;

SmtpMail.Send(Msg);

return;

}
#endregion


}
}


jmbkeyes168 2005-11-20
  • 打赏
  • 举报
回复
I'm a beginner of asp.net ,and do not know how to deal with it??
could you help me ???
kisshere 2005-11-19
  • 打赏
  • 举报
回复
SmtpMail.SmtpServer="smtp.mail.com"
where is "smtp.mail.com"?
if you use your local smtp server, you can comment this line.

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧