向多个地址发送雅虎电子邮件
本文关键字:雅虎 电子邮件 地址 | 更新日期: 2023-09-27 18:04:52
我正在使用这段代码从雅虎发送电子邮件。
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "mitshel@yahoo.com";
string password = "xxxxxx!";
string emailTo = "dimitris.chris@yahoo.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
但是如果我想添加更多的电子邮件地址呢?我尝试了这个,但是我得到一个错误:
string emailTo = "mitsoshellas@yahoo.com" ,"dimitris.christoforidis@hotmail.com" ;
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "mitshel@yahoo.com";
string password = "xxxxxx!";
List<string> emailToList = new List<string>;
emailToList.Add("dimitris.chris@yahoo.com");
//add as many other as you like
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
foreach(string recipient in emailToList){
mail.To.Add(recipient);
}
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
或
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "mitshel@yahoo.com";
string password = "xxxxxx!";
List<string> emailToList = new List<string>;
emailToList.Add("dimitris.chris@yahoo.com");
//add as many other as you like
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
foreach(string recipient in emailToList){
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(recipient);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
我假设您有一个包含多个电子邮件地址的字符串,由逗号和空格分隔,例如
string emailTo = "someEmail@email.com, someEmail2@email.com, someEmail3@email.com";
您需要将emailTo
分隔为字符串集合。要做到这一点,您可以使用以下命令:
// Separate the emailTo string into a list of email addresses
List<string> emailAddresses = new List<string>();
int startingIndex = 0;
while (startingIndex < emailTo.Length)
{
int commaIndex = emailTo.IndexOf(",", i);
if (commaIndex != -1)
{
//Extract the email address
string emailAddress = emailTo.Substring(startingIndex, commaIndex - startingIndex);
// Remove the space following the comma
if (emailAddress.Substring(0, 1) == " ")
{
emailAddress = emailAddress.Substring(1, emailAddress.Length - 1);
}
i = startingIndex + 1;
result.Add(emaiAddress);
}
}
// Add each email address to the message's recipients
foreach(string emailAddress in emailAddresses)
{
mail.To.Add(emailAddress);
}
这种方法的好处是,你不需要手动解析电子邮件地址,并在你有一个emailTo
字符串,其中包含许多电子邮件地址添加到收件人列表自己。