ASP.NET中发送Email完整实例(转)
2024-07-10 12:58:14
供稿:网友
asp.net中发送email完整实例
本文举例说明在asp.net中发送email的众多可能性,内容覆盖了诸如email格式、优先权、附件及email编码等方面。
asp.net被赋予了一个发送email的新对象,名为smtpmail。使用smtpmail对象从asp.net页面中发送email时,可以遵循以下简单步骤:
▲包含与邮件有关类所需要的名称空间;
▲例示一个信息对象,设置属性;
▲使用smtpmail对象实例的send方法发送邮件。
现在我们就来一步一步地研究从一个asp.net页面发送email的过程。我们使用了vb来说明这个例子,最后将包含vb和c#的完整代码。
第一步:包含名称空间
在asp.net 页面中引入system.web.util 名称空间,这个名称空间中包括了发送一个email所必须的所有对象。这些对象是:
smtpmail:代表邮件系统,用于发送email。
mailmessage:代表一个信息,其属性包括发件人地址、收件人地址等。
mailformat:代表信息的格式:html、文本等。
mailattachment:代表一个email附件。
mailencoding enum:代表base64 或uuencode的任何编码。取值范围:base64、uuencode
mailpriority enum:用来为信息设置优先权。值为:高、低、一般。
<% @import namespace = "system.web.util" %>
第二步:例示 mailmessage 对象
使用以下语句来例示mailmessage对象:
dim mailobj as new mailmessage
用mailmessage对象的属性来准备邮件。mailmessage对象有下列属性:
from:发件人的email地址
to:收件人的email地址
subject:email的主题
body:email的主体
cc:email抄送的收件人列表
bcc:email暗送的收件人列表
priority:信息的优先权:高、低或一般
bodyencoding:信息体的编码,如果有的话,就是base64或uuencode
bodyformat:信息的格式:html 或text
attachments:附加到email 的mailattachment对象列表,主要就是对这个对象集合的一个引用
下面这段代码示范了使用mailmessage 对象属性的方法,它们代表了将在本例中创建的一个信息,这个信息要用smtpmail对象来发送。在例子中,mailobj引用了信息对象的例示:
mailobj.from = "[email protected]"
mailobj.to = request.form ("to")
mailobj.subject = "subject of the mail"
mailobj.body = "message of the mail"
第三步:发送email
这时,我们就可以使用smtpmail 对象的send方法来发送邮件了:
smtpmail.send(mailobj)
完整实例
最后,我们把以上解释的属性结合在一个完整的例子中。为了说明用asp.net 发送一个email 的全部可能性,我们还包含了一些“小技巧”。下面是使用vb.net的完整例子:
<%@page language="vb" %>
<%@import namespace="system.web.util" %>
<html><body>
<script language="vb" runat="server">
' this method is called on the server when the submit
' button is clicked on the client and when the page
' posts back to itself
sub sendmail (obj as object, e as eventargs)
' instantiate a mailmessage object. this serves as a message object
' on which we can set properties.
dim mailobj as new mailmessage
' set the from and to address on the email
mailobj.from = request.form("from")
mailobj.to = request.form("to")
mailobj.subject = "subject of the mail"
mailobj.body = "body of the mail"
' optional: html format for the email
mailobj.bodyformat = mailformat.html
' optional: encoding for the message
mailobj.bodyencoding = mailformat.base64
' optional: set the priority of the message to high
mailobj.priority = mailpriority.high
' optional: attach a file to the email.
' note here that we have created a mailattachment object to
' attach a file to the email
mailobj.attachments.add(new mailattachment("c:/test.doc"))
' send the email using the smtpmail object
smtpmail.send(mailobj)
end sub
</script>
<asp:label id="headingmsg" text="enter your email address:" runat="server"/>
<form method="post" runat="server">
email recipient: <input type="text" name="to"> <br>
email sender: <input type="text" name="from">
<input type="submit" name="submit" value="send mail" runat="server" onserverclick="sendmail">
</form>
</body>
在以上例子中,from(发件人)和 to(收件人)的email地址是从相应的文本框中收集的,点击“send mail”(发送邮件)按钮时,邮件就被发送出去。当“send mail”(发送邮件)按钮被点击时,表单回递到它自己,在服务器上“sendmail”(发送邮件)程序被触发,邮件被发送。下面是使用c#的例子:
<%@page language="c#" %>
<%@import namespace="system.web.util" %>
<html><body>
<script language="c#" runat="server">
// this method is called on the server when the submit
// button is clicked on the client and when the page
// posts back to itself
public void sendmail (object obj, eventargs e)
{
// instantiate a mailmessage object. this serves as a message object
// on which we can set properties.
mailmessage mailobj = new mailmessage();
// set the from and to address on the email
mailobj.from = request.form("from");
mailobj.to = request.form("to");
mailobj.subject = "subject of the mail";
mailobj.body = "body of the mail";
// optional: html format for the email
mailobj.bodyformat = mailformat.html;
// optional: encoding for the message
mailobj.bodyencoding = mailformat.base64;
// optional: set the priority of the message to high
mailobj.priority = mailpriority.high;
// optional: attach a file to the email.
// note here that we have created a mailattachment object to
// attach a file to the email
mailobj.attachments.add(new mailattachment("c://test.doc"));
// send the email using the smtpmail object
smtpmail.send(mailobj);
}
</script>
<asp:label id="headingmsg" text="enter your email address:" runat="server"/>
<form method="post" runat="server">
email recipient: <input type="text" name="to"> <br>
email sender: <input type="text" name="from">
<input type="submit" name="submit" value="send mail" runat="server" onserverclick="sendmail">
</form>
</body>