用asp.net发送电子邮件

    .net提供了强大的功能,其中就包括发送邮件上.经过仔细研究才发现用.net发送电子邮件是很简单的一件事情.现在我们就来看一下.
    首先添加命名空间
Using System.Net.Mail
下面我们看发送邮件的代码部分:(注意:网易的邮件服务器是smtp.163.com)
MailAddress from = new MailAddress(TBMailFrom.Text);
        MailAddress to 
= new MailAddress(TBMailTo.Text);
        MailMessage message 
= new MailMessage(from,to);
        message.Subject 
= TBSubject.Text;
        message.Body 
= TBDescript.Text;
        
if (FileUpload1.PostedFile.FileName != "")
        
{
            Attachment att 
= new Attachment(FileUpload1.PostedFile.FileName);
            message.Attachments.Add(att);
        }

        SmtpClient client 
= new SmtpClient("smtp.163.com");
smtp.Credentials 
= new System.Net.NetworkCredential("username""password");
        client.Send(message);

我们这里在发送邮件的时候只是设置了邮件收,发件人,邮件主题和邮件正文部门,在.net里面还可以设置发送邮件的文本格式,优先级等.我们这里就不说明了,相信看看就会明白的.(message.Priority;设置优先级.mssage.Headers;设置邮件的标头. message.CC;设置抄送.message.IsBodyHtml;设置是否以html格式发送邮件)
下面是对页面的设置:
 1<body>
 2    <form id="form1" runat="server">
 3    <div>
 4        <table style="width: 268px">
 5        <tr><td>
 6            <asp:Label ID="Label4" runat="server" Text="发件人:"></asp:Label></td>
 7            <td>
 8                <asp:TextBox ID="TBMailFrom" runat="server"></asp:TextBox></td></tr>
 9            <tr>
10                <td style="width: 101px">
11                    <asp:Label ID="Label1" runat="server" Text=" 收件人:"></asp:Label></td>
12                <td>
13                    <asp:TextBox ID="TBMailTo" runat="server"></asp:TextBox></td>
14            </tr>
15            <tr>
16                <td style="width: 101px">
17                    <asp:Label ID="Label2" runat="server" Text="邮件主题:"></asp:Label></td>
18                <td>
19                    <asp:TextBox ID="TBSubject" runat="server"></asp:TextBox></td>
20            </tr>
21            <tr><td colspan="2">
22                <asp:FileUpload ID="FileUpload1" runat="server" Width="259px" /></td>
23            </tr>
24            <tr>
25                <td colspan="2">
26                    <asp:Label ID="Label3" runat="server" Text="邮件正文:"></asp:Label></td>
27            </tr>
28            <tr>
29                <td colspan="2">
30                    <asp:TextBox ID="TBDescript" runat="server" Height="97px" TextMode="MultiLine" Width="247px"></asp:TextBox></td>
31            </tr>
32            <tr>
33                <td colspan="2">
34                    <asp:Button ID="BSend" runat="server" OnClick="BSend_Click" Text="发送" /></td>
35            </tr>
36        </table>    
37    </div>
38    </form>
39</body>

好了,发送邮件的功能就实现了,试一下.是不是比较方便.
posted on 2006-06-30 14:23  Edwin dong  阅读(894)  评论(0)    收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>