C#原生郵件傳送+傳送日誌記錄
首先需要兩個引用:
using System.Net.Mail;
using System.IO;
以下為正文:
public int sendMail(string from, string to, string subject, string body, string host, string username, string password)
{
try
{
int n=0;
MailAddress fromAdd = new MailAddress(from);
MailMessage mail = new MailMessage();
mail.Subject = subject;
mail.From = new MailAddress(from);
mail.Body = body;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Host = host;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(username, password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
string[] toAdd = to.Split(';');
foreach (string temp in toAdd)
{
mail.To.Add(new MailAddress(temp));
client.Send(mail);
mail.To.Clear();
n++;
WriteInfo("傳送成功:"+temp);
}
return n;
}
catch (Exception ex)
{
throw ex;
}
}
public void WriteInfo(string errorMessage)
{
try
{
//string pathInfo = ConfigurationManager.AppSettings["ErrorsIn"];
//string ErrorTxtPath = pathInfo.ToString().Trim() + "" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
Directory.CreateDirectory("Log");//建立資料夾
string ErrorTxtPath = "Log" + "/" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
if (!File.Exists(ErrorTxtPath))
{
using (FileStream fs = File.Create(ErrorTxtPath))
{
}
}
StreamWriter writerInfo = new StreamWriter(ErrorTxtPath, true);
string errInfo = DateTime.Now.ToString() + ":工作郵件傳送日誌:" + errorMessage + "\r\n ";
writerInfo.Write(errInfo);
writerInfo.Close();
}
catch
{
}
}
相關文章
- 使用C#傳送郵件C#
- SSIS遍歷記錄傳送郵件
- AOP記錄異常郵件傳送記錄
- 郵件傳送
- 傳送郵件
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- 【工具】利用python 傳送MySQL 慢日誌郵件PythonMySql
- Laravel 傳送郵件Laravel
- PHP傳送郵件PHP
- Django——郵件傳送Django
- java郵件傳送Java
- Laravel傳送郵件Laravel
- gmail傳送郵件AI
- Oracle郵件傳送Oracle
- java傳送郵件Java
- Powershell郵件傳送
- thinkphp 郵件傳送PHP
- centos 傳送郵件CentOS
- phpcms傳送郵件PHP
- 郵件的傳送
- 利用SPF記錄缺失傳送偽造郵件
- JSP筆記-傳送郵件JS筆記
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- c# 傳送郵件程式碼,帶附件C#
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- 定時傳送郵件
- thinkjs 傳送郵件JS
- 使用phpmailer傳送郵件PHPAI
- Ubuntu的郵件傳送Ubuntu
- python傳送郵件Python
- nodejs 傳送郵件NodeJS
- 傳送郵件那些事
- javamail郵件傳送例子JavaAI
- 使用JavaMail傳送郵件JavaAI
- 使用nodemailer傳送郵件AI
- javamail傳送郵件(轉)JavaAI
- 日誌傳送部署