winform 发送邮件(不带附件) 发送返回dos tracert命令字符串

 

using System;
using System.Web;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.Mail;

namespace Route
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = Execute("tracert 
www.baidu.com -h 4 -w 1", 200);
            label2.Text = Execute("hostname", 200);
            //SendMailUseGmail();
            Getinfo();
        }
        /// <summary>
        /// 执行DOS命令,返回DOS命令的输出
        /// </summary>
        /// <param name="dosCommand">dos命令</param>
        /// <param name="milliseconds">等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待</param>
        /// <returns>返回输出,如果发生异常,返回空字符串</returns>
        public static string Execute(string dosCommand, int milliseconds)
        {
            string output = "";     //输出字符串
            if (dosCommand != null && dosCommand != "")
            {
                Process process = new Process();     //创建进程对象
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";      //设定需要执行的命令
                startInfo.Arguments = "/C " + dosCommand;   //设定参数,其中的“/C”表示执行完命令后马上退出
                startInfo.UseShellExecute = false;     //不使用系统外壳程序启动
                startInfo.RedirectStandardInput = false;   //不重定向输入
                startInfo.RedirectStandardOutput = true;   //重定向输出
                startInfo.CreateNoWindow = true;     //不创建窗口
                process.StartInfo = startInfo;
                try
                {
                    if (process.Start())       //开始进程
                    {
                        if (milliseconds == 0)
                            process.WaitForExit();     //这里无限等待进程结束
                        else
                            process.WaitForExit(milliseconds);  //这里等待进程结束,等待时间为指定的毫秒
                        output = process.StandardOutput.ReadToEnd();//读取进程的输出
                    }
                }
                catch
                {
                }
                finally
                {
                    if (process != null)
                        process.Close();
                }
            }
            return output;
        }

        public void SendMailUseGmail()
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("
aaa@163.com");
            //msg.To.Add(
bb@b.com);   
          

            msg.From = new MailAddress("
aaa@.com", "dulei", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
            msg.Subject = "邮件标题"+label2.Text.ToString();//邮件标题  
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码  
            msg.Body = "邮件内容" + label1.Text;//邮件内容  
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码  
            msg.IsBodyHtml = false;//是否是HTML邮件  
            msg.Priority = System.Net.Mail.MailPriority.High;//邮件优先级  
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("
aaa@gmail.com", "123456");
            //上述写你的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); 
                client.Send(msg);
                MessageBox.Show("发送成功");

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }

        public static void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
        {

            System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.Send(message);
        }
        private  void Getinfo()
        {
            string str = label2.Text;
            MailAddress MessageFrom = new MailAddress("
videotengs1991@163.com"); //发件人邮箱地址  邮箱我都写成一样的了 不让总是报 远程连接,或是邮箱名不可用
            string MessageTo = "
videotengs1991@163.com"; //收件人邮箱地址
            string MessageSubject = "标题:" ; //Convert.ToString( label2.Text).Replace('-','*') ; //邮件主题  在这一用dos命令字符串返回值报字符串不符合?。。
            string MessageBody = "内容:" +"计算机名"+ label2.Text+ label1.Text.ToString(); //邮件内容
            if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody))
            {
                MessageBox.Show("发送成功");
            }
            else
            {
                MessageBox.Show("发送失败");
            }

        }
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="MessageFrom">发件人邮箱地址</param>
        /// <param name="MessageTo">收件人邮箱地址</param>
        /// <param name="MessageSubject">邮件主题</param>
        /// <param name="MessageBody">邮件内容</param>
        /// <returns></returns>
        public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
        {
            MailMessage message = new MailMessage();
            message.From = MessageFrom;
            message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml =false; //是否为html格式    ture 显示一长串连接的字符串   false返回和dos输出的格式
            message.Priority = MailPriority.High; //发送邮件的优先等级

            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.163.com"; //指定发送邮件的服务器地址或IP
            //sc.Port = 587; //指定发送邮件端口
            sc.UseDefaultCredentials = true;
            sc.EnableSsl = true;   // 下面的只写用户名称  省略@163.com
            sc.Credentials = new System.Net.NetworkCredential("videotengs1991", "123456");  //指定登录服务器的用户名和密码
            try
            {
                sc.Send(message); //发送邮件
                //MessageBox.Show("发送成功");
            }
            catch (Exception e)
            {

                //MessageBox.Show("发送失败");
                return false;
            }
            return true;
        }


    }
}

//这是我整理的  上面代码是网上搜集的然后整理的     有问题请指点   谢谢。

 

下载地址:http://download.csdn.net/detail/sxfgen/4027319

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值