以下代码从表中获取收件人电子邮件,电子邮件主题和电子邮件正文,并创建电子邮件并将其发送到提取目录。有一个我没有在这里包含的数据库连接类,但我在下面的代码中调用它。 下面的代码运行正常,但我想知道它是否可以改进运行更快考虑它将用于发送10000多封电子邮件。
提前感谢您的帮助。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace SendEmails
{
class SendEmail
{
private class EmailsList
{
private class listData
{
public string email;
public string email_header;
public string email_body;
public string email_guid;
}
public void data()
{
SqlDataReader sqlData;
ArrayList Emaillist = new ArrayList();
sqlData = new SqlCommand("SELECT email, email_header, email_body, email_guid FROM dbo.vw_emails ", con.openconnection()).ExecuteReader();
// loop through the emails table and load arraylist
while (sqlData.Read())
{
listData itemData = new listData();
itemData.email = sqlData[0].ToString();
itemData.email_header = sqlData[1].ToString();
itemData.email_body = sqlData[2].ToString();
itemData.email_guid = sqlData[3].ToString();
Emaillist.Add(itemData);
}
sqlData.Close();
con.closeconnection
foreach (listData itemData in Eamillist)
{
//SEND EMAIL *****************************/
spSendMail(itemData.email, itemData.email_header, "me@yahoo.com", itemData.email_body, itemData.email_guid);
//DO THE UPDATE *********************/
SqlCommand cmd = new SqlCommand("up_emailLog", con.openconncetion());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@email_GUID", itemData.email_guid);
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
con.CloseConnection();
}
}
}
static void Main(string[] args)
{
try
{
EmailsList sEmails = new EmailsList();
sEmails.data();
}
catch (Exception ex)
{
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo.Err_Log](exception, insdt) VALUES('" + ex.Message.ToString() + "','" + "','"+ DateTime.Now + "')", Con.OpenConnection());
cmd.ExecuteNonQuery();
con.CloseConnection();
}
}
// Send Email Method
public static void spSendMail(string recipients, string subject, string from, string body, string email_guid)
{
try
{
using (MailMessage mailMessage = new MailMessage(from, recipients))
{
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("xxxxxx.xxxx.zo");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpClient.PickupDirectoryLocation = "\\\\exchange_server\\pickup";
//Create eml file and send it to pickup directory
smtpClient.Send(mailMessage);
}
}
catch (Exception ex)
{
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo.Err_Log](exception, communication, insdt) VALUES('" + ex.Message.ToString() +"','" + recipients.ToString() + "','" + DateTime.Now + "')", con.OpenConnection());
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
Con.CloseConnection();
}
}
}
}
答案 0 :(得分:0)
如果您可以使用.NET 2.0或更高版本,那么您可以使用SmtpClient.SendAsync
方法,该方法将创建一个新线程来发送电子邮件并释放主线程以进行更多处理。
阅读SmtpClient.SendAsync method了解文档。
如果您使用的是.NET 2.0或更高版本,您还应该考虑使用Generics,它将为您的Emaillist
对象提供类型安全性,但不会提高性能,因为您没有获得通常的拆箱效果ArrayList
,因为您没有使用值类型。
以下是您的EmailList
代码使用Generics List<T>
时的样子:
List<listData> Emaillist = new List<listData>();
while (sqlData.Read())
{
listData itemData = new listData();
itemData.email = sqlData[0].ToString();
itemData.email_header = sqlData[1].ToString();
itemData.email_body = sqlData[2].ToString();
itemData.email_guid = sqlData[3].ToString();
Emaillist.Add(itemData);
}
如果您尝试将除listData
之外的任何其他类型添加到Emaillist
列表,则上述代码将生成编译错误。