我正在尝试使用SMTP客户端发送电子邮件(例如gmail smtp)。我知道如何发送一封电子邮件,但我想要一些自动化。
我想要的:我想发送25封电子邮件,一个smtp,然后更改smtp(用户名和密码。) 例如,使用username1密码的前25封电子邮件 其他25封电邮与suername2密码2
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);
答案 0 :(得分:0)
我不熟悉,你只能用谷歌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();
}
我还没有编译代码,但这个想法可能就是你在寻找什么?