发送邮件

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using CDO;
using Huolx.Pubclass;

namespace MailSend
{
    class SendMail
    {
        static string ConnStr = "server=172.24.122.17;database=PDDB;uid=sa;pwd=tvemes";
        //static string ConnStr = "server=.;database=PDDB;uid=sa;pwd=sa";
        public void Send()
        {
            //讀出mail信息
            //調用CDO發送Mail
            SqlDataReader dr = null;
            SqlConnection connection = null;
            string sSql;
            try
            {
                try
                {
                    connection = GetConnection(ConnStr);
                }
                catch
                {
                    Console.WriteLine("The connection with the database can愒 be established");
                    return;
                }

                 sSql = "select * from ClewMailTable where State=0";
                  //sSql = "SELECT * FROM  ClewMailTable WHERE State = 0 order by clewMailID desc";

                dr = SqlHelper.ExecuteReader(connection, CommandType.Text, sSql);
                int i = 1;
                while (dr.Read())
                {
                    string Destination = Convert.ToString(dr["FromMail"]);
                    string RecipAddress = Convert.ToString(dr["ToMail"]);
                    string CC = Convert.ToString(dr["FbMail"]);
                    string Subject = Convert.ToString(dr["Title"]);
                    string NewMsg = Convert.ToString(dr["Body"]);
                    string IntID = dr["clewMailID"].ToString();
                    CDOsendmail(Destination, RecipAddress, CC, Subject, NewMsg, "robot", "robot123", "172.24.122.6");
                   // UPStatus(Convert.ToString(dr["ID"]));
                    Console.WriteLine(i + "封:發送Mail成功!" + "ID:" + IntID);
                    i++;
                }
            }
            catch (Exception ex)
            {
                string errMessage = "";
                for (Exception tempException = ex; tempException != null; tempException = tempException.InnerException)
                {
                    errMessage += tempException.Message + Environment.NewLine + Environment.NewLine;
                }

                Console.WriteLine(string.Format("There are some problems while trying to use the Data Access Application block, please check the following error messages: {0}"
                    + Environment.NewLine + "This test requires some modifications to the Northwind database. Please make sure the database has been initialized using the SetUpDataBase.bat database script, or from the  Install Quickstart option on the Start menu.", errMessage));
            }
            finally
            {
                if (dr != null)
                    ((IDisposable)dr).Dispose();

                if (connection != null)
                    connection.Dispose();
            }
        }

        private void UPStatus(String sID)
        {
            SqlConnection connection = null;
            try
            {
                try
                {
                    connection = GetConnection(ConnStr);
                }
                catch
                {
                    Console.WriteLine("The connection with the database can愒 be established");
                    return;
                }
            }
            catch (Exception ex)
            {
                string errMessage = "";
                for (Exception tempException = ex; tempException != null; tempException = tempException.InnerException)
                {
                    errMessage += tempException.Message + Environment.NewLine + Environment.NewLine;
                }

                Console.WriteLine(string.Format("There are some problems while trying to use the Data Access Application block, please check the following error messages: {0}"
                    + Environment.NewLine + "This test requires some modifications to the Northwind database. Please make sure the database has been initialized using the SetUpDataBase.bat database script, or from the  Install Quickstart option on the Start menu.", errMessage));
            }
            finally
            {
                if (connection != null)
                    connection.Dispose();
            }

            return ;
        }
        private SqlConnection GetConnection(string connectionString)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();

            return connection;
        }
        private static void CDOsendmail(string from, string to, string CC, string subject, string body, string userName, string password, string smtpServer)
        {

            CDO.Message Msg = new CDO.Message();
            Msg.From = from;
            Msg.To = to;
            Msg.CC = CC;
            Msg.Subject = subject;
            Msg.HTMLBody = "<html><body>" + body
                + "</body></html>";
            CDO.IConfiguration Config = Msg.Configuration;
            ADODB.Fields oFields = Config.Fields;
            oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
            oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = userName;
            oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;
            oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 0;
            oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
            oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = smtpServer;
            oFields.Update();

            Msg.BodyPart.Charset = "BIG5";
            Msg.HTMLBodyPart.Charset = "BIG5";

            Msg.Send();
            Msg = null;
        }
    }
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值