zoukankan      html  css  js  c++  java
  • 在asp.net 2.0中发送邮件

    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="attachment" 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并设置其属性
            MailMessage mm = new MailMessage(emailfrom.Text, emailto.Text);

            mm.Subject 
    = subject.Text;
            mm.Body 
    = body.Text;
            
    // HTML格式
            mm.IsBodyHtml = true;

            
    // 添加附件
            mm.Attachments.Add(new Attachment(attachment.PostedFile.InputStream, attachment.FileName));

            //
    *其他如抄送、优先级之类的都可以在MailMessage类的属性中设置*/

    4、实例化一个SmtpClient,调用其Send方法,参数为MailMessage对象
            SmtpClient sc = new SmtpClient();

            
    // 编程方式设置smtp(不用web.config)
            
    // sc.Host = "";
            
    // sc.Port = 25;
            
    // sc.Credentials = new NetworkCredential("username", "password");

            
    try
            
    {
                sc.Send(mm);
                Response.Write(
    "ok");
            }

            
    catch (Exception ex)
            
    {
                
    // 与smtp相关的错误
                if (ex is SmtpException)
                
    {
                    
    // ex.ToString();
                    Response.Write("smtp发信失败");
                }

                
    else
                
    {
                    Response.Write(ex.ToString());
                }

            }
    转载:http://www.cnblogs.com/webabcd/archive/2007/01/03/610735.html
  • 相关阅读:
    第一章:绪论
    第二章 算法入门
    java基本语法特殊点
    css学习の第六弹—样式设置小技巧
    122. Best Time to Buy and Sell Stock II--easy
    121. Best Time to Buy and Sell Stock--easy
    Weekly Contest 129--1023. Binary String With Substrings Representing 1 To N--Medium
    Weekly Contest 129--1021. Best Sightseeing Pair--Medium
    Weekly Contest 129--1022. Smallest Integer Divisible by K--Medium
    Weekly Contest 129--1020. Partition Array Into Three Parts With Equal Sum--easy
  • 原文地址:https://www.cnblogs.com/qfb620/p/1088964.html
Copyright © 2011-2022 走看看