<%@ page validateRequest=false %>
<%@ Import Namespace="System.Web.Mail"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
sub Page_Load
if not IsPostBack then
dim conn as Sqlconnection
dim cmd as SqlCommand
dim dtrMailto as SqlDataReader
conn = new SqlConnection("server=localhost;UID=sa;PWD=sa;DataBase=MailTest")
conn.open()
cmd = new SqlCommand("select email from MailTable",conn)
dtrMailto = cmd.ExecuteReader()
mailtoList.DataSource = dtrMailto
mailtoList.DataTextField ="email"
mailtoList.DataBind()
dtrMailto.close()
conn.close()
end if
end sub
sub Button_click(s as Object,e as EventArgs)
SmtpMail.Send(mailfrom.text,mailto.text,txtSubject.text,txtBody.text)
end sub
sub Button_Attachment_click(s as Object,e as EventArgs)
dim objMailMessage as MailMessage
dim objMailAttachment as MailAttachment
objMailAttachment = new MailAttachment(filename.PostedFile.FileName)
objMailMessage = new MailMessage
objMailMessage.From = mailfrom.text
objMailMessage.To = mailto.text
objMailMessage.Subject=txtSubject.text
objMailMessage.Body=txtBody.text
objMailMessage.Attachments.add(objMailAttachment)
SmtpMail.send(objMailMessage)
end sub
sub Button_html_click(s as Object,e as EventArgs)
dim objMailMessage as MailMessage
dim strContent as String
strContent = txtHtmlBody.text
objMailMessage = new MailMessage
objMailMessage.From = mailfrom.text
objMailMessage.To = mailto.text
objMailMessage.Subject=txtSubject.text
objMailMessage.Body=strContent
objMailMessage.BodyFormat = MailFormat.HTML
SmtpMail.send(objMailMessage)
end sub
sub Button_DB_click(s as Object,e as EventArgs)
dim objMailMessage as MailMessage
objMailMessage = new MailMessage
objMailMessage.From = mailfrom.text
objMailMessage.To = mailtoList.SelectedItem.text
objMailMessage.Subject=txtSubject.text
objMailMessage.Body=txtBody.text
SmtpMail.send(objMailMessage)
end sub
</script>
<html>
<body>
<h3><font color=green>发送邮件</font></h3>
<form runat="server">
<b>发件人:</b>
<asp:TextBox id="mailfrom" Columns="30" runat="server"/>
<br>
<b>收件人</b>
<asp:TextBox id="mailto" columns="30" runat="server"/>
<br><b>标 题:</b>
<asp:TextBox id="txtSubject" columns="30" runat="server"/>
<br><b>内 容:</b>
<asp:TextBox id="txtBody" TextMode="MultiLine" columns="30" runat="server"/>
<br>
<asp:Button text="发送" OnClick="Button_click" runat="server"/>
<br>
<b>收件人列表:</b><asp:DropDownList id="mailtoList" runat="server"/>
<br>
<asp:Button text="从数据库得收件人的发送" OnClick="Button_DB_click" runat="server"/>
<br><br>
<b>附 件:</b><input id="filename" type="File" runat="server"/>
<br>
<asp:Button text="带附件的发送" Onclick="Button_Attachment_click" runat="server"/>
<br><br>
<b>HTML内容:</b><asp:TextBox id="txtHtmlBody" TextMode="MultiLine" columns="30" runat="server"/>
<br>
<asp:Button text="发送HTML" OnClick="Button_html_click" runat="server"/>
</form>
</body>
</html>
运行结果:
此处没有指定SmtpMail.SmtpServer属性,所以使用的SMTP服务器就是本地的默认SMTP服务器,如果要使用其它的SMTP服务器可通过此属性进行设置.
本文地址:http://com.8s8s.com/it/it7916.htm