用asp.net发送电子邮件

本文介绍如何使用.NET框架通过SMTP服务器发送电子邮件,包括设置发件人、收件人、主题和正文,以及添加附件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

好了,发送邮件的功能就实现了,试一下.是不是比较方便.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值