sqlo 发表于 2015-9-14 13:51:57

C#邮件发送

SendMail.aspx
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMail.aspx.cs" Inherits="Admin_SendMail" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>SendMail</title>
</head>
<body>
    <form id="form1" runat="server">
      <table border="0" align="center" cellpadding="0" cellspacing="5" >
            
            <tr>
                <td width="100">收件人:</td>
                <td width="600"><asp:TextBox id="Addressee" runat="server" Width="600"></asp:TextBox>
                <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Addressee can't be empty!" ControlToValidate="Subject" Font-Bold="True"
       Display="Dynamic" ForeColor="Black"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator id="re" runat="server" ErrorMessage="The format inaccuracy of Email!" ControlToValidate="Addressee" Font-Bold="True"
       ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Black"></asp:RegularExpressionValidator>                </td>
            </tr>
            <tr>
                <td>抄送人:</td>
                <td><asp:TextBox id="Copy" runat="server" Width="600"></asp:TextBox></td>
            </tr>
            <tr>
                <td>密送人:</td>
                <td><asp:TextBox id="secret" runat="server" Width="600"></asp:TextBox></td>
            </tr>
            <tr>
                <td>主题:</td>
                <td><asp:TextBox id="Subject" runat="server" Width="600"></asp:TextBox>
                <asp:RequiredFieldValidator id="ru" runat="server" ErrorMessage="Subject can't be empty!" ControlToValidate="Subject" Font-Bold="True"
       Display="Dynamic" ForeColor="Black"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td>附件:</td>
                <td><asp:TextBox id="Attachment" runat="server" Width="600"></asp:TextBox></td>
            </tr>
            <tr>
                <td>正文:</td>
                <td><asp:TextBox ID="Body" runat="server" TextMode="MultiLine" Rows="18" Width="600"></asp:TextBox></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                <asp:Button ID="Send" runat="server" Text="Send" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Refurbish" runat="server" Text="Refurbish"></asp:Button>    </td>
            </tr>
      </table>
    </form>
</body>
</html>
  

  SendMail.aspx.cs
  using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;//核心代码开始
using System.Windows.Forms;

  //以下暂无调试
  public partial class Admin_SendMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      }
      public bool AddRecipient(string str)   //抄送的人
    {
      str = str.Trim();
      if (str == null || str == "" || str.IndexOf("@") == -1)
            return true;
      if (RecipientNum < 10)
      {
            Recipient.Add(RecipientNum, str);
            RecipientNum++;
            return true;
      }
      else
      {
            errmsg += "收件人过多";
            return false;
      }
    }
  
    public bool AddRecipientBCC(string str)   //密送的人
    {
      if (str == null || str.Trim() == "")
            return true;
      if (RecipientBCCNum < 10)
      {
            RecipientBCC.Add(RecipientBCCNum, str);
            RecipientBCCNum++;
            return true;
      }
      else
      {
            errmsg += "收件人过多";
            return false;
      }
    }
  public void SendMailUseGmail(object sender, EventArgs e)
    {
      MailMessage objMailMessage;
      MailAttachment objMailAttachment;
  
      // 创建邮件消息
      objMailMessage = new MailMessage();
  //发件人EMAIL
      objMailMessage.From = "ujtrading@ujtrading.com";//源邮件地址
  //收件人EMAIL
      String Addressee = this.Addressee.Text;
      objMailMessage.To = Addressee;//目的邮件地址
      //objMailMessage.To = "shirley@ujtrading.com";
  //邮件主题
      String Subject = this.Subject.Text;
      objMailMessage.Subject = Subject; //发送邮件的标题
      //objMailMessage.Subject = "test";
  //邮件内容
      String Body = this.Body.Text;
      objMailMessage.Body = Body;//发送邮件的内容
  // 创建一个附件对象
      String Attachment = this.Attachment.Text;
      if (this.Attachment.Text.ToString() != "")
      {
          objMailAttachment = new MailAttachment(Attachment);//发送邮件的附件 c:\\test.txt
          objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中
      }
      
      //接着利用SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本
      //基本权限
      objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
      //用户名
      objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "ujtrading");
      //密码
      objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "789456");
      //如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied
      //SMTP地址      
      SmtpMail.SmtpServer = "smtp.ujtrading.com";
      //开始发送邮件
  try
      {
            SmtpMail.Send(objMailMessage);
            //简单一点儿可以client.Send(msg);
            MessageBox.Show("Send mail success!","Messige");
            this.Addressee.Text = "";
      }
      catch (System.Net.Mail.SmtpException ex)
      {
            MessageBox.Show(ex.Message, "Send mail fail!");
      }
      //核心代码结束
    }
    protected void Refurbish_Click(object sender, EventArgs e)
    {
      this.Addressee.Text = "";
      this.Subject.Text = "";
      this.Body.Text = "";
      this.Attachment.Text = "";
    }
}
页: [1]
查看完整版本: C#邮件发送