0

以下のコードで達成しようとしているのは、データベースにある各電子メール アドレスに電子メールを送信することです。私の問題は、送信ボタンをクリックするとThe specified string is not in the form required for an e-mail address.、行に " "というエラーが表示されることmail.Bcc.Add(MyVar.Text)です。

private void sendmail()
    {
        Label MyVar = new Label();
        foreach (DataRowView UserEmail in SelectUserProfile.Select(DataSourceSelectArguments.Empty))
        {
            MyVar.Text = "";
            MyVar.Text += UserEmail["EMAIL"].ToString() + "; ";
        }

        //This line takes the last ; off of the end of the string of email addresses
        MyVar.Text += MyVar.Text.Substring(0, (MyVar.Text.Length - 2));

        MailMessage mail = new MailMessage();

        mail.Bcc.Add(MyVar.Text);
        mail.From = new MailAddress("syntaxbugerror@gmail.com");
        mail.Subject = "New Member Application";
        mail.Body = "Good day, in this e-mail you can find a word document attached in which it contains new membership application details.";
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential("myusername@gmail.com", "mypassword");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }

アーニー

4

2 に答える 2

1

BCC メールアドレスの文字列を作成するのはなぜですか?

Bccコレクションなので、そのように扱ってください。あなたがレーベルで何をしているのか、またはその理由がよくわからないので、今のところそれを無視して、このようなことがうまくいくはずです

MailMessage mail = new MailMessage();

foreach (DataRowView UserEmail in SelectUserProfile.Select(DataSourceSelectArguments.Empty))
{
   MyVar.Text = "";
   MyVar.Text += UserEmail["EMAIL"].ToString() + "; ";

   try
   {
       mail.Bcc.Add(UserEmail["EMAIL"].ToString());
   }
   catch(FormatException fe)
   {
      // Do something with the invalid email address error.
    }
}
于 2012-03-19T15:15:00.923 に答える
0

ロジックフローは意味がありません。あなたは電子メールを一緒に解析していて、それからいくつかの欠陥のあるロジックを通してあなたの電子メールアドレスを解析しようとしています。代わりに、メールメッセージを作成してから、メールアドレスループして、それぞれをBCCに追加します。

// Create Message (...)
foreach(...)
{
   mail.Bcc.Add(UserEmail["EMAIL"].ToString()); 
}
// Finalize and send (...)
于 2012-03-19T15:12:43.763 に答える