我在书里看到写发email的方法:
Sub SendEmailAlert(ByVal strOutlookId As String, ByVal strName As String, ByVal cryNewPass As String)
Dim sFrom As String
Dim sTO As String
Dim sBody As String
Dim sSubj As String
Dim oMail As New MailMessage()
Dim DeCryKey As String DeCryKey = DeCrypt(cryNewPass, "abcde") sFrom = "[email protected]"
sTO = strOutlookId & "@singtel.com"
'sSubj = "ISSR Password Created : "
sBody = "Dear " & strName & "," & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "Your ISSR application UserID and Password has been created."
sBody = sBody & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "ISSR UserID : " & strOutlookId
sBody = sBody & Chr(13)
sBody = sBody & "ISSR Password : " & DeCryKey
sBody = sBody & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "Please change your password on your first login."
sBody = sBody & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "Best Regards," & Chr(13)
sBody = sBody & "ISSR Admin" oMail = Server.CreateObject("CDO.MESSAGE")
With oMail
.BodyFormat = MailFormat.Html
.From = sFrom
.To = sTO
.Subject = sSubj
.Body = sBody
End With
SmtpMail.Send(oMail)
oMail = Nothing
End Sub
Sub SendEmailAlert(ByVal strOutlookId As String, ByVal strName As String, ByVal cryNewPass As String)
Dim sFrom As String
Dim sTO As String
Dim sBody As String
Dim sSubj As String
Dim oMail As New MailMessage()
Dim DeCryKey As String DeCryKey = DeCrypt(cryNewPass, "abcde") sFrom = "[email protected]"
sTO = strOutlookId & "@singtel.com"
'sSubj = "ISSR Password Created : "
sBody = "Dear " & strName & "," & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "Your ISSR application UserID and Password has been created."
sBody = sBody & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "ISSR UserID : " & strOutlookId
sBody = sBody & Chr(13)
sBody = sBody & "ISSR Password : " & DeCryKey
sBody = sBody & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "Please change your password on your first login."
sBody = sBody & Chr(13)
sBody = sBody & Chr(13)
sBody = sBody & "Best Regards," & Chr(13)
sBody = sBody & "ISSR Admin" oMail = Server.CreateObject("CDO.MESSAGE")
With oMail
.BodyFormat = MailFormat.Html
.From = sFrom
.To = sTO
.Subject = sSubj
.Body = sBody
End With
SmtpMail.Send(oMail)
oMail = Nothing
End Sub
oMail = Server.CreateObject("CDO.MESSAGE")这里的时候就报错:
System.InvalidCastException: Specified cast is not valid.
是不是因为我的server 上没有 SMTP Service?
如果一定要安装的话,怎么安装?我是xp professional 的操作系统。
在系统目录(如c:\winnt或c:\windows)的system32子目录中可以找到一个名称为cdosys.dll的文件,我们可以通过ASP.NET调用此COM组件来实现Email的发送。cdosys构建在SMTP协议和NNTP协议之上,并且作为Windows2000
Server的组件被安装,当然我们也可以使用Exchange2000中cdoex.dll来实现发送邮件的机制。由于cdosys.dll自动内嵌到了操作系统中,所以不用再去注册相应的其他发送程序,下面我们来做一个发送实例。
1、新建一个项目文件
2、添加引用系统目录下的cdosys.dll文件,在引用中会发现添加了两个要用到的接口:CDO,ADODB
3、添加新项文件SendMail.aspx,在其页面上放置三个Label,三个Textbox,作用分别为收件人地址、主题、内容,放置一个Button按钮。
4、切换到代码页,创建一下内容
public void CDOsendmail()
{
try
{
CDO.Message Msg = new CDO.Message();
Msg.From = "[email protected]";
Msg.To = this.TextBox1.Text.Trim();
Msg.Subject = this.TextBox2.Text.Trim();
Msg.HTMLBody = "<html><body>"+this.TextBox3.Text
+"</body></html>";
CDO.IConfiguration Config = Msg.Configuration;
ADODB.Fields oFields = Config.Fields;
oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value
= 2;
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="rattlesnake";
oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="pass";
oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;
oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.263.net";
oFields.Update();
Msg.BodyPart.Charset = "gb2312";
Msg.HTMLBodyPart.Charset = "gb2312"; Msg.Send();
Msg = null;
}
catch(Exception err)
{
throw err;
}
}
5、为Button添加Click事件
private void Button1_Click(object sender, System.EventArgs e)
{
this.CDOsendmail();
}//打包发送
//MailMessage mail = new MailMessage();
//mail.To = "[email protected]";
//mail.From = "[email protected]";
//mail.Subject = "this is a test email.";
//mail.Body = "Some text goes here";
//
//mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
//mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username"); //set your username here
//mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");//set your password here
//
//SmtpMail.SmtpServer = "smtp.sohu.com";//your real server goes here
//
//SmtpMail.Send( mail );
可不可以详细解释一下
如何添加引用系统目录下的cdosys.dll文件,在引用中会发现添加了两个要用到的接口:CDO,ADODB?
{
MailMessage MailObj = new MailMessage();
MailObj.From = FromMail;
MailObj.To = ToMail;
MailObj.Subject = Subject;
MailObj.Body = Body;
if(isHtml)
{
MailObj.BodyFormat = MailFormat.Html;
}
else
{
MailObj.BodyFormat = MailFormat.Text;
}
MailObj.Priority = MailPriority.High;
SmtpMail.Send(MailObj);
}
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
'设置你的发送地址
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","[email protected]")
'设置你的发送密码
mymail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "pwd")
'设置发送方的smtp服务器地址,下面以163的为例
SmtpMail.SmtpServer = "smtp.163.com"
SmtpMail.Send(MyMail)
还是不可以,错误仍然在
mail.To = "[email protected]";
mail.From = "[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "Some text goes here";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "user"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password"); //set your password here SmtpMail.SmtpServer = "mail.163.com"; //your real server goes here
SmtpMail.Send( mail );
不过你先要把你的计算机的IIS安装好才行原程序如下:
<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">
Sub SendButton_Click(sender as Object, e as EventArgs)
'创立一个 MailMessage 对象
Dim MailObj as New MailMessage()
'以下是设定MailMessage 对象的属性,及程序的一些常规判断。
'设定邮件的发送地址
if EmailFrom.text<>"" then MailObj.From = EmailFrom.text
'设定邮件的目的地址
if EmailTo.text<>"" then MailObj.To = EmailTo.text
'设定邮件的抄送地址
if EmailCc.text<>"" then MailObj.Cc = EmailCc.text
'设定邮件的密送地址
if EmailBcc.text<>"" then MailObj.Bcc = EmailBcc.text
'设定邮件格式是文本格式,如果要设定成超文本,把MailFormat.Text改成 MailFormat.Html
MailObj.BodyFormat = MailFormat.Text
'设定邮件优先级,可为 High(高), Low(低), Normal(普通)
MailObj.Priority = MailPriority.Normal
'设定邮件主题
MailObj.Subject = EmailSubject.text
'设定邮件内容
MailObj.Body = EmailBody.text
'给邮件增加一个附件
Dim strFileName as string
strFileName=Emailfile.PostedFile.FileName
if strFileName<>"" then MailObj.Attachments.Add(new MailAttachment(strFileName))
'指定使用缺省的SMTP服务器
SmtpMail.SmtpServer = ""
'现在开始发送邮件
SmtpMail.Send(MailObj)
panelSendEmail.Visible = false
panelMailSent.Visible = true
End Sub
</script>
<html>
<body>
<asp:panel id="panelSendEmail" runat="server">
<form Method="Post" EncType="Multipart/Form-Data" runat="server">
<h2>欢迎用ASP.NET来发送E-mail</h2>
<b>请输入邮件发送地址:</b>
<asp:textbox id="EmailFrom" size="30" runat="server" />
<p>
<b>请输入邮件目的地址:</b>
<asp:textbox id="EmailTo" size="30" runat="server" />
<p>
<b>请输入邮件抄送地址:</b>
<asp:textbox id="EmailCc" size="30" runat="server" />
<p>
<b>请输入邮件密送地址:</b>
<asp:textbox id="EmailBcc" size="30" runat="server" />
<p>
<b>请输入邮件主题:</b>
<asp:textbox id="EmailSubject" size="30" runat="server" />
<p>
<b>请输入邮件主体:</b>
<asp:textbox id="EmailBody" TextMode="MultiLine"
Columns="40" Rows="10" runat="server" />
<p>
<b>请加入附件名称:</b>
<input id="Emailfile" type="file" runat="server" size="40" />
<asp:button runat="server" id="SendButton" Text="发送"
OnClick="SendButton_Click" />
</form>
</asp:panel>
<asp:panel id="panelMailSent" runat="server" Visible="False">
您的邮件已经成功发送,欢迎您的再次使用!
</asp:panel>
</body>
</html>
Exception Details: System.Runtime.InteropServices.COMException: The "SendUsing" configuration value is invalid. [COMException (0x80040220): The "SendUsing" configuration value is invalid.]
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) +0
System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) +473
System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args) +29
System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type, Object obj, String methodName, Object[] args)
System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)[HttpException (0x80004005): Could not access 'CDO.Message' object.]
System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
System.Web.Mail.CdoSysHelper.Send(MailMessage message)
System.Web.Mail.SmtpMail.Send(MailMessage message)
latte.confirm.SendEmailAlert(String strOutlookId, String strName, String cryNewPass) in C:\Inetpub\wwwroot\latte\admin\confirm.aspx.vb:264
latte.confirm.fncUpdatetable() in C:\Inetpub\wwwroot\latte\admin\confirm.aspx.vb:201
latte.confirm.Restpsd_Proc(String strOutlookId) in C:\Inetpub\wwwroot\latte\admin\confirm.aspx.vb:156
latte.confirm.imgYes_Click(Object sender, ImageClickEventArgs e) in C:\Inetpub\wwwroot\latte\admin\confirm.aspx.vb:90
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1263
下载:http://www.aspxcontrol.com