ASP.NETとC#を使用してフォルダーからファイルを取得するコードがあり、CheckBoxListを使用してファイルを表示します。これは、ユーザーがファイルの1つを選択できるようになり、最後にボタンを使用してユーザーが次のことができるようになるためです。選択したアイテムをメールで送信します。
私が抱えている問題は、アイテムが選択されて電子メールが送信された後、ページがリロードされ、アイテムが複製され、修正する理由や方法がわからないことです。どんな助けでもそれを高く評価するでしょう。
コードは次のとおりです。
if (File.Exists(wavFile))
{
ListItemCollection itemCollection = CheckBoxList2.Items;
itemCollection.Add(new ListItem(wavFile));
itemCollection.Add(new ListItem("<asp:Panel ID=\"Panel1\" runat=\"server\"><object id=\"MediaPlayer\" width=\"100\" height=\"42\" classid=\"CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95\" standby=\"Loading Windows Media Player components...\"" +
"type=\"application/x-oleobject\"><param name=\"FileName\" value=\"" + wavFile + "\"><param name='AUTOPLAY' value='0'>" +
"<embed type=\"application/x-mplayer2\" src=\"" + wavFile + "\" pluginspage=\"http://www.microsoft.com/Windows/MediaPlayer/\" name=\"MediaPlayer\" uimode=\"none\" width=\"300\" height=\"42\">" +
"</embed></object></asp:Panel><br/><br/>"));
}
そして、電子メールは次のコードのボタンで制御されます。
protected void btnSend_Click1(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("email@email.com");
mail.From = new MailAddress("email@email.com");
mail.Subject = claimNumber.Text;
mail.Body = "This is a test of the email again.";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(CheckBoxList2.SelectedValue);
mail.Attachments.Add(attachment);
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.email.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("email@email.com", "pa$$w0rd");
smtp.EnableSsl = true;
smtp.Send(mail);
this.labelSuccessIndex.Text = "<br/><strong>The file has been emailed.</strong>";
}
catch (Exception ex)
{
labelError.Text = ex.Message;
}
}