临时需要开发的一个通过excel发送附件工资条的工具
1. 读取excel 模板邮箱发送邮件
2. 薪资项是生成的附件htm文件
3. 发送成功和失败记录有日志记载
预览:
核心代码:
//发件人地址
MailAddress from = new MailAddress(fromEmail, fromEmail);
//收件人地址
MailAddress to = new MailAddress(emailTo);
MailMessage message = new MailMessage(from, to);
message.Subject = System.Configuration.ConfigurationManager.AppSettings["SUBJECT"].ToString();//"3月薪资单、注意保密";
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = "body";
message.Body = "薪资请见附件";
string smtpserver = System.Configuration.ConfigurationManager.AppSettings["SMTP"].ToString();//"smtp.live.com";
int smtpport = 25;
bool isssl = false;
SmtpClient client = new SmtpClient(smtpserver, smtpport);
if (chbSafe.Checked)
{
isssl = true;
}
client.EnableSsl = isssl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(fromEmail, passWord);
client.Timeout = 100000000;
byte[] array = Encoding.ASCII.GetBytes(msg);
MemoryStream stream = new MemoryStream(array); //convert stream 2 string
StreamReader reader = new StreamReader(stream);
Attachment data = Attachment.CreateAttachmentFromString(msg, name + ".htm", Encoding.Default,null);
message.Attachments.Add(data);
try
{
client.Send(message);
//for (int i = 0; i < 100; i++)
{
log4net.ILog log = log4net.LogManager.GetLogger("log");
log.Info(count + " Info:" + "员工:" + name + " 邮件:" + emailTo + "-----" + "ok!");
count++;
}
}
catch (IndexOutOfRangeException e)
{
MessageBox.Show(e.Message);
}
catch (Exception ee)
{
log4net.ILog log = log4net.LogManager.GetLogger("log");
log.Error("error", new Exception("员工:" + name + " 邮件:" + emailTo + "-----" + "error!" + ee.Message));
}