Beispiel #1
0
 /// <summary>
 /// 创建邮件信息
 /// </summary>
 /// <returns></returns>
 private MailInfo GetMailInfo()
 {
     MailInfo info = new MailInfo();
     info.Subject = this.TxtSubject.Text;
     info.MailBody = this.TxtContent.Text;
     string selectedValue = this.RadlPriority.SelectedValue;
     if (selectedValue != null)
     {
         if (!(selectedValue == "0"))
         {
             if (selectedValue == "1")
             {
                 info.Priority = MailPriority.Low;
             }
             else if (selectedValue == "2")
             {
                 info.Priority = MailPriority.High;
             }
         }
         else
         {
             info.Priority = MailPriority.Normal;
         }
     }
     if (!string.IsNullOrEmpty(this.TxtSenderName.Text))
     {
         info.FromName = this.TxtSenderName.Text;
     }
     if (!string.IsNullOrEmpty(this.TxtSenderEmail.Text))
     {
         info.ReplyTo = new MailAddress(this.TxtSenderEmail.Text);
     }
     return info;
 }
Beispiel #2
0
        //------------------Tools
        public static MailInfo GetMailInfo(string email, string from, string subject, string content)
        {
            MailAddress address  = new MailAddress(email);
            MailInfo    mailInfo = new MailInfo();

            mailInfo.Subject    = subject;
            mailInfo.IsBodyHtml = true;
            mailInfo.FromName   = from;
            mailInfo.ToAddress  = address;
            mailInfo.MailBody   = content;
            return(mailInfo);
        }
Beispiel #3
0
        private static MailState ValidInfo(MailInfo mailInfo, MailConfig mailSettings)
        {
            MailState none = MailState.None;

            if (string.IsNullOrEmpty(mailSettings.MailFrom) || string.IsNullOrEmpty(mailSettings.MailServer))
            {
                return(MailState.MailConfigIsNullOrEmpty);
            }
            if (mailInfo.ToAddress == null)
            {
                return(MailState.NoMailToAddress);
            }
            if (string.IsNullOrEmpty(mailInfo.Subject))
            {
                return(MailState.NoSubject);
            }
            return(none);
        }
Beispiel #4
0
        /// <summary>
        /// 发送邮件(仅用于SMTP25或Explicit SSL(587端口)发送,Implicit SSL使用:SendSSLEmailL)
        /// 该方法仅用于测试,使用请用异步调用
        /// </summary>
        /// <param name="account">邮箱名</param>
        /// <param name="passwd">邮箱密码</param>
        /// <param name="server">SMTP服务器域名</param>
        /// <param name="port">SMTP为25,SSL:465,如果使用IMAP则需另设</param>
        /// <param name="mailInfo">邮件内容</param>
        /// <param name="attach">附件物理地址</param>
        /// <returns></returns>
        public static MailState SendEmail(string account, string passwd, string server, MailInfo mailInfo, string[] attach)
        {
            SmtpClient        client     = new SmtpClient(server, 25);
            NetworkCredential credential = new NetworkCredential(account, passwd);

            client.UseDefaultCredentials = true;
            client.EnableSsl             = false;
            client.Credentials           = credential.GetCredential(server, 25, "Basic");
            client.DeliveryMethod        = SmtpDeliveryMethod.Network;
            //--------------------------------------------------
            MailMessage mailMessage = GetMailMessage(mailInfo, account);

            mailMessage.BodyEncoding = Encoding.UTF8;
            mailMessage.IsBodyHtml   = true;
            foreach (string file in attach)//物理路径
            {
                if (string.IsNullOrEmpty(file))
                {
                    continue;
                }
                Attachment item = new Attachment(file);
                mailMessage.Attachments.Add(item);
            }
            client.Send(mailMessage);
            MailState state = MailState.Ok;

            return(state);
        }
Beispiel #5
0
        public static MailState SendSSLEmail(string account, string passwd, string server, MailInfo mailInfo, string[] attach)
        {
            Message mailMessage = new Message();
            //CDO.IConfiguration iConfg;
            //iConfg = oMsg.Configuration;
            //ADODB.Fields oFields;
            //oFields = iConfg.Fields;
            Configuration conf = new ConfigurationClass();

            conf.Fields[CdoConfiguration.cdoSendUsingMethod].Value  = CdoSendUsing.cdoSendUsingPort;
            conf.Fields[CdoConfiguration.cdoSMTPAuthenticate].Value = CdoProtocolsAuthentication.cdoBasic;
            conf.Fields[CdoConfiguration.cdoSMTPUseSSL].Value       = true;
            conf.Fields[CdoConfiguration.cdoSMTPServer].Value       = server;//必填,而且要真实可用
            conf.Fields[CdoConfiguration.cdoSMTPServerPort].Value   = 465;
            conf.Fields[CdoConfiguration.cdoSendEmailAddress].Value = account;
            conf.Fields[CdoConfiguration.cdoSendUserName].Value     = account; //真实的邮件地址
            conf.Fields[CdoConfiguration.cdoSendPassword].Value     = passwd;  //为邮箱密码,必须真实


            conf.Fields.Update();
            mailMessage.Subject       = mailInfo.Subject;
            mailMessage.Configuration = conf;
            //oMsg.TextBody = "Hello, how are you doing?";
            mailMessage.HTMLBody = mailInfo.MailBody;
            //TODO: Replace with your preferred Web page
            //oMsg.CreateMHTMLBody("http://www.microsoft.com",CDO.CdoMHTMLFlags.cdoSuppressNone, "", "");
            mailMessage.From = account;
            mailMessage.To   = mailInfo.ToAddress.Address;
            //oMsg.AddAttachment("C:\Hello.txt", "", "");
            foreach (string file in attach)//物理路径
            {
                if (string.IsNullOrEmpty(file))
                {
                    continue;
                }
                mailMessage.AddAttachment(file, "", "");
            }
            mailMessage.Send();
            return(MailState.Ok);
        }
Beispiel #6
0
        public static MailState Send(MailInfo mailInfo)
        {
            MailConfig mailConfig = SiteConfig.MailConfig;
            MailState mailcode = ValidInfo(mailInfo, mailConfig);

            if (mailcode == MailState.None)
            {
                SmtpClient client = new SmtpClient();
                MailMessage mailMessage = GetMailMessage(mailInfo, mailConfig);
                try
                {
                    try
                    {
                        client.Host = mailConfig.MailServer;
                        client.Port = mailConfig.Port;
                        NetworkCredential credential = new NetworkCredential(mailConfig.MailServerUserName, mailConfig.MailServerPassWord);
                        string authenticationType = string.Empty;
                        switch (mailConfig.AuthenticationType)
                        {
                            case AuthenticationType.None:
                                client.UseDefaultCredentials = false;
                                break;

                            case AuthenticationType.Basic:
                                client.UseDefaultCredentials = true;
                                authenticationType = "Basic";
                                break;

                            case AuthenticationType.Ntlm:
                                authenticationType = "NTLM";
                                break;
                        }
                        client.EnableSsl = mailConfig.EnabledSsl;
                        client.Credentials = credential.GetCredential(mailConfig.MailServer, mailConfig.Port, authenticationType);
                        client.DeliveryMethod = SmtpDeliveryMethod.Network;
                        client.Send(mailMessage);
                        mailcode = MailState.Ok;
                    }
                    catch (SmtpException exception)
                    {
                        SmtpStatusCode statusCode = exception.StatusCode;
                        if (statusCode != SmtpStatusCode.GeneralFailure)
                        {
                            if (statusCode == SmtpStatusCode.MustIssueStartTlsFirst)
                            {
                                goto Label_01D3;
                            }
                            if (statusCode == SmtpStatusCode.MailboxNameNotAllowed)
                            {
                                goto Label_01CE;
                            }
                            goto Label_01D8;
                        }
                        if (exception.InnerException is IOException)
                        {
                            mailcode = MailState.AttachmentSizeLimit;
                        }
                        else if (exception.InnerException is WebException)
                        {
                            if (exception.InnerException.InnerException == null)
                            {
                                mailcode = MailState.SmtpServerNotFind;
                            }
                            else if (exception.InnerException.InnerException is SocketException)
                            {
                                mailcode = MailState.PortError;
                            }
                        }
                        else
                        {
                            mailcode = MailState.NonsupportSsl;
                        }
                        goto Label_01FA;
                    Label_01CE:
                        mailcode = MailState.UserNameOrPasswordError;
                        goto Label_01FA;
                    Label_01D3:
                        mailcode = MailState.MustIssueStartTlsFirst;
                        goto Label_01FA;
                    Label_01D8:
                        mailcode = MailState.SendFailure;
                    }
                }
                finally
                {

                }
            }
            Label_01FA:
            mailInfo.Msg = GetMailStateInfo(mailcode);
            return mailcode;
        }
Beispiel #7
0
 private static MailState ValidInfo(MailInfo mailInfo, MailConfig mailSettings)
 {
     MailState none = MailState.None;
     if (string.IsNullOrEmpty(mailSettings.MailFrom) || string.IsNullOrEmpty(mailSettings.MailServer))
     {
         return MailState.MailConfigIsNullOrEmpty;
     }
     if (mailInfo.ToAddress == null)
     {
         return MailState.NoMailToAddress;
     }
     if (string.IsNullOrEmpty(mailInfo.Subject))
     {
         return MailState.NoSubject;
     }
     return none;
 }
Beispiel #8
0
        private static MailMessage GetMailMessage(MailInfo mailInfo, MailConfig mailSettings)
        {
            MailMessage message = new MailMessage();
            message.To.Add(mailInfo.ToAddress);

            if (mailInfo.ReplyTo != null)
            {
                message.ReplyTo = mailInfo.ReplyTo;
            }
            if (!string.IsNullOrEmpty(mailInfo.FromName))
            {
                message.From = new MailAddress(mailSettings.MailFrom, mailInfo.FromName);
            }
            else
            {
                message.From = new MailAddress(mailSettings.MailFrom);
            }
            message.Subject = mailInfo.Subject;
            message.SubjectEncoding = Encoding.UTF8;
            message.Body = mailInfo.MailBody;
            message.BodyEncoding = Encoding.UTF8;
            message.Priority = mailInfo.Priority;
            message.IsBodyHtml = mailInfo.IsBodyHtml;
            return message;
        }
Beispiel #9
0
        public static MailState Send(MailInfo mailInfo)
        {
            MailConfig mailConfig = SiteConfig.MailConfig;
            MailState  mailcode   = ValidInfo(mailInfo, mailConfig);

            if (mailcode == MailState.None)
            {
                SmtpClient  client      = new SmtpClient();
                MailMessage mailMessage = GetMailMessage(mailInfo, mailConfig);
                try
                {
                    try
                    {
                        client.Host = mailConfig.MailServer;
                        client.Port = mailConfig.Port;
                        NetworkCredential credential         = new NetworkCredential(mailConfig.MailServerUserName, mailConfig.MailServerPassWord);
                        string            authenticationType = string.Empty;
                        switch (mailConfig.AuthenticationType)
                        {
                        case AuthenticationType.None:
                            client.UseDefaultCredentials = false;
                            break;

                        case AuthenticationType.Basic:
                            client.UseDefaultCredentials = true;
                            authenticationType           = "Basic";
                            break;

                        case AuthenticationType.Ntlm:
                            authenticationType = "NTLM";
                            break;
                        }
                        client.EnableSsl      = mailConfig.EnabledSsl;
                        client.Credentials    = credential.GetCredential(mailConfig.MailServer, mailConfig.Port, authenticationType);
                        client.DeliveryMethod = SmtpDeliveryMethod.Network;
                        client.Send(mailMessage);
                        mailcode = MailState.Ok;
                    }
                    catch (SmtpException exception)
                    {
                        SmtpStatusCode statusCode = exception.StatusCode;
                        if (statusCode != SmtpStatusCode.GeneralFailure)
                        {
                            if (statusCode == SmtpStatusCode.MustIssueStartTlsFirst)
                            {
                                goto Label_01D3;
                            }
                            if (statusCode == SmtpStatusCode.MailboxNameNotAllowed)
                            {
                                goto Label_01CE;
                            }
                            goto Label_01D8;
                        }
                        if (exception.InnerException is IOException)
                        {
                            mailcode = MailState.AttachmentSizeLimit;
                        }
                        else if (exception.InnerException is WebException)
                        {
                            if (exception.InnerException.InnerException == null)
                            {
                                mailcode = MailState.SmtpServerNotFind;
                            }
                            else if (exception.InnerException.InnerException is SocketException)
                            {
                                mailcode = MailState.PortError;
                            }
                        }
                        else
                        {
                            mailcode = MailState.NonsupportSsl;
                        }
                        goto Label_01FA;
Label_01CE:
                        mailcode = MailState.UserNameOrPasswordError;
                        goto Label_01FA;
Label_01D3:
                        mailcode = MailState.MustIssueStartTlsFirst;
                        goto Label_01FA;
Label_01D8:
                        mailcode = MailState.SendFailure;
                    }
                }
                finally
                {
                }
            }
Label_01FA:
            mailInfo.Msg = GetMailStateInfo(mailcode);
            return(mailcode);
        }
Beispiel #10
0
 public static MailState SendSSLEmail(string account, string passwd, string server, MailInfo mailInfo, string[] attach)
 {
     return(MailState.Ok);
 }