示例#1
0
        public static void SendMailWithTLS()
        {
            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
            string type = "tls";  //
            string body = "no ssl";

            MailBee.SmtpMail.Smtp       smtp       = new MailBee.SmtpMail.Smtp();
            MailBee.SmtpMail.SmtpServer smtpServer = new MailBee.SmtpMail.SmtpServer();
            switch (type)
            {
            case "ssl":
                smtpServer.SslMode     = MailBee.Security.SslStartupMode.OnConnect;
                smtpServer.SslProtocol = MailBee.Security.SecurityProtocol.Auto;
                smtpServer.Port        = 465;
                body = "with ssl";
                break;

            case "tls":
                smtpServer.SslMode = MailBee.Security.SslStartupMode.UseStartTlsIfSupported;
                //smtpServer.SslProtocol = MailBee.Security.SecurityProtocol.Tls12;
                smtpServer.Port = 587;
                body            = "with tls";
                break;

            default:
                smtpServer.Port = 25;
                break;
            }
            MailBee.Mime.MailMessage message = new MailBee.Mime.MailMessage();
            BuildMessage("qq", message, smtpServer);
            smtpServer.AuthMethods = MailBee.AuthenticationMethods.SaslLogin;
            smtp.SmtpServers.Add(smtpServer);
            //bool con = smtp.Connect();
            //bool login = smtp.Login();
            message.To.Add(new EmailAddress("*****@*****.**"));
            message.BodyHtmlText = body;

            smtp.Message      = message;
            smtp.Log.Enabled  = true;  //写日志
            smtp.Log.Filename = @"F:\DevProject\MailTestDemo\log.txt";
            smtp.Send();
        }
示例#2
0
 public bool SendHtmlFormattedEmailMailbee(string recepientEmail, string subject, string body)
 {
     try
     {
         MailBee.SmtpMail.Smtp.LicenseKey = "MN200-31F9F661F915F943F9D9C38AF266-7B6";//Demo Key
         MailBee.SmtpMail.Smtp ObjSmtp = new MailBee.SmtpMail.Smtp();
         ObjSmtp.Message.From.AsString = ConfigurationManager.AppSettings["UserName"].ToString();
         ObjSmtp.Message.To.AsString   = recepientEmail;
         ObjSmtp.Message.Subject       = subject;
         ObjSmtp.Message.BodyHtmlText  = body;
         SmtpServer server = new SmtpServer(ConfigurationManager.AppSettings["Host"], ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["Password"]);
         server.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
         ObjSmtp.SmtpServers.Add(server);
         ObjSmtp.Send();
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
示例#3
0
        public static void SendEmail()
        {
            MailBee.SmtpMail.Smtp       smtp   = new MailBee.SmtpMail.Smtp();
            MailBee.SmtpMail.SmtpServer server = new SmtpServer();
            server.Name        = "smtp.126.com";
            server.Port        = 25;
            server.AccountName = "frankfeng23";
            server.Password    = "******";
            server.AuthMethods = MailBee.AuthenticationMethods.SaslLogin;
            server.SslMode     = MailBee.Security.SslStartupMode.UseStartTlsIfSupported;
            smtp.SmtpServers.Add(server);

            MailMessage message = new MailMessage();

            message.Subject = "test edited sender";
            message.To.Add("*****@*****.**");
            message.From.Email   = "*****@*****.**";
            message.BodyHtmlText = "test return path";
            //message.Sender = "*****@*****.**";
            smtp.Message = message;
            smtp.Send();
        }
示例#4
0
        public static void SendMail(Account account, WebMailMessage message, string dataFolder)
        {
            WebmailResourceManager resMan;

            if (dataFolder != string.Empty)
            {
                resMan = new WebmailResourceManager(dataFolder);
            }
            else
            {
                resMan = (new WebmailResourceManagerCreator()).CreateResourceManager();
            }

            try
            {
                MailMessage messageToSend = new MailMessage();
                messageToSend.Priority    = message.MailBeeMessage.Priority;
                messageToSend.Sensitivity = message.MailBeeMessage.Sensitivity;

                messageToSend.From          = message.MailBeeMessage.From;
                messageToSend.To            = message.MailBeeMessage.To;
                messageToSend.Cc            = message.MailBeeMessage.Cc;
                messageToSend.Bcc           = message.MailBeeMessage.Bcc;
                messageToSend.Subject       = message.MailBeeMessage.Subject;
                messageToSend.ConfirmRead   = message.MailBeeMessage.ConfirmRead;
                messageToSend.Date          = DateTime.Now;
                messageToSend.BodyHtmlText  = message.MailBeeMessage.BodyHtmlText;
                messageToSend.BodyPlainText = message.MailBeeMessage.BodyPlainText;



                if (HttpContext.Current != null)
                {
                    string str = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    messageToSend.Headers.Add("X-Originating-IP", str, false);
                }

                foreach (Attachment attach in message.MailBeeMessage.Attachments)
                {
                    messageToSend.Attachments.Add(attach);
                }

                WebmailSettings settings;
                if (dataFolder != string.Empty)
                {
                    settings = (new WebMailSettingsCreator()).CreateWebMailSettings(dataFolder);
                }
                else
                {
                    settings = (new WebMailSettingsCreator()).CreateWebMailSettings();
                }


                MailBee.SmtpMail.Smtp.LicenseKey = settings.LicenseKey;


                MailBee.SmtpMail.Smtp objSmtp = new MailBee.SmtpMail.Smtp();

                if (settings.EnableLogging)
                {
                    objSmtp.Log.Enabled = true;

                    if (dataFolder != string.Empty)
                    {
                        objSmtp.Log.Filename = Path.Combine(dataFolder, Constants.logFilename);
                    }
                    else
                    {
                        string dataFolderPath = Utils.GetDataFolderPath();
                        if (dataFolderPath != null)
                        {
                            objSmtp.Log.Filename = Path.Combine(dataFolderPath, Constants.logFilename);
                        }
                    }
                }

                SmtpServer server = new SmtpServer();
//                server.SmtpOptions = ExtendedSmtpOptions.NoChunking;
                server.Name        = !string.IsNullOrEmpty(account.MailOutgoingHost) ? account.MailOutgoingHost : account.MailIncomingHost;
                server.Port        = account.MailOutgoingPort;
                server.AccountName = !string.IsNullOrEmpty(account.MailOutgoingLogin) ? account.MailOutgoingLogin : account.MailIncomingLogin;
                server.Password    = (!string.IsNullOrEmpty(account.MailOutgoingPassword)) ? account.MailOutgoingPassword : account.MailIncomingPassword;

                if (account.MailOutgoingAuthentication)
                {
                    server.AuthMethods = AuthenticationMethods.Auto;
                    server.AuthOptions = AuthenticationOptions.PreferSimpleMethods;
                }

                if (server.Port == 465)
                {
                    server.SslMode     = SslStartupMode.OnConnect;
                    server.SslProtocol = SecurityProtocol.Auto;
                    server.SslCertificates.AutoValidation = CertificateValidationFlags.None;
                }

                if (server.Port == 587 && Constants.UseStartTLS)
                {
                    server.SslMode = SslStartupMode.UseStartTlsIfSupported;
                }

                objSmtp.SmtpServers.Add(server);

                Encoding outgoingCharset = Utils.GetOutCharset(account.UserOfAccount.Settings);
                messageToSend.Charset = outgoingCharset.HeaderName;

                objSmtp.Message = messageToSend;

                try
                {
                    objSmtp.Connect();

                    objSmtp.Send();
                }
                catch (MailBeeGetHostNameException ex)
                {
                    Log.WriteException(ex);
                    throw new WebMailException(resMan.GetString("ErrorSMTPConnect"));
                }
                catch (MailBeeConnectionException ex)
                {
                    Log.WriteException(ex);
                    throw new WebMailException(resMan.GetString("ErrorSMTPConnect"));
                }
                catch (MailBeeSmtpLoginBadCredentialsException ex)
                {
                    Log.WriteException(ex);
                    throw new WebMailException(resMan.GetString("ErrorSMTPAuth"));
                }
                catch (MailBeeException ex)
                {
                    Log.WriteException(ex);
                    throw new WebMailMailBeeException(ex);
                }
                finally
                {
                    if (objSmtp.IsConnected)
                    {
                        objSmtp.Disconnect();
                    }
                }
            }
            catch (MailBeeException ex)
            {
                Log.WriteException(ex);
                throw new WebMailMailBeeException(ex);
            }
        }