class Program
{
private static string baseDir = "" ;
private static string email = "" ;
static void Main( string [] args)
{
Console.WriteLine( "--- 开始执行 --- " );
baseDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
email = ConfigurationManager.AppSettings[ "email" ];
var date = DateTime.Now.ToString( "yyyy-MM-dd" );
FileStream fs = new FileStream($ "{baseDir}\\[Log]{date}.txt" , FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine( "===== 发送日志 =====" );
List<Contact> contacts = GetContacts();
var smtpClient = GetSmtpClient();
foreach ( var contact in contacts)
{
SendMail(smtpClient, contact, sw);
}
sw.WriteLine( "===== 执行完成 =====" );
sw.Flush();
sw.Close();
fs.Close();
Console.WriteLine( "--- 执行完成 --- " );
Console.ReadLine();
}
private static SmtpClient GetSmtpClient()
{
string server = ConfigurationManager.AppSettings[ "server" ];
string port = ConfigurationManager.AppSettings[ "port" ];
string password = ConfigurationManager.AppSettings[ "password" ];
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = server;
smtpClient.Port = Convert.ToInt32(port);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true ;
smtpClient.Credentials = new NetworkCredential(email, password);
return smtpClient;
}
private static void SendMail(SmtpClient smtpClient, Contact contact, StreamWriter sw)
{
try
{
var mailMessage = GetMailMessage(contact);
smtpClient.Send(mailMessage);
Console.WriteLine($ " --- 发送成功, Email = {contact.Email} --- " );
sw.WriteLine($ " --- 发送成功, Email = {contact.Email} --- " );
}
catch (Exception ex)
{
Console.WriteLine($ " === 发送失败, Email = {contact.Email} === " );
sw.WriteLine($ " === 发送失败, Email = {contact.Email} === " );
}
}
private static MailMessage GetMailMessage(Contact contact)
{
string subject = ConfigurationManager.AppSettings[ "subject" ];
string introduce = ConfigurationManager.AppSettings[ "introduce" ];
string link = ConfigurationManager.AppSettings[ "link" ];
string content = "<p style=\"font - size:16px\">Dear " + contact.Name + " , </p> " +
"<p style=\"font - size:16px\">" + introduce + "</p>" +
"<p style=\"font - size:16px\">报名链接:<a target=\"_blank\" href=\"" + link + "\">" + link + "</a></p>"
+ GetContent();
MailMessage mailMessage = new MailMessage(email, contact.Email);
mailMessage.Subject = subject;
mailMessage.Body = content;
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.IsBodyHtml = true ;
mailMessage.Priority = MailPriority.Normal;
return mailMessage;
}
private static string GetContent()
{
var dir = baseDir + "\\content.txt" ;
StreamReader sr = new StreamReader(dir, Encoding.UTF8);
string content = sr.ReadToEnd();
sr.Close();
return content;
}
private static List<Contact> GetContacts()
{
List<Contact> contacts = new List<Contact>();
var dir = baseDir + "\\contacts.txt" ;
StreamReader sr = new StreamReader(dir, Encoding.UTF8);
string line;
while ((line = sr.ReadLine()) != null )
{
line = line.Replace( "," , "," );
var contact = line.Split( new [] { "," }, StringSplitOptions.RemoveEmptyEntries);
if (contact.Length == 2 && ! string .IsNullOrEmpty(contact[0]) && ! string .IsNullOrEmpty(contact[1]))
{
contacts.Add( new Contact() {Name = contact[0], Email = contact[1]});
}
}
sr.Close();
return contacts;
}
public class Contact
{
public string Name { get ; set ; }
public string Email { get ; set ; }
}
}
|