简体   繁体   中英

how to send email with different smtp?

I am trying to sent emails with SMTP client (for example gmail smtp). I know how to send one email, but I want some automation.

what I want: I want to send 25 email with one smtp and then change smtp (username and password.) for example first 25 email with username1 password other 25 email with suername2 password2

    SmtpClient client = new SmtpClient();
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    client.Timeout = 10000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("user@gmail.com","password");

    MailMessage mm = new MailMessage("donotreply@domain.com", "sendtomyemail@domain.co.uk", "test", "test");
    mm.BodyEncoding = UTF8Encoding.UTF8;
    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

    client.Send(mm);

I'm not familiar with, that you only can send one email at a time with googles smtp, but if your looking for something that can switch between smtp's, something like this may do it:

public void MyMailFunction()
    {
        while (true)
        {
            bool SwitchSMTP = false;

            using (var db = whatEverContext())
            {
                var q = from s in db.mail select s;
                var myList = q.ToList.Take(25);
                if (myList.Count() == 0)
                {
                    break; 
                }

                if (!SwitchSMTP)
                    {
                        SendMails(myList, 25, "smtp.gmail.com", "myusername", "mypassword");
                        SwitchSMTP = true;

                    }
                    else
                    {
                        SendMails(myList, 25, "smtp.gmail.com", "myusername", "mypassword");
                        SwitchSMTP = false;
                    }


            }

        }
    }

    internal void SendMails(IEnumerable<Mail> myList, int port, string host, string username, string password)
    {
        SmtpClient client = new SmtpClient();
        client.Port = port;
        client.Host = host;
        client.EnableSsl = true;
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(username, password);

        foreach (var m in myList)
        {

            MailMessage mm = new MailMessage("donotreply@domain.com", "sendtomyemail@domain.co.uk", "test", "test");
            mm.BodyEncoding = UTF8Encoding.UTF8;
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            client.Send(mm);
        }

        client.Dispose();
    }

I haven't compiled the code, but the idea is maybe what you are looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM