我使用此代码从雅虎发送电子邮件。
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" ;
答案 0 :(得分:1)
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);
}
}
答案 1 :(得分:1)
我假设你有一个包含多个电子邮件地址的字符串,用逗号分隔,然后用空格分隔,例如
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
字符串时自行将其添加到收件人列表中包含许多电子邮件地址。