I am wondering what the best way to send bu开发者_JAVA百科lk emails is using System.Net.Mail and C#.
Is it a good idea to send emails in batches?
Should I use the to field or BCC?
I prefer using the To field and send e-mails one at a time in stead of using the BCC field. This way the receiver sees his e-mail address in the TO field (less spam-sensitive) and you can personalize the e-mails per user in the future.
For the sending, you should use batches to prevent timeouts and heavy server loads. You could use a queue for all the e-mails and send them using a configurable schedule using a service, scheduled task, or whatever.
If you do send a single email to multiple recipient and if it's for sending emails to people who don't know each other you should definitely use the BCC field otherwise you're sure to make a lot of people quite angry when you're giving away their email addresses to strangers (and you might also be breaking some kind of data protection law depending on where you live).
bulk Emails using mailkit you have to import it by nuget manager set in gmail https://myaccount.google.com/lesssecureapps?pli=1 to be on
public void ReadFileAndSend()
{
using (StreamReader reader = new StreamReader(@"d:\Email.txt"))
{
while (!(reader.ReadLine() == null))
{
String line = reader.ReadLine();
if (line != "")
{
try
{
Send("", line.Trim());
Thread.Sleep(500);
}
catch
{
}
}
}
Console.ReadLine();
}
}
public void Send(String FromAddress,String ToAddress)
{
try
{
string FromAdressTitle = "";
string ToAdressTitle = "";
string Subject = "";
string BodyContent = "";
string SmtpServer = "smtp.gmail.com";
int SmtpPortNumber = 587;
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
mimeMessage.Subject = Subject;
mimeMessage.Body = new TextPart("html")
{
Text = BodyContent
};
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect(SmtpServer, SmtpPortNumber, false);
client.Authenticate("your email", "pass");
client.Send(mimeMessage);
Console.WriteLine("The mail has been sent successfully !!");
client.Disconnect(true);
}
}
catch (Exception ex)
{
throw ex;
}
}
精彩评论