Exemplo n.º 1
0
        /// <summary>
        /// 文件转为base64编码
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns>文件的base64编码</returns>
        public static string FileToBase64Str(string filePath)
        {
            string base64Str = string.Empty;

            try
            {
                using (FileStream filestream = new FileStream(filePath, FileMode.Open))
                {
                    byte[] bt = new byte[filestream.Length];

                    //调用read读取方法
                    filestream.Read(bt, 0, bt.Length);
                    base64Str = Convert.ToBase64String(bt);
                    filestream.Close();
                }

                return(base64Str);
            }
            catch (Exception)
            {
                MessageForm messageForm = new MessageForm("提醒", "附件转码出现问题", "确定");
                messageForm.ShowDialog();
                if (messageForm.DialogResult == DialogResult.Cancel)
                {
                    messageForm.Dispose();
                }
                return(base64Str);
            }
        }
Exemplo n.º 2
0
 private void EditSortFormulas()
 {
     if (File.Exists(mSortStringFileName))
     {
         MessageForm ef = new MessageForm(SystemIcons.Question);
         ef.Text = "Edit the sort formulas";
         ef.MessageText = ef.MessageText = File.ReadAllText(mSortStringFileName);
         ef.MessageEditable = true;
         ef.AuxButtonText = "&Restore Default";
         DialogResult result = ef.ShowDialog(this);
         if (result == DialogResult.OK)
         {
             string res = ef.MessageText.Replace("\r\n","\n");
             res = res.Replace("\n", Environment.NewLine); // make the text file nice for text editors
             File.WriteAllText(mSortStringFileName, res);
         }
         else if (result == DialogResult.Abort)
         {
             // restore the default sort data
             string res = PlayerSorter.sFormulasString.Replace("\r\n", "\n");
             res = res.Replace("\n", Environment.NewLine); // make the text file nice for text editors
             File.WriteAllText(mSortStringFileName, res);
         }
         ef.Dispose();
     }
     else
     {
         MessageBox.Show(this, "File: " + mSortStringFileName + " does not exist.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemplo n.º 3
0
        // 发送邮件
        public bool SendMail(string to, string subject, string content, ListView lsb_attach)
        {
            if (State == SmtpSTATE.UNCONNECTED)
            {
                if (!Login(User))
                {
                    return(false);
                }
            }
            try
            {
                //Send Email
                cmdData = "MAIL FROM: <" + User.Username + ">" + CRLF;
                szData  = Encoding.ASCII.GetBytes(cmdData.ToCharArray());
                StrmWtr.Write(szData, 0, szData.Length);
                this.getSatus();

                cmdData = "RCPT TO: <" + to + ">" + CRLF;
                szData  = Encoding.ASCII.GetBytes(cmdData.ToCharArray());
                StrmWtr.Write(szData, 0, szData.Length);
                this.getSatus();

                cmdData = "DATA" + CRLF;
                szData  = Encoding.ASCII.GetBytes(cmdData.ToCharArray());
                StrmWtr.Write(szData, 0, szData.Length);
                this.getSatus();
                //以MIME格式发送邮件
                if (lsb_attach.Items.Count > 0)
                {
                    DateTime currentTime = new DateTime();
                    currentTime = DateTime.Now;
                    string date = currentTime.ToString("r");
                    //Date:属性
                    date = System.Text.RegularExpressions.Regex.Replace(date, "GMT", "+0800");
                    //附件的base64编码
                    List <string> attachs = new List <string>();
                    foreach (ListViewItem item in lsb_attach.Items)
                    {
                        attachs.Add(FileUtil.FileToBase64Str(item.Text));
                    }
                    //分隔符,边界字符串
                    //String boundary = "_NextPart_" + GetRnd(10, true, true, true, false, "");

                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append("Date: " + date + CRLF)    //日期
                    .Append("From: " + User.Username + CRLF)
                    .Append("To: " + to + CRLF)
                    .Append("Subject: " + subject + CRLF)
                    .Append("Content-Type: multipart/mixed;" + CRLF)
                    .Append(" boundary=\"__=_Part_Boundary_001_011991.029871\"" + CRLF)
                    .Append("Mime-Version: 1.0" + CRLF + CRLF)
                    .Append("--__=_Part_Boundary_001_011991.029871" + CRLF)
                    .Append("Content-Type: text/plain; charset=\"UTF-8\"" + CRLF + CRLF)
                    .Append(content + CRLF + CRLF);
                    for (int i = 0; i < attachs.Count; i++)
                    {
                        string filePath  = lsb_attach.Items[i].Text;
                        string fileName  = Path.GetFileName(filePath);
                        string extension = Path.GetExtension(filePath);
                        stringBuilder.Append("--__=_Part_Boundary_001_011991.029871" + CRLF)
                        .Append("Content-Type: application/octet-stream;" + CRLF)
                        .Append("\tfilename=\"" + fileName + "\"" + CRLF)
                        .Append("Content-Transfer-Encoding: base64" + CRLF)
                        .Append("Content - Disposition: attachment; filename =\"" + fileName + "\"" + CRLF + CRLF)
                        .Append(attachs[i] + CRLF + CRLF);
                    }
                    stringBuilder.Append("--__=_Part_Boundary_001_011991.029871--" + CRLF + CRLF)
                    .Append(CRLF + "." + CRLF);
                    cmdData = stringBuilder.ToString();
                }
                else
                {
                    cmdData = "from: " + User.Username + CRLF
                              + "to: " + to + CRLF
                              + "subject: " + subject + CRLF + CRLF
                              + content + CRLF + "." + CRLF;
                }
                szData = Encoding.UTF8.GetBytes(cmdData.ToCharArray());
                StrmWtr.Write(szData, 0, szData.Length);
                this.getSatus();
                return(true);
            }
            catch (Exception)
            {
                MessageForm messageForm = new MessageForm("提醒", "发送邮件失败", "确定");
                messageForm.ShowDialog();
                if (messageForm.DialogResult == DialogResult.Cancel)
                {
                    messageForm.Dispose();
                }
                return(false);
            }
        }