发送Email方法

        /// <summary>
        /// 发送Email方法
        /// </summary>
        /// <param name="tomail">收件人列表,';'分隔</param>
        /// <param name="body">内容</param>
        /// <param name="subject">标题</param>
        /// <param name="file">附件列表,';'分隔</param>
        public static void SendEmail(string tomail, string body, string subject, string file)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

            if (tomail != "")
            {
                var strTo = tomail.Split(';');
                if (strTo.Length > 0)
                {
                    for (int i = 0; i < strTo.Length; i++)
                    {
                        if (strTo[i] != "" || strTo != null)
                        {
                            msg.To.Add(strTo[i]);
                        }
                    }
                }
            }
            else throw new Exception("收件人不可为空!");

            //收件人或者寄件人的地址
            MailAddress form1 = new MailAddress("471478656@qq.com");
            //发件人
            msg.From = form1;
            msg.Subject = subject;
            msg.IsBodyHtml = true;
            msg.Body = body;

            //附件
            Attachment a1;
            if (file != "")
            {
                var strfile = file.Split(';');
                if (strfile.Length > 0)
                {
                    for (int i = 0; i < strfile.Length; i++)
                    {
                        if (strfile[i] != "" || strfile != null)
                        {
                            a1 = new Attachment(strfile[i]);
                            msg.Attachments.Add(a1);
                        }
                    }
                }
            }
            //设置邮箱的地址或IP
            SmtpClient client = new SmtpClient("smtp.qq.com");//或者client.host="smtp.qq.com";
            client.EnableSsl = true;//QQ邮箱使用ssl加密,需要设置SmtpClient.EnableSsl 属性为True表示“指定 SmtpClient 使用安全套接字层 (SSL) 加密连接。”
            //设置邮箱端口,pop3端口:110, smtp端口是:25
            client.Port = 25;
            //要输入邮箱用户名与授权码
            client.Credentials = new NetworkCredential("471478656@qq.com", "*****");
            client.Send(msg);
        }

注意:输入邮箱用户名和授权码,而不是密码

 

posted @ 2017-01-06 20:10  花生打代码会头痛  阅读(132)  评论(0)    收藏  举报