Sometimes, it's convenient to do some tasks through background job. In other words, through Scheduled Tasks. If you really do so, it's difficult to know whether the job executed successfully. However, we can let system send mail automatically to remind us when the job failed. The below code may help you.
Instance 1:
using System;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace OF_Common
{
public class MailHelper
{
public bool SendMail(ArrayList arrMailTo, ArrayList arrCCMailTo, string strFrom, string strSMTPServer, string strSubject, string strBody, ArrayList arrAttachment)
{
bool sendCheck = false;
if (arrMailTo.Count != 0)
{
string strMailTo = "";
foreach (string MailTo in arrMailTo)
{
strMailTo += MailTo + ";";
}
MailMessage bjmail = new MailMessage(strFrom, strMailTo.Substring(0, strMailTo.Length - 1), strSubject, strBody);
if (arrCCMailTo != null)
{
foreach (string CCMailTo in arrCCMailTo)
{
objmail.CC.Add(CCMailTo);
}
}
SmtpClient bjSMTP = new SmtpClient(strSMTPServer);
if (arrAttachment != null)
{
foreach (string Attachment in arrAttachment)
{
objmail.Attachments.Add(new Attachment(Attachment));
}
}
try
{
objSMTP.Send(objmail);
sendCheck = true;
}
catch //(Exception Ex)
{
sendCheck = false;
}
}
return sendCheck;
}
}
}
Instance 2:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Net.Mail;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace Myspace
{
class clsSendMail
{
public static string defaultSendName = "……";
public static string defaultMailTitle = "mail title";
public static int mailPority = 0;//0:normal,1:low,2:high
public static string[] sendMailgroup ={……};
public static string[] copyMailGroup ={ ……};
public static string[] arrAttachment ={ "" };
public static string strBodyType = "HTML";
private static string mailServer = "……";
private string filePath = null;
private string preMailBody = null;
private string suffMailBody = null;
private string sendMailBody = null;
public void sendMail()
{
try
{
filePath = @"E:\……\";
preMailBody = "
public static bool SendMailGroup(string subject, string body, ArrayList attachFilePath)
{
bool sendCheck = false;
if (sendMailgroup.Length > 0)
{
MailAddress from = new MailAddress(defaultSendName, defaultMailTitle);
MailMessage message = new MailMessage();
message.From = from;
foreach (string to in sendMailgroup)
{
if (to != "")
{
message.To.Add(to);
}
}
foreach (string cc in copyMailGroup)
{
if (cc != "")
{
message.CC.Add(cc);
}
}
foreach (string attachFile in attachFilePath)
{
message.Attachments.Add(new Attachment(attachFile));
}
message.Subject = subject;
message.Body = body;
if (strBodyType.ToUpper() == "HTML")
{
message.IsBodyHtml = true;
}
else
{
message.IsBodyHtml = false;
}
switch (mailPority)
{
case 0:
message.Priority = MailPriority.Normal;
break;
case 1:
message.Priority = MailPriority.Low;
break;
case 2:
message.Priority = MailPriority.High;
break;
default:
message.Priority = MailPriority.Normal;
break;
}
try
{
SmtpClient client = new SmtpClient(mailServer);
client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
client.Send(message);
message.Attachments.Clear();
message.Attachments.Dispose();
message.Dispose();
sendCheck = true;
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
}
return sendCheck;
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15053694/viewspace-442078/ ,如需转载,请注明出处,否则将追究法律责任。