简体   繁体   English

如何使用不同的SMTP发送电子邮件?

[英]how to send email with different smtp?

I am trying to sent emails with SMTP client (for example gmail smtp). 我正在尝试使用SMTP客户端发送电子邮件(例如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 我想要的是:我想发送25条带有一个smtp的电子邮件,然后更改smtp(用户名和密码。)例如,前25个使用username1密码的电子邮件,其他25条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: 我不熟悉,您一次只能通过Google smtp发送一封电子邮件,但是如果您正在寻找可以在smtp之间切换的邮件,则可以这样做:

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? 我还没有编译代码,但是这个主意也许就是您想要的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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