Exemplo n.º 1
0
        public void Send(bool notifySite)
        {
            if (!string.IsNullOrEmpty(ToMailAddress))
            {
                try
                {
                    MailAddress fromAddress;
                    fromAddress = new MailAddress(CompanyMailAuto, CompanyName);

                    //handle multiple to: addresses
                    MailAddress toAddress;
                    string[]    toAddresses = ToMailAddress.Split(',', ';');
                    toAddress = new MailAddress(toAddresses[0]);                        //take the first one as the main address

                    MailMessage message = new MailMessage(fromAddress, toAddress);

                    //check if multiple TO addresses, then add after 2nd one
                    for (int i = 1; i < toAddresses.Count <string>(); i++)
                    {
                        message.To.Add(toAddresses[i]);
                    }

                    //check if CC is available. Accepts multiple separated with a (",")
                    if (!string.IsNullOrEmpty(CcMailAddress))
                    {
                        message.CC.Add(CcMailAddress);
                    }

                    // if notify site, add company email in Bcc
                    if (notifySite)
                    {
                        message.Bcc.Add(CompanyMail);
                    }

                    //prepare the message
                    Message += string.Format("<p>Copyright &copy; {0} <br /> <a href='http://{1}'>{1}</a></p>",
                                             CompanyName,
                                             CompanyWebsite
                                             );

                    message.Subject    = Subject;
                    message.Body       = Message;
                    message.IsBodyHtml = !IsBodyText;

                    SmtpClient client = new SmtpClient(this.Host);
                    if (EnableSsl.HasValue)
                    {
                        client.EnableSsl = EnableSsl.Value;
                    }
                    client.Credentials = new NetworkCredential(this.m_SmtpUserName, this.m_SmtpUserPassword);//要身份验证

                    client.Send(message);
                }
                catch (Exception e)
                {
                    BtVideo.Helpers.GlobalHelper.WriteInLog(e.Message);
                }
            }
        }
Exemplo n.º 2
0
        public void Send(bool notifySite)
        {
            if (!string.IsNullOrEmpty(ToMailAddress))
            {
                MailAddress fromAddress;
                fromAddress = new MailAddress(CompanyMailAuto, CompanyName);

                //handle multiple to: addresses
                MailAddress toAddress;
                string[]    toAddresses = ToMailAddress.Split(',', ';');
                toAddress = new MailAddress(toAddresses[0]);                    //take the first one as the main address

                MailMessage message = new MailMessage(fromAddress, toAddress);

                //check if multiple TO addresses, then add after 2nd one
                for (int i = 1; i < toAddresses.Count <string>(); i++)
                {
                    message.To.Add(toAddresses[i]);
                }

                //check if CC is available. Accepts multiple separated with a (",")
                if (!string.IsNullOrEmpty(CcMailAddress))
                {
                    message.CC.Add(CcMailAddress);
                }

                // if notify site, add company email in Bcc
                if (notifySite)
                {
                    message.Bcc.Add(CompanyMail);
                }

                //prepare the message
                Message += string.Format("<p>Copyright &copy; {0} <br /> <a href='http://{1}'>{1}</a></p>",
                                         CompanyName,
                                         CompanyWebsite
                                         );

                message.Subject    = Subject;
                message.Body       = Message;
                message.IsBodyHtml = !IsBodyText;

                SmtpClient client = new SmtpClient();
                client.Send(message);
            }
        }
Exemplo n.º 3
0
        public async override Task OnShare(string text, string imageFilePath)
        {
            string[] attachmentPaths = null;
            if (File.Exists(imageFilePath))
            {
                attachmentPaths = new[] { imageFilePath }
            }
            ;

            string mailPassword = Decrypt(Password);
            string mailUserName = Decrypt(UserName);

            if (Server.Trim().Length == 0 || Port == 0 || mailUserName.Trim().Length == 0 || mailPassword.Trim().Length == 0)
            {
                Cbi.Log.Process(typeof(Resource), "CoreGlobalsSendMail", null, Cbi.LogLevel.Error, Cbi.LogCategories.Default);                 // Don't do LogLevel.Alert or you'll get a recursive call to email the alert
                return;
            }

            List <FileStream> attachmentStreams = new List <FileStream>();
            MailMessage       smtpMail          = new MailMessage
            {
                Body       = text,
                From       = new MailAddress(FromMailAddress, SenderDisplayName),
                IsBodyHtml = IsBodyHtml,
                Subject    = Subject,
            };

            foreach (string tmp in ToMailAddress.Split(new char[] { ',', ';' }))
            {
                MailAddress to;
                try
                {
                    to = new MailAddress(tmp);
                    smtpMail.To.Add(to);
                }
                catch (FormatException) { continue; }
            }

            if (!string.IsNullOrEmpty(imageFilePath))
            {
                foreach (string attachmentPath in attachmentPaths)
                {
                    if (!string.IsNullOrEmpty(attachmentPath))
                    {
                        string tmpFile = string.Format(Core.Globals.GeneralOptions.CurrentCulture, @"{0}tmp\{1}", Core.Globals.UserDataDir, Guid.NewGuid().ToString("N"));
                        File.Copy(attachmentPath, tmpFile);

                        FileStream fs = new FileStream(tmpFile, FileMode.Open, FileAccess.Read);
                        attachmentStreams.Add(fs);
                        smtpMail.Attachments.Add(new Attachment(fs, new FileInfo(attachmentPath).Name));
                    }
                }
            }

            try
            {
                using (SmtpClient smtp = new SmtpClient(Server, Port))
                {
                    smtp.EnableSsl             = UseSSL;
                    smtp.Timeout               = 120 * 1000;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials           = new NetworkCredential(mailUserName, mailPassword);
                    smtp.SendCompleted        += (o, e) =>
                    {
                        if (e.Error != null)
                        {
                            LogAndPrint(typeof(Custom.Resource), "ShareMailSendError", new[] { e.Error.Message }, Cbi.LogLevel.Error);
                        }
                        else
                        {
                            LogAndPrint(typeof(Custom.Resource), "ShareMailSentSuccessfully", new[] { Name }, Cbi.LogLevel.Information);
                        }

                        foreach (FileStream fs in attachmentStreams)
                        {
                            string tmp = fs.Name;
                            fs.Close();
                            if (File.Exists(tmp))
                            {
                                File.Delete(tmp);
                            }
                        }
                    };

                    await smtp.SendMailAsync(smtpMail);
                }
            }
            catch (SmtpException ex)
            {
                Exception innerEx = ex.InnerException;
                string    error   = ex.Message;
                while (innerEx != null)
                {
                    error  += " " + innerEx.Message;
                    innerEx = innerEx.InnerException;
                }

                Log(string.Format(Custom.Resource.ShareMailException, error), Cbi.LogLevel.Error);
            }
            finally
            {
                Subject       = string.Empty;
                ToMailAddress = string.Empty;
            }
        }