RSS订阅优然探索
你的位置:首页 » 技术文章 » 正文

C# 发送Email

选择字号: 超大 标准 发布时间:2009-12-1 22:13:49 | 作者:admin | 0个评论 | 人浏览

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MailAddress MessageFrom = new MailAddress("quzefeng@139.com");   //发件人邮箱地址
            string MessageTo = "quzefeng@0451sky.com";   //收件人邮箱地址
            string MessageSubject = "邮件主题";         //邮件主题
            string MessageBody = "这里是邮件内容。";    //邮件内容
            if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody))
            {
                htm = htm&("发送邮件成功");
            }
            else
            {
                htm = htm&("发送邮件失败");
            }
        }


        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="MessageFrom">发件人邮箱地址</param>
        /// <param name="MessageTo">收件人邮箱地址</param>
        /// <param name="MessageSubject">邮件主题</param>
        /// <param name="MessageBody">邮件内容</param>
        /// <returns></returns>
        public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
        {
            #region 基本设置

            MailMessage message = new MailMessage();
            message.From = MessageFrom;
            message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = true;              //是否为html格式
            message.Priority = MailPriority.High;   //发送邮件的优先等级

            #endregion
          
            #region 邮件设置

            string sHost, sUserName, sPwd;
            sHost = "smtp.139.com";
            sUserName = "username";
            sPwd = "pwd";

            #endregion

            #region 发送邮件

            SmtpClient sc = new SmtpClient();
            sc.Host = sHost;                      //指定发送邮件的服务器地址或IP
            sc.Port = 25;                           //指定发送邮件端口
            sc.Credentials = new System.Net.NetworkCredential(sUserName, sPwd); //指定登录服务器的用户名和密码
            try
            {
                sc.Send(message);       //发送邮件
            }
            catch (Exception ex)
            {
                //htm = htm&(ex.ToString());
                return false;
            }
            return true;

            #endregion
           
        }


    }
}
 

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。