现在我修复了文件扩展名的过滤但我在“catch”上遇到了问题,它没有显示任何错误信息但很好,它没有发送错误文件类型的电子邮件。
但我的问题在这里: catch(例外错误) {
Console.WriteLine(err.Message);
lblStatus.ForeColor = Color.Red;
lblFile.ForeColor = Color.Red;
lblStatus.Text = "There was an error occured while submitting your application";
lblFile.Text = " Accepts .doc, .docx and .pdf files only!";
return ;
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (fileUpload1.HasFile)
{
var fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
try
{
string strExtension = Path.GetExtension(fileName);
if (strExtension == ".docx" || strExtension == ".doc" || strExtension == ".pdf")
{
MailMessage myMessage = new MailMessage();
myMessage.To.Add(new MailAddress("someone@yahoo.com"));
myMessage.From = new MailAddress("me@gmail.com");
myMessage.Subject = txtSubject.Text;
myMessage.Body = "<html><body><br/><b>Sender Name:</b> " + txtName.Text.ToString() + "<br/><br/><b>Email:</b> " + txtEmail.Text.ToString() +
"<br/><br/><b>Contact Number:</b> " + txtContact.Text.ToString() + "<br/><br/><b>Subject</b> " + txtSubject.Text.ToString() +
"<br/><br/><b>CV Summary:</b><br/><br/>" + txtSummary.Text.ToString() + "</body></html>";
myMessage.IsBodyHtml = true;
myMessage.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));
SmtpClient mySmtp = new SmtpClient();
mySmtp.Host = "smtp.gmail.com";
mySmtp.Credentials = new System.Net.NetworkCredential("me@gmail.com", "mypassword");
mySmtp.EnableSsl = true;
mySmtp.Send(myMessage);
lblStatus.ForeColor = Color.Green;
lblStatus.Text = "We will contact you once you have been shortlisted to the position you are applying for. <br/> You may now close this window.";
txtName.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
txtSummary.Text = "";
txtContact.Text = "";
}
}
catch(Exception err)
{
Console.WriteLine(err.Message);
lblStatus.ForeColor = Color.Red;
lblFile.ForeColor = Color.Red;
lblStatus.Text = "There was an error occured while submitting your application";
lblFile.Text = " Accepts .doc, .docx and .pdf files only!";
return ;
}
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
txtSummary.Text = "";
txtContact.Text = "";
}
答案 0 :(得分:0)
这是一种更好的方法:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (fileUpload1.HasFile) ;
{
var fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
try
{
string strExtension = Path.GetExtension(fileName);
if (strExtension != ".docx" || strExtension != ".doc" || strExtension != ".pdf")
{
lblFile.ForeColor = Color.Red;
lblFile.Text = "You can attach .doc,.docx and pdf files only";
lblStatus.ForeColor = Color.Red;
lblStatus.Text = "There was an error occured while sending your email. Please try again later.";
return;
}
MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress(txtEmail.Text);
myMessage.To.Add(new MailAddress("EMAIL@yahoo.com"));
myMessage.Subject = txtSubject.Text;
myMessage.Body = "<html><body><br/><b>Sender Name:</b> " + txtName.Text.ToString() + "<br/><br/><b>Email:</b> " + txtEmail.Text.ToString() +
"<br/><br/><b>Contact Number:</b> " + txtContact.Text.ToString() + "<br/><br/><b>Subject</b> " + txtSubject.Text.ToString() +
"<br/><br/><b>CV Summary:</b><br/><br/>" + txtSummary.Text.ToString() + "</body></html>";
myMessage.IsBodyHtml = true;
myMessage.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileName));
SmtpClient mySmtp = new SmtpClient();
mySmtp.Host = "smtp.gmail.com";
mySmtp.Credentials = new System.Net.NetworkCredential("EMAIL@gmail.com", "PASSWORD");
mySmtp.EnableSsl = true;
mySmtp.Send(myMessage);
lblStatus.ForeColor = Color.Green;
lblStatus.Text = "Email successfully sent! We will contact you as soon as you have been shortlisted for the position you are applying for.<br/> Thank You. You can close this window now. ";
txtName.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
txtSummary.Text = "";
txtContact.Text = "";
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
基本上,您只想捕获异常,例如IO或邮件发送失败。
如果扩展名不是doc,docx或pdf而不是调用发送邮件代码,则此代码将返回。
此外,myMessage.From()地址必须是有效的电子邮件地址,并且是您通过NetworkCredentials提供的地址()
以下是我测试并验证其正常工作的代码的缩减版本:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile) ;
{
var fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
try
{
string strExtension = Path.GetExtension(fileName);
if (strExtension != ".docx" || strExtension != ".doc" || strExtension != ".pdf")
{
lblFile.ForeColor = Color.Red;
lblFile.Text = "You can attach .doc,.docx and pdf files only";
lblStatus.ForeColor = Color.Red;
lblStatus.Text = "There was an error occured while sending your email. Please try again later.";
return;
}
MailMessage myMessage = new MailMessage();
myMessage.To.Add(new MailAddress("OtherPersonsEmail@email.xyz"));
myMessage.From = new MailAddress("YourEmailAddress@gmail.com");
myMessage.Subject = "Test";
myMessage.Body = "asd asd as d";
myMessage.IsBodyHtml = true;
myMessage.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, fileName));
SmtpClient mySmtp = new SmtpClient();
mySmtp.Host = "smtp.gmail.com";
mySmtp.Credentials = new System.Net.NetworkCredential("YourEmailAddress@gmail.com", "YourPassword");
mySmtp.EnableSsl = true;
mySmtp.Send(myMessage);
}
catch(Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
Console.WriteLine(ex.Message);
}
}
}