当前位置:编程学习 > C#/ASP.NET >>

C#的E_mail发送简单实现

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendEeail.aspx.cs" Inherits="Web_test_SendEeail" %>

<!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 runat="server">
    <title>SendEmail</title>
</head>
<body>
    <form id="form1" Enctype="multipart/form-data" runat="server">
        <table border=1>
            <tr><td colspan="2"></td></tr>
            <tr><td colspan="2"><asp:Label id="lblShowMsg" ForeColor="red" runat="server" /></td></tr>
            <tr><td>收件人:</td><td><asp:TextBox id="tbTo" runat="server" />
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="无效的E_mail地址!" ControlToValidate="tbTo" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td></tr>
            <tr><td>发件人:</td><td><asp:TextBox id="tbFrom" runat="server" />
                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="无效的E_mail地址!" ControlToValidate="tbFrom" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td></tr>
            <tr><td>邮件主题:</td><td><asp:TextBox id="tbSubject" runat="server" /></td></tr>
            <tr>
             <td>优先级:
                 <asp:DropDownList id="ddlPriority" runat="server">
         <asp:ListItem Value="High">高</asp:ListItem>
         <asp:ListItem Value="Normal" Selected="true">普通</asp:ListItem>
         <asp:ListItem Value="Low">低</asp:ListItem>
        </asp:DropDownList>
             </td>
             <td>邮件格式:
                 <asp:DropDownList id="ddlBodyFormat" runat="server">
         <asp:ListItem Value="Text">文本格式</asp:ListItem>
         <asp:ListItem Value="Html">HTML格式</asp:ListItem>
        </asp:DropDownList>
             </td>
            </tr>
            <tr><td colspan="2">邮件内容:</td></tr>
            <tr><td colspan="2"><asp:TextBox TextMode="MultiLine" Rows="5" Columns="50" id="tbBody" runat="server" /></td></tr>
            <tr><td>邮件附件:</td><td><input type="file" id="AttachFile" runat="server" /></td></tr>
            <tr><td><asp:Button id="btnSend" Text="发送" OnClick="Mail_Send" runat="server" /></td></tr>
        </table>
    </form>
</body>
</html>

*********************************************************************************************************

//SendEeail.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.Net.Mail;
using System.Net;

public partial class Web_test_SendEeail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Mail_Send(object sender, EventArgs e)
    {
        MailAddress from = new MailAddress(tbFrom.Text);
        MailAddress to = new MailAddress(tbTo.Text);

        MailMessage MyMsg = new MailMessage(from, to);
        MyMsg.Subject = tbSubject.Text.Trim() != "" ? tbSubject.Text.Trim() : "nosubject";
        MyMsg.Priority = (MailPriority)ddlPriority.SelectedIndex;
        MyMsg.IsBodyHtml = ddlBodyFormat.SelectedValue == "Html" ? true : false;
        MyMsg.Body = tbBody.Text;

        HttpPostedFile hpfFile = AttachFile.PostedFile;
        if (hpfFile.FileName != "")
        {
            //有附件,则上传到Temp目录中
            //取得文件名(不含路径)
            char[] de = { '\\' };
            string[] AFilename = hpfFile.FileName.Split(de);
            string strFilename = AFilename[AFilename.Length - 1];
            string strPath = Request.ServerVariables["appl_physical_path"] + "Web\\temp\\" + strFilename;
            hpfFile.SaveAs(strPath);
            //添加附件
            MyMsg.Attachments.Add(new Attachment(strPath));
        }

        try
        {
            //发送
            SmtpClient SmtpMail = new SmtpClient("smtp.163.com");
            SmtpMail.Credentials = new NetworkCredential("username", "password");//在这里填上username和password
            SmtpMail.Send(MyMsg);
            lblShowMsg.Text = "发送成功";
            tbTo.Text = "";
            tbSubject.Text = "";
            tbBody.Text = "";
            ddlPriority.SelectedIndex = 1;
            ddlBodyFormat.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            lblShowMsg.Text = "发送失败:" + ex.ToString();
        }
    }
}

--------------------编程问答-------------------- up --------------------编程问答-------------------- using System;
using System.Net.Mail;

namespace Microsoft.Samples.Mailer
{

    // Mailer sends an e-mail.  
    // It will authenticate using Windows authentication if the server
    // (i.e. Exchange) requests it.
    static class Mailer
    {
        enum MailMessagePart
        {
            From,
            To,
            Subject,
            Message
        }

        static void Main(string[] args)
        {
            if (args.Length < 4)
            {    
                Console.WriteLine(
                    "Expected: mailer.exe [from] [to] [subject] [message]");
                return;
            }

            // Set mailServerName to be the name of the mail server
            // you wish to use to deliver this message
            string mailServerName = "smtphost";
            string from = args[(int) MailMessagePart.From];
            string to = args[(int) MailMessagePart.To];
            string subject = args[(int) MailMessagePart.Subject];
            string body = args[(int) MailMessagePart.Message];

            try
            {
                // MailMessage is used to represent the e-mail being sent
                using (MailMessage message = 
                    new MailMessage(from, to, subject, body))
                {

                    // SmtpClient is used to send the e-mail
                    SmtpClient mailClient = new SmtpClient(mailServerName);

                    // UseDefaultCredentials tells the mail client to use the 
                    // Windows credentials of the account (i.e. user account) 
                    // being used to run the application
                    mailClient.UseDefaultCredentials = true;

                    // Send delivers the message to the mail server
                    mailClient.Send(message);
                }
                Console.WriteLine("Message sent.");
            }
            catch (FormatException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (SmtpException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

--------------------编程问答-------------------- mark下 --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- 就这样直接发就行吗,你看我这里有段代码
 public partial class sendEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bool result = SendEmail("362335821@QQ.com", "whfbest@yahoo.cn", "how are you!", "I'm shuanghongy ,now test;");
        }
        public bool SendEmail(string FromEmail, string ToEmail, string Subject, string Body)
        {
            bool Success = false;
            try
            {
                System.Net.Mail.SmtpClient client = new SmtpClient("smtp.yahoo.cn");
                client.Credentials = new System.Net.NetworkCredential("362335821", "whfbest");
                //string inceptMail = WebConfigurationManager.AppSettings["ek_InceptMail"].ToString(); 
                MailMessage message = new MailMessage();
                //message.Bcc.Add(inceptMail); 
                message.To.Add(ToEmail);
                message.From = new MailAddress(FromEmail);
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = true;
                client.Send(message);

                Success = true;
            }
            catch (Exception ex)
            {
                string Error = ex.Message;
                Success = false;
                Response.Write(Error);
            }
            return Success;

        }
    }

提示发送失败 --------------------编程问答-------------------- 学习。 --------------------编程问答-------------------- 不懂之~~! --------------------编程问答-------------------- 你的错误在什么地方,把异常找出来 

我前天刚写了篇发送邮件的代码,可以发送的 --------------------编程问答-------------------- mark
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,