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;
}
}
}