예제 #1
0
        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="emailEntity">EmailEntity</param>
        /// <returns></returns>
        public static bool Send(EmailEntity emailEntity)
        {
            SmtpClient client = null;

            try
            {
                List <string> receiverList   = emailEntity.ReceiverList;
                List <string> attachmentList = emailEntity.AttachmentList;

                MailMessage mailMessage = new MailMessage();
                // 设置发件人
                mailMessage.From = new MailAddress(emailEntity.Address);
                // 设置收件人
                if (receiverList != null && receiverList.Count > 0)
                {
                    foreach (string receiver in receiverList)
                    {
                        mailMessage.To.Add(receiver);
                    }
                }
                // 设置邮件标题
                mailMessage.Subject         = emailEntity.Subject;
                mailMessage.SubjectEncoding = emailEntity.SubjectEncoding;
                // 设置邮件内容
                mailMessage.Body         = emailEntity.Content;
                mailMessage.Body         = emailEntity.Content;
                mailMessage.BodyEncoding = emailEntity.ContentEncoding;
                // 设置附件
                if (attachmentList != null && attachmentList.Count > 0)
                {
                    foreach (string attachment in attachmentList)
                    {
                        mailMessage.Attachments.Add(new Attachment(attachment));
                    }
                }
                // 设置优先级
                mailMessage.Priority = emailEntity.Priority;
                // 设置邮件发送服务器
                client = new SmtpClient(emailEntity.Smtp, emailEntity.Port);
                // 设置发件人邮箱账号和密码
                client.Credentials = new NetworkCredential(emailEntity.Account, emailEntity.Password);
                // 是否启用安全发送
                client.EnableSsl = emailEntity.IsSsl;
                // 发送邮件
                client.Send(mailMessage);
                return(true);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="sendAddress">发件人地址</param>
        /// <param name="receiverAddress">收件人地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="content">邮件内容</param>
        /// <param name="account">发件人帐号</param>
        /// <param name="accountPassword">发件人密码</param>
        /// <param name="smtp">Smtp</param>
        /// <param name="smtpPort">Smtp 端口</param>
        /// <param name="isSsl">是否安全发送</param>
        /// <returns></returns>
        public static bool Send(string sendAddress, string receiverAddress, string subject, string content, string account, string accountPassword, string smtp = "smtp.qq.com", int smtpPort = 25, bool isSsl = false)
        {
            EmailEntity entity = new EmailEntity()
            {
                Address      = sendAddress,
                ReceiverList = new List <string>()
                {
                    receiverAddress
                },
                Subject  = subject,
                Content  = content,
                Account  = account,
                Password = accountPassword,
                Smtp     = smtp,
                Port     = smtpPort,
                IsSsl    = isSsl
            };

            return(Send(entity));
        }