Skip to main content
 首页 » 编程设计

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

2024年01月02日31qlqwjy

现在我修复了文件扩展名的过滤,但是我在“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 = ""; 
 
} 

请您参考如下方法:

这里有一个更好的方法:

  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); 
                    } 
                } 
            }