shawl.qiu c# .net 发邮件用户控件 v1.0

shawl.qiu c# .net 发邮件用户控件 v1.0

说明:
写这个只是时间问题,没什么难度, 就不说明什么了。。。

shawl.qiu
2007-04-24
http://blog.csdn.net/btbtd

下载:
http://files.myopera.com/btbtd/csharp/uc/csharp_dotNet_uc_Mails_v_1.0_by_shawl.qiu.7z


相关文章:
ASP Class(类) 之 使用内建组件 cdo.message 发送邮件 By shawl.qiu
http://blog.csdn.net/btbtd/archive/2006/09/12/1214643.aspx


内容:
1. Mails.aspx
  1. <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" ValidateRequest="false" %>
  2. <%@ Reference Control="Mails.ascx" %>
  3. <script runat="server">

  4.  void Page_Load(Object s, EventArgs e)
  5.  {
  6.    Mails mails = (Mails)LoadControl("Mails.ascx");
  7.     MailPh.Controls.Add(mails);
  8.     mails = null;
  9.  } // end Page_Load
  10.  
  11. </script>

  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  13. <html>
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Untitled Document</title>
  17. </head>
  18. <body>
  19. <form runat="server">
  20.  <asp:Label ID="InfoLabel" runat="server" />
  21.  <asp:PlaceHolder ID="MailPh" runat="server" />
  22. </form>
  23. </body>
  24. </html> 

2. Mails.ascx
  1. <%@ Control Language="C#" AutoEventWireup="TrueclassName="Mails" %>
  2. <%@ import Namespace="System.Text" %>
  3. <%@ import Namespace="System.Web.Mail" %>
  4. <script runat="server">
  5.  public const String auSubject = "shawl.qiu c# .net 发邮件用户控件";
  6.  public const String auVersion = "v1.0";
  7.  public const String au = "shawl.qiu";
  8.  public const String auEmail = "shawl.qiu@gmail.com";
  9.  public const String auBlog = "http://blog.csdn.net/btbtd";
  10.  public const String auCreateDate = "2007-4-24";
  11.  
  12.  public bool Debug = false;
  13.  
  14.  public string sFrom = "alyy@21cn.com";
  15.  public string sTo = "shawl.qiu@gmail.com";
  16.  public string sCc = "";
  17.  public string sBcc = "";
  18.  public string sSubject = "shawl.qiu c# .net 发邮件测试 "+DateTime.Now;
  19.  
  20.  public string sFormatDefaultSelect = "0";
  21.  public string sEncoding = "UTF-8";
  22.  
  23.  public string sText ="Just a test, <b>这是一个测试</b>";
  24.  public string sSmtp = "smtp.21cn.com";
  25.  
  26.  public string sPort = "25";
  27.  public string sTimeout = "60";
  28.  
  29.  public string sSsl = "false";
  30.  
  31.  public string sAuthDefaultSelect = "1";
  32.  public string sUsingDefaultSelect = "2";
  33.  
  34.  public string sUsername = "alyy";
  35.  public string sPassword = "alyysq";
  36.  
  37.  void Page_Load(Object s, EventArgs e)
  38.  {
  39.   DspSbjLbl.Text = auSubject+" "+auVersion;
  40.   
  41.   InfoLabel.Text = "";
  42.   DebugLabel.Text = "";
  43.   
  44.   if(!IsPostBack)
  45.   {
  46.    FromTbx.Text = sFrom;
  47.    ToTbx.Text = sTo;
  48.    CcTbx.Text = sCc;
  49.    BccTbx.Text = sBcc;
  50.    SubjectTbx.Text = sSubject;
  51.    
  52.    FormatCbl.SelectedValue = sFormatDefaultSelect+"";
  53.    //EncodingTbx.Text = sEncoding;
  54.    
  55.    TextTbx.Text = sText;
  56.    SmtpTbx.Text = "smtp.21cn.com";
  57.    
  58.    PortTbx.Text = sPort+"";
  59.    TimeoutTbx.Text = sTimeout+"";
  60.    
  61.    SslCbx.Checked = Boolean.Parse(sSsl);
  62.    
  63.    AuthDdl.SelectedValue = sAuthDefaultSelect+"";
  64.    UsingDdl.SelectedValue = sUsingDefaultSelect+"";
  65.    
  66.    UsernameTbx.Text = sUsername;
  67.    PasswordTbx.Text = sPassword;
  68.    
  69.    if(Debug)
  70.    {
  71.     Response.Write("<li/>PasswordTbx.Text: "+PasswordTbx.Text);
  72.    }
  73.   } // end if
  74.  } // end Page_Load
  75.  
  76.  void SendMailFunc(Object s, EventArgs e)
  77.  {
  78.   SendMail sendMail = new SendMail();
  79.   
  80.   sendMail.Debug = true;
  81.   sendMail.DebugLabel = DebugLabel;
  82.   sendMail.InfoLabel = InfoLabel;
  83.   
  84.   sendMail.From = FromTbx.Text;
  85.   sendMail.To = ToTbx.Text;
  86.   sendMail.Cc = CcTbx.Text;
  87.   sendMail.Bcc = BccTbx.Text;
  88.   sendMail.Subject = SubjectTbx.Text;
  89.   sendMail.Format = FormatCbl.SelectedValue;
  90.   sendMail.Text = TextTbx.Text;
  91.   sendMail.Smtp = SmtpTbx.Text;
  92.   sendMail.Port = PortTbx.Text;
  93.   sendMail.Timeout = TimeoutTbx.Text;
  94.   sendMail.Ssl = SslCbx.Checked+"";
  95.   sendMail.Auth = AuthDdl.SelectedValue;
  96.   sendMail.Using = UsingDdl.SelectedValue;
  97.   sendMail.Username = UsernameTbx.Text;
  98.   sendMail.Password = PasswordTbx.Text;
  99.   
  100.   sendMail.Go();
  101.   sendMail = null;
  102.  }
  103.  
  104. /*-----------------------------------------------------------------------------------*/
  105.  * shawl.qiu class SendMail v1.0
  106. /*-----------------------------------------------------------------------------------*/
  107. //---------------------------------------------------------------------begin class SendMail
  108. public class SendMail
  109. {
  110.  //-----------------------------------begin event
  111.  public SendMail()
  112.  {
  113.  }
  114.  
  115.  ~SendMail()
  116.  {
  117.  }
  118.  //-----------------------------------end event
  119.  
  120.  //-----------------------------------begin public constant
  121.  //-----------------------begin about
  122.  public const String auSubject = "shawl.qiu class SendMail v1.0";
  123.  public const String auVersion = "v1.0";
  124.  public const String au = "shawl.qiu";
  125.  public const String auEmail = "shawl.qiu@gmail.com";
  126.  public const String auBlog = "http://blog.csdn.net/btbtd";
  127.  public const String auCreateDate = "2007-4-24";
  128.  //-----------------------end about
  129.  //-----------------------------------end public constant
  130.  
  131.  //-----------------------------------begin private constant
  132.  //-----------------------------------end private constant
  133.  
  134.  //-----------------------------------begin public static method
  135.  public static void Message(Label InfoLabel, string msg)
  136.  {
  137.   InfoLabel.Text +="<div style=/"display:table;width:100%;background-color:yellow!important;";
  138.   InfoLabel.Text += "color:black!important;text-align:center!important;/">";
  139.   InfoLabel.Text += msg+"</div>";
  140.  }
  141.  
  142.  public static void Message(string msg, Label InfoLabel)
  143.  {
  144.   Message(InfoLabel, msg);
  145.  }
  146.  
  147.  public static void Message(Label InfoLabel, string msg, string charSet)
  148.  {
  149.   InfoLabel.Text+="<meta http-equiv=/"Content-Type/" content=/"text/html; charset="+
  150.   charSet+"/" />";
  151.   Message(InfoLabel, msg);
  152.  }
  153.  //-----------------------------------end public static method
  154.  
  155.  //-----------------------------------begin private static method
  156.  //-----------------------------------end private static method
  157.  
  158.  //-----------------------------------begin public variable
  159.  
  160.  public bool Debug = false;
  161.  
  162.  public string From = "";
  163.  public string To = "";
  164.  public string Cc = "";
  165.  public string Bcc = "";
  166.  public string Subject = "";
  167.  
  168.  public string Format = "1";
  169.  public string Encoding = "UTF-8";
  170.  
  171.  public string Text = "";
  172.  public string Smtp = "";
  173.  
  174.  public string Port = "25";
  175.  public string Timeout = "60";
  176.  
  177.  public string Ssl = "false";
  178.  
  179.  public string Auth = "1";
  180.  public string Using = "2";
  181.  
  182.  public string Username = "";
  183.  public string Password = "";
  184.  
  185.  public Label InfoLabel;
  186.  public Label DebugLabel;
  187.  //-----------------------------------end public variable
  188.  
  189.  //-----------------------------------begin private variable
  190.  //-----------------------------------end private variable
  191.  
  192.  //-----------------------------------begin public method
  193.  public void Go()
  194.  {
  195.   if(Debug)
  196.   {
  197.    System.Web.HttpContext.Current.Response.Write("<li/>public void Go(): Ok");
  198.   }
  199.   if(From=="")
  200.   {
  201.    if(InfoLabel!=null)
  202.    {
  203.     Message("From 不能为空!", InfoLabel);
  204.    }
  205.    return;
  206.   }
  207.   
  208.   if(To=="")
  209.   {
  210.    if(InfoLabel!=null)
  211.    {
  212.     Message("To 不能为空!", InfoLabel);
  213.    }
  214.    return;
  215.   }
  216.   
  217.   if(Subject=="")
  218.   {
  219.    if(InfoLabel!=null)
  220.    {
  221.     Message("Subject 不能为空!", InfoLabel);
  222.    }
  223.    return;
  224.   }
  225.   
  226.   if(Text=="")
  227.   {
  228.    if(InfoLabel!=null)
  229.    {
  230.     Message("Text 不能为空!", InfoLabel);
  231.    }
  232.    return;
  233.   }
  234.   
  235.   if(Smtp=="")
  236.   {
  237.    if(InfoLabel!=null)
  238.    {
  239.     Message("Smtp 不能为空!", InfoLabel);
  240.    }
  241.    return;
  242.   }
  243.   
  244.   if(Username=="")
  245.   {
  246.    if(InfoLabel!=null)
  247.    {
  248.     Message("Username 不能为空!", InfoLabel);
  249.    }
  250.    return;
  251.   }
  252.   
  253.   if(Password=="")
  254.   {
  255.    if(InfoLabel!=null)
  256.    {
  257.     Message("Password 不能为空!", InfoLabel);
  258.    }
  259.    return;
  260.   }
  261.   
  262.   if(Encoding=="")
  263.   {
  264.    Encoding = "UTF-8";
  265.   }
  266.   
  267.   if(Debug)
  268.   {
  269.    System.Web.HttpContext.Current.Response.Write("<li/>From: "+From);
  270.    System.Web.HttpContext.Current.Response.Write("<li/>To: "+To);
  271.    System.Web.HttpContext.Current.Response.Write("<li/>Cc: "+Cc);
  272.    System.Web.HttpContext.Current.Response.Write("<li/>Bcc: "+Bcc);
  273.    System.Web.HttpContext.Current.Response.Write("<li/>Subject: "+Subject);
  274.     
  275.    System.Web.HttpContext.Current.Response.Write("<li/>Format: "+Format);
  276.     
  277.    System.Web.HttpContext.Current.Response.Write("<li/>Text: "+Text);
  278.    System.Web.HttpContext.Current.Response.Write("<li/>Smtp: "+Smtp);
  279.     
  280.    System.Web.HttpContext.Current.Response.Write("<li/>Port: "+Port);
  281.    System.Web.HttpContext.Current.Response.Write("<li/>Timeout: "+Timeout);
  282.     
  283.    System.Web.HttpContext.Current.Response.Write("<li/>Ssl: "+Ssl);
  284.     
  285.    System.Web.HttpContext.Current.Response.Write("<li/>Auth: "+Auth);
  286.    System.Web.HttpContext.Current.Response.Write("<li/>Using: "+Using);
  287.     
  288.    System.Web.HttpContext.Current.Response.Write("<li/>Username: "+Username);
  289.    System.Web.HttpContext.Current.Response.Write("<li/>Password: "+Password);
  290.   } // end if
  291.   
  292.   MailMessage mm = new MailMessage();
  293.    mm.From = From;
  294.    mm.To = To;
  295.    if(Cc!="") mm.Cc = Cc;
  296.    if(Bcc!="") mm.Bcc = Bcc;
  297.    mm.Subject = Subject;
  298.    
  299.    switch(Format)
  300.    {
  301.     case "0":
  302.      mm.BodyFormat = MailFormat.Text;
  303.      break;
  304.      
  305.     case "1":
  306.      mm.BodyFormat = MailFormat.Html;
  307.      break;
  308.      
  309.     default:
  310.      break;
  311.    }
  312.    
  313.    mm.Body = Text;
  314.    
  315.    mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing",   Using.Trim());   
  316.    //mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver",   Smtp);   
  317.    mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",   Auth.Trim());  
  318.    mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",   Port.Trim());   
  319.    mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",   Username.Trim());   
  320.    mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",   Password.Trim());   
  321.    mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout",   Timeout.Trim());  
  322.    mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl",   Boolean.Parse(Ssl));   

  323.    SmtpMail.SmtpServer = Smtp.Trim();
  324.    SmtpMail.Send(mm);
  325.    
  326.    mm = null;
  327.  } // end public void Go
  328.  //-----------------------------------end public method
  329.  
  330.  //-----------------------------------begin private method
  331.  //-----------------------------------end private method
  332.  
  333.  //-----------------------------------begin public property
  334.  //-----------------------------------end public property
  335.   
  336.  //-----------------------------------begin private property
  337.  //-----------------------------------end private property
  338. }
  339. //---------------------------------------------------------------------end class SendMail

  340.  
  341. </script>
  342. <style type="text/css">
  343. /* <![CDATA[ */
  344.  .MailsTbl 
  345.  {
  346.   margin: 5px 0px;
  347.  }
  348.  
  349.  .MailsTbl tr td
  350.  {
  351.   padding: 2px;
  352.  }
  353.  
  354.  .CssMainWidth
  355.  {
  356.   width:60%;
  357.  }
  358. /* ]]> */
  359. </style>
  360. <asp:Label ID="InfoLabel" runat="server" />
  361. <asp:Label ID="DebugLabel" runat="server" />
  362. <table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0" class="MailsTbl">
  363.  <tr align="center">
  364.   <td colspan="2">
  365.    <strong><asp:Label ID="DspSbjLbl" runat="server" /></strong>
  366.   </td> 
  367.  </tr>
  368.  <tr>
  369.   <td width="120" align="right" valign="top">From:</td>
  370.   <td>
  371.    <asp:TextBox ID="FromTbx" runat="server"
  372.     CssClass="CssMainWidth"
  373.     />
  374.   </td>
  375.  </tr>
  376.  <tr>
  377.   <td width="120" align="right" valign="top">To:</td>
  378.   <td>
  379.    <asp:TextBox ID="ToTbx" runat="server"
  380.     CssClass="CssMainWidth"
  381.     Rows=3
  382.     TextMode="MultiLine"
  383.     />
  384.   </td>
  385.  </tr>
  386.  <tr>
  387.   <td width="120" align="right" valign="top">CC:</td>
  388.   <td>
  389.    <asp:TextBox ID="CcTbx" runat="server"
  390.     CssClass="CssMainWidth"
  391.     />
  392.   </td>
  393.  </tr>
  394.  <tr>
  395.   <td width="120" align="right" valign="top">BCC:</td>
  396.   <td>
  397.    <asp:TextBox ID="BccTbx" runat="server"
  398.     CssClass="CssMainWidth"
  399.     />
  400.   </td>
  401.  </tr>
  402.  <tr>
  403.   <td width="120" align="right" valign="top">Subject:</td>
  404.   <td>
  405.    <asp:TextBox ID="SubjectTbx" runat="server"
  406.     CssClass="CssMainWidth"
  407.     />
  408.   </td>
  409.  </tr>
  410.  <tr>
  411.   <td width="120" align="right" valign="top">Text Format:</td>
  412.   <td>
  413.    <asp:RadioButtonList ID="FormatCbl" runat="server"
  414.     RepeatDirection="Horizontal"
  415.     RepeatLayout="Flow"
  416.     >
  417.     <asp:ListItem Value="0">Text</asp:ListItem>
  418.     <asp:ListItem Value="1">HTML</asp:ListItem>
  419.    </asp:RadioButtonList>
  420.   </td>
  421.  </tr>
  422.  <tr>
  423.   <td align="right" valign="top">Text Encoding: </td>
  424.   <td>
  425.    <%-- <asp:TextBox ID="EncodingTbx" runat="server" /> --%>
  426.   </td>
  427.  </tr>
  428.  <tr>
  429.   <td width="120" align="right" valign="top">Text:</td>
  430.   <td>
  431.    <asp:TextBox ID="TextTbx" runat="server"
  432.     CssClass="CssMainWidth"
  433.     Rows=10
  434.     TextMode="MultiLine"
  435.     />
  436.   </td>
  437.  </tr>
  438.  <tr>
  439.   <td width="120" align="right" valign="top">Remote SMTP: </td>
  440.   <td>
  441.    <asp:TextBox ID="SmtpTbx" runat="server"
  442.     /> 
  443.    </td>
  444.  </tr>
  445.  <tr>
  446.   <td width="120" align="right" valign="top">Port:</td>
  447.   <td>
  448.    <asp:TextBox ID="PortTbx" runat="server"
  449.     Size=5
  450.     /> 
  451.   </td>
  452.  </tr>
  453.  <tr>
  454.   <td width="120" align="right" valign="top">Timeout:</td>
  455.   <td>
  456.    <asp:TextBox ID="TimeoutTbx" runat="server"
  457.     Size=5
  458.     /> 
  459.    (Unit: Second) </td>
  460.  </tr>
  461.  <tr>
  462.   <td width="120" align="right" valign="top">SSL:</td>
  463.   <td>
  464.    <asp:CheckBox ID="SslCbx" runat="server"
  465.     />
  466.   </td>
  467.  </tr>
  468.  <tr>
  469.   <td align="right" valign="top">SMTP验证选项:  </td>
  470.   <td>

  471.    <asp:DropDownList ID="AuthDdl" runat="server"
  472.     RepeatDirection="Horizontal"
  473.     RepeatLayout="Flow"
  474.     >
  475.     <asp:ListItem Value="0">匿名验证</asp:ListItem>
  476.     <asp:ListItem Value="1">普通验证</asp:ListItem>
  477.     <asp:ListItem Value="2">NTLM 验证</asp:ListItem>
  478.    </asp:DropDownList>
  479.   </td>
  480.  </tr>
  481.  <tr>
  482.   <td align="right" valign="top">邮件发送选项:</td>
  483.   <td>
  484.    <asp:DropDownList ID="UsingDdl" runat="server"
  485.     RepeatDirection="Horizontal"
  486.     RepeatLayout="Flow"
  487.     >
  488.     <asp:ListItem Value="1">Send Using Pickup</asp:ListItem>
  489.     <asp:ListItem Value="2">Send Using Port</asp:ListItem>
  490.    </asp:DropDownList>
  491.   </td>
  492.  </tr>
  493.  <tr>
  494.   <td align="right" valign="top">Username:</td>
  495.   <td>
  496.    <asp:TextBox ID="UsernameTbx" runat="server"
  497.     /> 
  498.   </td>
  499.  </tr>
  500.  <tr>
  501.   <td align="right" valign="top">Password:</td>
  502.   <td>
  503.    <asp:TextBox ID="PasswordTbx" runat="server"
  504.     /> 
  505.   </td>
  506.  </tr>
  507.  <tr>
  508.   <td align="right" valign="top">&nbsp;</td>
  509.   <td>
  510.    <input type="submit" value="Submit" runat="server"
  511.     onclick="return confirm('现在发送邮件吗?')"
  512.     OnServerClick=SendMailFunc
  513.     />
  514.    <input type="reset" name="Reset" value="Reset"
  515.     onclick="return confirm('现在重置吗?')"
  516.     />
  517.   </td>
  518.  </tr>
  519. </table>




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值