AutoPackage/AutoPekage/frmPackage.cs

183 lines
6.1 KiB
C#
Raw Normal View History

2017-02-05 15:30:23 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;
using System.Linq;
namespace AutoPekage
{
public partial class frmPackage : Form
{
List<string> fileList;
public frmPackage(List<string> fileList)
{
this.fileList = fileList;
InitializeComponent();
cbSender.DataSource = LiteDbHelper.GetAllSender();
cbSender.DisplayMember = "Name";
cbSender.ValueMember = "Id";
cbMailTo.DataSource = LiteDbHelper.GetAllMailTo();
cbMailTo.DisplayMember = "MailTo";
var dao = LiteDbHelper.Settings;
cbSendMail.Checked = dao.SendMail;
if (dao.LastPath == null)
{
rbDesktop.Checked = true;
}
else
{
rbCustom.Checked = true;
txtPackagePath.Text = dao.LastPath;
}
}
private void btnSelectPath_Click(object sender, EventArgs e)
{
var dao = LiteDbHelper.Settings;
sfdPackagePath.FileName = dao.DefaultFileName;
if (sfdPackagePath.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtPackagePath.Text = sfdPackagePath.FileName;
}
}
private void rbDesktop_CheckedChanged(object sender, EventArgs e)
{
if (rbDesktop.Checked)
{
txtPackagePath.Enabled = false;
btnSelectPath.Enabled = false;
txtCaption.Text = LiteDbHelper.Settings.DefaultFileName;
}
else
{
txtPackagePath.Enabled = true;
btnSelectPath.Enabled = true;
if (txtPackagePath.Text != null)
{
txtCaption.Text = Path.GetFileName(txtPackagePath.Text);
}
}
}
private void txtPackagePath_DoubleClick(object sender, EventArgs e)
{
btnSelectPath.PerformClick();
}
private void btnOK_Click(object sender, EventArgs e)
{
string filePath;
var dao = LiteDbHelper.Settings;
if (rbDesktop.Checked)
{
filePath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), dao.DefaultFileName);
if (dao.LastPath != null)
{
dao.LastPath = null;
LiteDbHelper.Settings = dao;
}
}
else
{
filePath = txtPackagePath.Text;
if (dao.LastPath != filePath)
{
dao.LastPath = filePath;
LiteDbHelper.Settings = dao;
}
}
if (File.Exists(filePath))
{
if (MessageBox.Show("该文件已存在,是否替换?", "自动打包", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
File.Delete(filePath);
}
else
{
return;
}
}
ZipControl.Zip(filePath, fileList);
if (cbSendMail.Checked)
{
var info = LiteDbHelper.GetSenderById((int)cbSender.SelectedValue);
MailMessage msg = new MailMessage();
msg.To.Add(cbMailTo.Text);
msg.From = new MailAddress(info.UserName, info.NickName, System.Text.Encoding.UTF8);
msg.Subject = txtCaption.Text;//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = txtContext.Text;//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.Normal;//邮件优先级
msg.Attachments.Add(new Attachment(filePath));
SmtpClient client = new SmtpClient(info.StmpServer);
client.Credentials = new NetworkCredential(info.UserName, info.Password);
client.EnableSsl = true;
try
{
client.Send(msg);
//简单一点儿可以client.Send(msg);
MessageBox.Show("打包并发送邮件成功");
if (LiteDbHelper.GetAllMailTo().Where(x => x.MailTo == cbMailTo.Text).Count() == 0)
{
LiteDbHelper.AddMailTo(cbMailTo.Text);
cbMailTo.DataSource = LiteDbHelper.GetAllMailTo();
}
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message, "打包成功,发送邮件出错");
return;
}
}
else
{
MessageBox.Show("打包完成");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void cbSendMail_CheckedChanged(object sender, EventArgs e)
{
if (cbSendMail.Checked)
{
gbMail.Enabled = true;
}
else
{
gbMail.Enabled = false;
}
}
private void btnAddSender_Click(object sender, EventArgs e)
{
new frmAddSender().ShowDialog();
cbSender.DataSource = LiteDbHelper.GetAllSender();
}
private void txtPackagePath_TextChanged(object sender, EventArgs e)
{
if (txtPackagePath.Text != null)
{
txtCaption.Text = Path.GetFileName(txtPackagePath.Text);
}
}
}
}