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 ></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 = "";
    }
}

相关文章: