Example #1
0
        static System.Net.Mail.MailMessage GetMailMessage(this SendItem item)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            foreach (EmailItem email in item.To)
            {
                msg.To.Add(email.Email);
            }
            foreach (EmailItem email in item.Cc)
            {
                msg.CC.Add(email.Email);
            }
            foreach (EmailItem email in item.BCc)
            {
                msg.Bcc.Add(email.Email);
            }

            if (item.From.Count > 0)
            {
                msg.From = new System.Net.Mail.MailAddress(item.From[0].Email, item.From[0].Name, System.Text.Encoding.GetEncoding(item.From[0].Encoding));
            }
            msg.Subject         = item.Subject.Content;
            msg.SubjectEncoding = System.Text.Encoding.GetEncoding(item.Subject.Encoding);
            msg.Body            = item.Body.Content;
            msg.BodyEncoding    = System.Text.Encoding.GetEncoding(item.Body.Encoding);
            msg.IsBodyHtml      = item.Body.IsBodyHtml;
            msg.Priority        = item.Priority;
            return(msg);
        }
Example #2
0
        public static void SendLocal(this SendItem item)
        {
            System.Net.Mail.MailMessage msg = GetMailMessage(item);

            SmtpClient client = new SmtpClient();

            client.Host = item.Host.Address;
            object userstate = msg;

            client.Send(msg);
        }
Example #3
0
        public static void SendExchange(this SendItem item)
        {
            System.Net.Mail.MailMessage msg = GetMailMessage(item);
            SmtpClient client = new SmtpClient(item.Host.Address);

            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials    = (item.Credentials.UserID.Length > 0)?(new System.Net.NetworkCredential(item.Credentials.UserID, item.Credentials.Password)):
                                    System.Net.CredentialCache.DefaultNetworkCredentials;
            client.EnableSsl = item.Host.EnableSsl;
            client.Send(msg);
        }
Example #4
0
        public static void SendSmtpServer(this SendItem item)
        {
            System.Net.Mail.MailMessage msg = GetMailMessage(item);

            SmtpClient client = new SmtpClient();

            client.Credentials = new System.Net.NetworkCredential(item.Credentials.UserID, item.Credentials.Password);
            client.Host        = item.Host.Address;
            client.Port        = Convert.ToInt32(item.Host.Port);
            client.EnableSsl   = item.Host.EnableSsl;
            object userstate = msg;

            client.Send(msg);
        }
Example #5
0
        public static void Send(this SendItem item)
        {
            switch (item.Host.Type)
            {
            case HostType.Local:
                SendLocal(item);
                break;

            case HostType.Smtp:
                SendSmtpServer(item);
                break;

            case HostType.Exchange:
            default:
                SendExchange(item);
                break;
            }
        }
Example #6
0
        public void SendMail(string title, string content, string to, string user, string password)
        {
            try
            {
                if (Host == null)
                {
                    throw new Exception("邮件服务器信息Host没有设置");
                }

                if (string.IsNullOrWhiteSpace(title))
                {
                    throw new ArgumentException("请设置邮件标题", title);
                }
                if (string.IsNullOrWhiteSpace(content))
                {
                    throw new ArgumentException("请设置邮件内容", content);
                }
                if (string.IsNullOrWhiteSpace(to))
                {
                    throw new ArgumentException("邮件接受者不能为空", to);
                }
                if (string.IsNullOrWhiteSpace(user))
                {
                    throw new ArgumentException("邮件服务器的身份验证信息用户不能为空", user);
                }
                if (string.IsNullOrWhiteSpace(password))
                {
                    throw new ArgumentException("邮件服务器的身份验证信息密码不能为空", password);
                }

                var sendItem = new SendItem
                {
                    To          = to.CreateItems().ToList(),
                    Host        = Host,
                    Credentials = new Credentials {
                        UserID = user.Trim(), Password = password
                    },
                    From = new List <EmailItem> {
                        new EmailItem {
                            Email = user
                        }
                    },
                    Body = new Body {
                        Content = content
                    },
                    Subject = new Subject {
                        Content = title.Trim()
                    }
                };

                sendItem.Send();
                this.Log(string.Format("SendMail Server:{0},Port:{1},Sender:{2},To:{3},Title:{4}",
                                       Host.Address, Host.Port, user, to, title));
            }
            catch (Exception ex)
            {
                this.Log(string.Format("SendMail Error Server:{0},Port:{1},Sender:{2},To:{3},Title:{4},Error:{5}",
                                       Host.Address, Host.Port, user, to, title, ex.Message));
                throw;
            }
        }