在 .net 2.0 中发送邮件

在 .net 2.0 中发送邮件将使用到新增的 SmtpClient 类,并且可以在配置文件中加入 mailSettings 元素来配置 smtp。下面以 asp.net 为例写的 demo.

在 web.config 文件添加
<system.net>
    
<mailSettings>
        
<smtp>
            
<network host="localhost" port="25" userName="your username"
                password
="your password"/>
        
</smtp>
    
</mailSettings>
</system.net>

发送简单的文本格式的邮件

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

<script runat="server">
    protected 
void SendEmail_Click (object sender, EventArgs e)
    {
        MailMessage mm 
= new MailMessage (tbxUsersEmail.Text,
            tbxUsersEmail.Text);

        mm.Subject 
= tbxSubject.Text;
        mm.Body 
= tbxBody.Text;
        mm.IsBodyHtml 
= false;

        SmtpClient smtp 
= new SmtpClient ();

        smtp.Send (mm);
        Response.Write (
"Completed!");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>SendMail</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<table border="0">
                
<tr>
                    
<td>
                        
<b>Your Email:</b></td>
                    
<td>
                        
<asp:TextBox runat="server" ID="tbxUsersEmail" Columns="30">
                        </
asp:TextBox>
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
                                runat
="server" ControlToValidate="tbxUsersEmail"
                            ErrorMessage
="Must input your email.">*
                        </
asp:RequiredFieldValidator>
                    </
td>
                
</tr>
                
<tr>
                    
<td>
                        
<b>Subject:</b></td>
                    
<td>
                        
<asp:TextBox runat="server" ID="tbxSubject" Columns="30">
                        </
asp:TextBox>
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
                            runat
="server"
                            ControlToValidate
="tbxSubject"
                            ErrorMessage
="Must input your subject.">*
                       
</asp:RequiredFieldValidator>
                    </
td>
                
</tr>
                
<tr>
                    
<td colspan="2">
                        
<b>Body:</b><br />
                        
<asp:TextBox runat="server" ID="tbxBody" TextMode="MultiLine"
                            Columns
="55"  Rows="10"></asp:TextBox>
                    
</td>
                
</tr>
                
<tr>
                    
<td colspan="2" align="center">
                        
<asp:Button runat="server" ID="btnSendEmail"
                             OnClick="SendEmail_Click"  Text="Send Feedback" />
                    
</td>
                
</tr>
            
</table>
        
</div>
        
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
            ShowMessageBox
="True"  ShowSummary="False" />
    
</form>
</body>
</html>

发送 HTML 格式的邮件

 1 <%@ Page Language="C#" %>
 2 <%@ Import Namespace="System.Net" %>
 3 <%@ Import Namespace="System.Net.Mail" %>
 4 
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
 6 
 7 <script runat="server">
 8     protected void Page_Load (object sender, EventArgs e)
 9     {
10         lblContent.Text = @"<h2>This is an HTML-Formatted Email Send Using the
                    <code>IsBodyHtml</code> Property</h2><p>Isn't HTML <em>neat</em>?</p>
                    <p>You can make all sorts of <span style=
""color:red;font-weight:bold;"">
                    pretty colors!!</span>.</p>
";
11     }
12 
13     protected void SendMail_Click (object sender, EventArgs e)
14     {
15         MailMessage mm = new MailMessage (tbxMail.Text, tbxMail.Text);
16 
17         mm.Subject = "HTML-Formatted Email test";
18         mm.Body = lblContent.Text;
19         mm.IsBodyHtml = true;
20 
21         SmtpClient smtp = new SmtpClient ();
22 
23         smtp.Send (mm);
24         Response.Write ("Completed!");
25     }
26 </script>
27 
28 <html xmlns="http://www.w3.org/1999/xhtml" >
29 <head runat="server">
30     <title>SendHtmlMail</title>
31 </head>
32 <body>
33     <form id="form1" runat="server">
34     <div>
35         <asp:Button ID="btnSend" OnClick="SendMail_Click" runat="server"
                Text
="Send below context to your mail" />
36         <asp:TextBox ID="tbxMail" runat="server" Width="367px"></asp:TextBox>
37         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate
="tbxMail"
38             ErrorMessage="Must input your email.">*</asp:RequiredFieldValidator>
             <
br />
39         <br />
40         <asp:Label ID="lblContent" runat="server"></asp:Label><br />
41         &nbsp;<asp:ValidationSummary ID="ValidationSummary1" runat="server"
                ShowMessageBox
="True"
42             ShowSummary="False" />
43     </div>
44     </form>
45 </body>
46 </html>
47 

发送带有上传附件的邮件

 1 <%@ Page Language="C#" %>
 2 <%@ Import Namespace="System.Net" %>
 3 <%@ Import Namespace="System.Net.Mail" %>
 4 
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
 6 
 7 <script runat="server">
 8     protected void SendMail_Click (object sender, EventArgs e)
 9     {
10         MailMessage mm = new MailMessage (tbxUsersEmail.Text, tbxUsersEmail.Text);
11 
12         mm.IsBodyHtml = false;
13         mm.Subject = "Emailing an Uploaded File as an Attachment Demo";
14         mm.Body = tbxBody.Text;
15         mm.Attachments.Add (
               
new Attachment (fuAttachmentFile.PostedFile.InputStream,
                    fuAttachmentFile.FileName));
16 
17         SmtpClient smtp = new SmtpClient ();
18 
19         smtp.Send (mm);
20         Response.Write ("Completed!");
21     }
22 </script>
23 
24 <html xmlns="http://www.w3.org/1999/xhtml" >
25 <head runat="server">
26     <title>Send mail with attachment file.</title>
27 </head>
28 <body>
29     <form id="form1" runat="server">
30     <div>
31     <table border="0">
32     <tr>
33         <td><b>Your Email:</b></td>
34         <td><asp:TextBox runat="server" ID="tbxUsersEmail" Columns="30">
                    </
asp:TextBox>
35             <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                ControlToValidate
="tbxUsersEmail"
36                 ErrorMessage="Must input your email.">*</asp:RequiredFieldValidator>
            </
td>
37     </tr>
38     <tr>
39         <td><b>File to Send:</b></td>
40         <td>
41             <asp:FileUpload ID="fuAttachmentFile" runat="server" />
42             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                    ControlToValidate
="fuAttachmentFile"
43                 ErrorMessage="Must choice upload file">*</asp:RequiredFieldValidator>
            </
td>
44     </tr>
45     <tr>
46         <td colspan="2">
47             <b>Body:</b><br />
48             <asp:TextBox runat="server" ID="tbxBody" TextMode="MultiLine"
                    Columns
="55"  Rows="10"></asp:TextBox>
49         </td>
50     </tr>
51     <tr>
52         <td colspan="2" align="center">
53             <asp:Button runat="server" ID="btnSendEmail" OnClick="SendMail_Click"
                    Text
="Send Feedback" />
54         </td>
55     </tr>
56 </table>
57     </div>
58         <asp:ValidationSummary ID="ValidationSummary1" runat="server"
                ShowMessageBox
="True"  ShowSummary="False" />
60     </form>
61 </body>
62 </html>
63 








posted on 2006-08-15 17:58  Easy Company  阅读(411)  评论(0编辑  收藏  举报

导航