读Excel发送工资条小工具
生活随笔
收集整理的這篇文章主要介紹了
读Excel发送工资条小工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
臨時需要開發的一個通過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));
}
完整代碼:http://files.cnblogs.com/agtaimaer/WMEmail.rar
轉載于:https://www.cnblogs.com/agtaimaer/archive/2011/04/11/2012323.html
總結
以上是生活随笔為你收集整理的读Excel发送工资条小工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红帽Linux登录密码忘了,redhat
- 下一篇: 5分钟教小白通过ipv6远程访问白群晖