【问题标题】:Send Yahoo email to more than one address [duplicate]将雅虎电子邮件发送到多个地址[重复]
【发布时间】:2017-01-08 05:35:18
【问题描述】:

我正在使用此代码从雅虎发送电子邮件。

  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 emailTo = "mitsoshellas@yahoo.com, dimitris.christoforidis@hotmail.com";(所有的电子邮件地址都包含在一个字符串中,在“”内 - 即“”)

标签: c#


【解决方案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);
         }
}

【讨论】:

    【解决方案2】:

    我假设您有一个包含多个电子邮件地址的字符串,由逗号和空格分隔,例如

    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字符串>许多电子邮件地址

    【讨论】:

    • 为什么用这种方式发送邮件,把它作为垃圾邮件发送到hotmail?
    • 这可能与介绍的方法无关。这可能是因为您在测试期间多次发送了相同的消息。这通常会被视为垃圾邮件。我建议将发件人列入白名单。
    • 如果您想确认是这种情况,请尝试使用其他方法,看看它是否进入垃圾邮件。如果是这样,那是因为您多次发送完全相同的消息。如果没有,那我的方法确实有问题。
    • 感谢您的帮助,我会检查它。如果我通过这种方式发送大量电子邮件,雅虎也可以阻止我的电子邮件吗?
    猜你喜欢
    • 2013-09-19
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2011-06-08
    • 1970-01-01
    • 2017-10-17
    相关资源
    最近更新 更多