簡體   English   中英

在列表框中逐行讀取文件

[英]Reading file line by line in listbox

我嘗試制作自己的 email 發件人,但現在我已發出逐行執行郵件列表。 我目前的代碼是

foreach (string emails in listBoxEmails.Items)
{
    mail.From.Add(new MailboxAddress(textBoxSendName.Text, textBoxSendFrom.Text));
    mail.To.Add(new MailboxAddress(emails));

    client.Send(mail);

    Thread.Sleep(1000);
}

我希望程序運行起來,在我發送列表的第一個郵件成功后,它會發送第二個郵件。 但是從我從谷歌獲得的代碼中,它將直接將所有 email 一起發送。

您應該編寫一個類似返回 true 或 false 的方法。

    foreach (string email in listBoxEmails.Items)
    {
        if (!sendMail(textBoxSendName.Text, textBoxSendFrom.Text, email)){
            break;
        }
    }

    private bool sendMail(string name, string from, string to)
    {
        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.????.???");

            mail.From = new MailAddress(from, name, Encoding.UTF8);
            mail.To.Add(to);
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing...";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            return true;
        }
        catch (Exception ex)
        {   
            MessageBox.Show(ex.ToString());
            return false;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM