SDB001_SQL

[C#]Sendmail by Query Data

AC posted @ Fri, 17 Oct 2008 04:22:56 +0800 in Job with tags C# Job , 1351 readers

Sendmail by Query Data

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

 

namespace MailToUser
{

        public class Form1 : System.Windows.Forms.Form
        {

                private System.ComponentModel.Container components = null;
                public Form1()
                {

                        InitializeComponent();

                }

                protected override void Dispose(bool disposing)
                {
                        if (disposing)
                        {
                                if (components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose(disposing);
                }

                private void InitializeComponent()
                {
 
                        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                        this.ClientSize = new System.Drawing.Size(240, 189);
                        this.Name = "Form1";
                        this.Text = "Form1";
                        this.Load += new System.EventHandler(this.Form1_Load);

                }
 
                [STAThread]
                static void Main()
                {
                        Application.Run(new Form1());
                }
                private void Form1_Load(object sender, System.EventArgs e)
                {
                        MailToUser();
                }
                private void MailToUser()
                {
                        InoteraSD.Network.Email SendMail = new InoteraSD.Network.Email();
                        string strConn = "user id=admIxxxxxx;data source=Sxxxxx;initial catalog=IS?????;pwd=xxxxxx";
                        string mailfrom = "ITAudit";
                        SqlConnection Conn = new SqlConnection(strConn);
                        Conn.Open();

                        string strConn2 = "user id=admIxxxxxx;data source=Sxxxxx;initial catalog=IS?????;pwd=xxxxxx";

                        string strSQL2 = "select Distinct convert(varchar(10),SFA_SystemTime,111) as SFA_SystemTime,ID_no,SFA_FDName,E_Mail,SFA_ComputerName from dbo.SFA_Email_v Group by SFA_FDName,ID_No,E_Mail,SFA_ComputerName,SFA_SystemTime order by ID_No";
                        SqlConnection Conn2 = new SqlConnection(strConn2);
                        Conn2.Open();
                        SqlCommand vSqlCom2 = new SqlCommand(strSQL2, Conn2);

                        SqlDataReader sqlr2 = vSqlCom2.ExecuteReader();

                        string SFA = string.Empty, LastID = null, ID= null, mailto = string.Empty;
                        while (sqlr2.Read())           

                        {

                                ID = sqlr2[1].ToString();
                                if(LastID == null)
                                {
                                        mailto = sqlr2[3].ToString();
                                        SFA = sqlr2[2].ToString();
                                        LastID = ID;
                                        continue;
                                }

                                if(ID.Equals(LastID))
                                {
                                        SFA += "、" + sqlr2[2].ToString();
                                        continue;
                                }
                                //mailto = "jxxxx@mailcc.com";

                                string mailbody = "<Font face='Arial'>Dear Colleagues,<br>You shared <font color=RED face='Arial'><B>" + SFA + "</B></font> folder in computer <font color=RED face='Arial'><B>" + sqlr2[4].ToString().Trim() + "</B></font> with insecure permission settings of Everyone: Full Control on <font color=RED face='Arial'><B>" + sqlr2[0].ToString().Trim() + "</B></font>.<br>For virus attack and personal privacy concern, please take one of following safety configurations to modify share folder:<br>";
                                mailbody += "<P><Font face='Arial'>1.To disable the share folder,<br>2.To set read-only permission of Everyone,<br>3.To remove permission of Everyone and set the appropriate permission of designated person for access.</font><br>";
                                mailbody += "<p><font face='Arial'>If you still have any concern with share folder, please contact MIS-xxxx #xxxx<p>";
                                mailbody += "<P><P><Font face='Arial'>親愛的同仁,<br>";
                                mailbody += "您於 <font color=RED face='Arial'><B>" + sqlr2[0].ToString().Trim() + "</B></font> 在電腦名稱<font color=RED face='Arial'><B>" + sqlr2[4].ToString().Trim() + "</B></font>分享 <font color=RED face='Arial'><B>" + SFA + "</B></font> 資料夾,並開啟 Everyone: Full Control 權限,此為不安全之電腦設定。<br>";
                                mailbody += "<Font face='Arial'>為避免病毒藉由此途徑感染及個人隱私的問題,請即刻採取以下任一方式進行較安全性之設定:<br>";
                                mailbody += "<p><Font face='Arial'>1.關閉分享資料夾;<br>2.將Everyone權限設定為唯讀;<br>3.取消Everyone權限,並設定特定人員讀取。<br>";
                                mailbody += "<p><Font face='Arial'>對於資料夾分享有任何疑問,請聯繫MIS人員-xxx,分機:xxx。<BR>";

                                string mailsubject = "Notification of Share Folder Audit 分享資料夾稽核通知";

                                SendMail.SendMail(mailfrom, mailto, mailsubject, mailbody, "");

                                mailto = sqlr2[3].ToString();
                                SFA = sqlr2[2].ToString();
                                LastID = ID;
                        }
                        sqlr2.Close();
                        Conn2.Close();
                        Application.Exit();
                }
        }
}
 


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter