Ejemplo n.º 1
0
        public static bool Send(string host, int port, string fromEmail, string fromName, string pwd, string path, string toUser, string title, string body)
        {
            //检测附件大小 发件必需小于10M 否则返回  不会执行以下代码
            if (path != "")
            {
                if (!Attachment_MaiInit(path))
                {
                    return(false);
                }
            }
            if (host == "")
            {
                MessageBoxHelper.Show("请输入SMTP服务器名!");
                return(false);
            }


            if (fromEmail == "")
            {
                MessageBoxHelper.Show("请输入发件人邮箱地址!");
                return(false);
            }
            if (pwd == "")
            {
                MessageBoxHelper.Show("请输入发件人邮箱密码!");
                return(false);
            }

            if (toUser.Length == 0)
            {
                MessageBoxHelper.Show("无效收件人!" + toUser);
                return(false);
            }
            SmtpClient  smtpClient       = null; //设置SMTP协议
            MailAddress mailAddress_from = null; //设置发信人地址  当然还需要密码
            MailMessage mailMessage_Mai  = null;

            try
            {
                smtpClient         = new SmtpClient();
                smtpClient.Host    = host; //指定SMTP服务名  例如QQ邮箱为 smtp.qq.com 新浪cn邮箱为 smtp.sina.cn等
                smtpClient.Port    = port; //指定端口号
                smtpClient.Timeout = 0;    //超时时间
            }
            catch (Exception Ex)
            {
                MessageBoxHelper.Show("邮件发送失败,请确定SMTP服务名是否正确!" + "\n" + "技术信息:\n" + Ex.Message);
                return(false);
            }
            try
            {
                mailAddress_from = new System.Net.Mail.MailAddress(fromEmail, fromName);
                //指定发件人信息  包括邮箱地址和邮箱密码
                smtpClient.Credentials = new System.Net.NetworkCredential(mailAddress_from.Address, pwd);
            }
            catch (Exception Ex)
            {
                MessageBoxHelper.Show("邮件发送失败,请确定发件邮箱地址和密码的正确性!" + "\n" + "技术信息:\n" + Ex.Message);
                return(false);
            }
            //清空历史发送信息 以防发送时收件人收到的错误信息(收件人列表会不断重复)
            //mailMessage_Mai.To.Clear();
            mailMessage_Mai = new MailMessage();
            string[] toUsers = toUser.Split(';');
            //添加收件人邮箱地址
            foreach (string str in toUsers)
            {
                mailMessage_Mai.To.Add(new MailAddress(str));
            }
            //MessageBox.Show("收件人:" + dataGridViewX1.Rows.Count.ToString() + "  个");

            //发件人邮箱
            mailMessage_Mai.From = mailAddress_from;
            //邮件主题
            mailMessage_Mai.Subject         = title;
            mailMessage_Mai.SubjectEncoding = System.Text.Encoding.UTF8;
            //邮件正文
            mailMessage_Mai.Body         = body;
            mailMessage_Mai.BodyEncoding = System.Text.Encoding.UTF8;
            //清空历史附件  以防附件重复发送
            mailMessage_Mai.Attachments.Clear();
            //添加附件
            mailMessage_Mai.Attachments.Add(new Attachment(path, MediaTypeNames.Application.Octet));
            //注册邮件发送完毕后的处理事件
            // smtpClient.SendCompleted += callback;
            //开始发送邮件
            //smtpClient.SendAsync(mailMessage_Mai, "000000000");
            smtpClient.Send(mailMessage_Mai);
            return(true);
        }