可以使用for循环发送批量邮件,批量邮件问题,AWS(亚马逊)SES

时间:2015-07-15 02:20:09

标签: c# asp.net email amazon-web-services amazon-s3

我正在使用C#和.net进行编码,使用AWS(AMazon)SES(简单电子邮件服务)API一次发送大约5000封邮件的批量邮件,如果发送的邮件数量少于500,一切正常-600(大约),但如果它更像5000,它将发送高达500-600然后它将停止发送电子邮件。我使用datatable存储数据库中的邮件列表,分配模板主体和主题,然后用于循环逐个发送电子邮件。我需要知道它是编码问题还是循环问题或其他一些东西?任何建议对我都有帮助吗?

for (i = 0; i < dtable1.Rows.Count; i++)
    {

        Object a=dtable1.Rows[i]["student_emailid"];
        String TO1=Convert.ToString(a);
        TO1 = TO1.Trim();
        if (TO1.Equals("")) {
            continue;
        }
        const String FROM = "asit@amcsquare.com";   // Replace with your "From" address. This address must be verified.
        String TO = TO1;  // Replace with a "To" address. If your account is still in the
        // sandbox, this address must be verified.

        String SUBJECT = email_subject;
        String BODY = templateBody;


        // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
        const String SMTP_USERNAME = "XXXXXXXXXXXX";  // Replace with your SMTP username. 
        const String SMTP_PASSWORD = "XXXXXXXXXXXXXXXXXX";  // Replace with your SMTP password.

        // Amazon SES SMTP host name. This example uses the us-west-2 region.
        const String HOST = "email-smtp.us-east-1.amazonaws.com";

        // Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
       // STARTTLS to encrypt the connection.
        const int PORT = 587;


        // Create an SMTP client with the specified host name and port.
        using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
        {
            // Create a network credential with your SMTP user name and password.
            client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);

                //Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then 
               //the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
            client.EnableSsl = true;
            System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);

            message1.IsBodyHtml = true;
            client.Send(message1);

        }




    }

1 个答案:

答案 0 :(得分:0)

我不知道它现在正在工作的确切原因,但它正在发挥作用。我改变了上面代码的逻辑,它开始工作了。为了每次都发送每封邮件,这次我只取了一次smtp连接并用它一次发送所有批量邮件而不是每次都提取SMTP连接而是开始工作。但问题是发送时间,它是花太多钱发送所有的邮件。无论如何我也会找到解决方案。

using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
    client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
    client.EnableSsl = true;
    for(i=0;i<mail.Count;i++)

            {
                String TO = mail[i];
                System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);

                message1.IsBodyHtml = true;

                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(message1);

            }

    client.Dispose();

}

Label1.Text = mail.Count.ToString() + " mails sent !!";

}