在asp.net 2.0中发送邮件


作者: webabcd


1、在web.config中的<configuration>内加入如下配置信息(host—smtp服务地址;port—端口号;userName—用户名;password—密码。请自行修改)。
<system.net>
        <mailSettings>

            <smtp>
                <network host="smtpserver" port="25" userName="uid" password="pwd" />
            </smtp>

        </mailSettings>
    </system.net>

2、aspx页面HTML代码
 
< table border ="0" >
                 < tr >
                         < td >
                                发件人
                         </td>
                         < td >
                                 < asp:TextBox runat ="server" ID ="emailfrom" > </asp:TextBox>
                         </td>
                 </tr>
                 < tr >
                         < td >
                                收件人
                         </td>
                         < td >
                                 < asp:TextBox runat ="server" ID ="emailto" > </asp:TextBox>
                         </td>
                 </tr>
                 < tr >
                         < td >
                                主题
                         </td>
                         < td >
                                 < asp:TextBox runat ="server" ID ="subject" > </asp:TextBox>
                         </td>
                 </tr>
                 < tr >
                         < td >
                                附件
                         </td>
                         < td >
                                 < asp:FileUpload ID ="p_w_upload" runat ="server" />
                         </td>
                 </tr>
                 < tr >
                         < td >
                                内容
                         </td>
                         < td >
                                 < asp:TextBox runat ="server" ID ="body" TextMode ="MultiLine" Columns ="50" Rows ="10" > </asp:TextBox>
                         </td>
                 </tr>
                 < tr >
                         < td colspan ="2" align ="center" >
                                 < asp:Button runat ="server" ID ="btnSend" Text ="发送" OnClick ="btnSend_Click" />
                         </td>
                 </tr>
         </table>
 
3、实例化一个MailMessage并设置其属性
InBlock.gifMailMessage mm = new MailMessage(emailfrom.Text, emailto.Text);
InBlock.gif
InBlock.gif                mm.Subject = subject.Text;
InBlock.gif                mm.Body = body.Text;
InBlock.gif                 // HTML格式
InBlock.gif                mm.IsBodyHtml = true;
InBlock.gif
InBlock.gif                 // 添加附件
InBlock.gif                mm.Attachments.Add( new Attachment(p_w_upload.PostedFile.InputStream, p_w_upload.FileName));
InBlock.gif
InBlock.gif                 /*其他如抄送、优先级之类的都可以在MailMessage类的属性中设置*/
 
4、实例化一个SmtpClient,调用其Send方法,参数为MailMessage对象
InBlock.gifSmtpClient sc = new SmtpClient();
InBlock.gif
InBlock.gif                 // 编程方式设置smtp(不用web.config)
InBlock.gif                 // sc.Host = "";
InBlock.gif                 // sc.Port = 25;
InBlock.gif                 // sc.Credentials = new NetworkCredential("username", "password");
InBlock.gif
InBlock.gif                 try
InBlock.gif                {
InBlock.gif                        sc.Send(mm);
InBlock.gif                        Response.Write( "ok");
InBlock.gif                }
InBlock.gif                 catch (Exception ex)
InBlock.gif                {
InBlock.gif                         // 与smtp相关的错误
InBlock.gif                         if (ex is SmtpException)
InBlock.gif                        {
InBlock.gif                                 // ex.ToString();
InBlock.gif                                Response.Write( "smtp发信失败");
InBlock.gif                        }
InBlock.gif                         else
InBlock.gif                        {
InBlock.gif                                Response.Write(ex.ToString());
InBlock.gif                        }
InBlock.gif                }
 

OK