发送带有附件的电子邮件,该附件接受ASP.NET C#中的特定文件类型

本文关键字:NET 类型 文件 ASP 电子邮件 | 更新日期: 2023-09-27 18:24:33

现在我修复了文件扩展名的过滤,但我在"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>&nbsp;" + txtName.Text.ToString() + "<br/><br/><b>Email:</b>&nbsp;" + txtEmail.Text.ToString() +
                                     "<br/><br/><b>Contact Number:</b>&nbsp;" + txtContact.Text.ToString() + "<br/><br/><b>Subject</b>&nbsp;" + 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 = "";
}

发送带有附件的电子邮件,该附件接受ASP.NET C#中的特定文件类型

这里有一个更好的方法:

  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>&nbsp;" + txtName.Text.ToString() + "<br/><br/><b>Email:</b>&nbsp;" + txtEmail.Text.ToString() +
                                    "<br/><br/><b>Contact Number:</b>&nbsp;" + txtContact.Text.ToString() + "<br/><br/><b>Subject</b>&nbsp;" + 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);
                    }
                }
            }