2

嗨,我相信我非常接近找出我的代码出了什么问题,但希望有人可以帮助我或指出我正确的方向。我能够运行我的程序,并且在用户将要上传文件的页面上,它让我可以选择文件。但是当我按下提交时,其他信息会发送给我,但文件永远不会出现。我认为这是因为当文件发送到我的电子邮件时,我无法确定将文件临时保存在哪里。这是我目前的代码:

此外,此代码的用途是我网站上的评论/请求页面,用户可以在其中评论并添加屏幕截图。

private string SendMessage(string strTo, string strFrom, string strSubject, string    strMessage, string strAttachment, string strBCC)
{
    try
    {
        MailMessage mailMsg;
        string strEmail = "";
        string strSmtpClient = ConfigurationManager.AppSettings["SmtpClient"];
        string[] arrEmailAddress = strTo.Split(';');
        for (int intCtr = 0; intCtr < arrEmailAddress.Length; intCtr++)
        {
            strEmail = "";
            if (arrEmailAddress[intCtr].ToString().Trim() != "")
            {
                strEmail = arrEmailAddress[intCtr].ToString().Trim();
                mailMsg = new MailMessage(strFrom, strEmail, strSubject, strMessage);
                mailMsg.IsBodyHtml = true;
                if (!strBCC.Trim().Equals(string.Empty))
                    mailMsg.Bcc.Add(strBCC);

                SmtpClient smtpClient = new SmtpClient(strSmtpClient);
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Port = 25;

                smtpClient.Send(mailMsg);
                mailMsg.Dispose();
            }
        }
        return "Message sent to " + strTo + " at " + DateTime.Now.ToString() + ".";
    }
    catch (Exception objEx)
    {
        return objEx.Message.ToString();
    }

    string strUpLoadDateTime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
    string strFileName1 = string.Empty;
    if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
    {
        string strUploadFileName1 = File1.PostedFile.FileName;
        strFileName1 = strUpLoadDateTime + "." + Path.GetFileNameWithoutExtension(strUploadFileName1) + Path.GetExtension(strUploadFileName1);
        strFileName1 = strFileName1.Replace("'", "");
        string strSaveLocation = Server.MapPath("") + "\\" + strFileName1;
        File1.PostedFile.SaveAs(strSaveLocation);
        txtComments.Text = "The file has been uploaded";
    }

我的问题是我在哪里出错了在这段代码中我应该把文件放在我想要保存的位置。

代码的以下部分是我在发送电子邮件时用来格式化电子邮件的部分。并选择将在电子邮件中发送的内容。

protected void Submit_Click1(object sender, EventArgs e)
{
    try
    {
        string dandt = System.DateTime.Now.ToString("yyyyMMddHHmmss");
        string strMessage = "Bug Name: " + txtBugName.Text.Trim() + "<br/>" +
                     "Module Name: " + ddlModule.SelectedValue + "<br/>" +
                     "Page Name: " + ddlPage.SelectedValue + "<br/>" +
                     "Description: " + txtComments.Text.Trim() + "<br/>" +
                      File1.f + "<br/>" +
                      "Email is" + " " + txtemail.Text.Trim() + "<br/>" +
                      "The request was sent at" + dandt;  

        SendMessage(ConfigurationManager.AppSettings["EmailAddrTo"],
            ConfigurationManager.AppSettings["EmailAddrFrom"],
            txtBugName.Text.Trim(),
            strMessage, "", "");
    }
    catch 
    {
    }
}

由于某种原因,当我按下提交时,我的电子邮件中没有发送任何内容。我还试图弄清楚如何在电子邮件中输入电子邮件的发送时间和日期。即使很明显我的电子邮件会包含这些信息,但如果电子邮件由于某种原因被延迟,我希望知道用户按下提交按钮的时间和日期。File.F 在这部分代码中的位置是我试图弄清楚如何将文件附件发送到电子邮件的地方,但我不确定代码中应该使用什么语法。

4

2 回答 2

2

您似乎正在尝试将用户计算机中的一些文件附加到您发送的电子邮件中。如果是这种情况,您需要先上传文件,然后再致电SendMessage.

在您Submit_Click需要做的第一件事是将文件上传到某处的代码。另外,删除File1.fstrMessage怀疑导致您的消息对您无效的地方。

上传文件后,将strSavedLocation保存文件的文件位置传递给您的SendMessage()方法。

在您的SendMessage方法中,您可以在构建MailMessage. strAttachment是您上传文件的路径名:

var attachment = new Attachment(strAttachment);
// Add time stamp information for the file.
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(strAttachment);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(strAttachment);
disposition.ReadDate = System.IO.File.GetLastAccessTime(strAttachment);
mailMsg.Attachments.Add(attachment);
于 2012-07-12T14:48:14.120 回答
0

在我看来,您这里的主要部分减去了方便的 System.Net.Mail.Attachment。

如果我这样做,我会将文件上传处理代码移动到 Submit_Click 处理程序中,然后只需添加 Mail.Attachment 代码。

    private string SendMessage(string strTo, string strFrom, string strSubject, string strMessage, string strAttachment, string strBCC)
    {
        try
        {
            System.Net.Mail.MailMessage mailMsg;
            string strEmail = "";
            string strSmtpClient = ConfigurationManager.AppSettings["SmtpClient"];
            string[] arrEmailAddress = strTo.Split(';');
            for (int intCtr = 0; intCtr < arrEmailAddress.Length; intCtr++)
            {
                strEmail = "";
                if (arrEmailAddress[intCtr].ToString().Trim() != "")
                {
                    strEmail = arrEmailAddress[intCtr].ToString().Trim();
                    mailMsg = new MailMessage(strFrom, strEmail, strSubject, strMessage);
                    mailMsg.IsBodyHtml = true;
                    if (!strBCC.Trim().Equals(string.Empty))
                        mailMsg.Bcc.Add(strBCC);

                    /*** Added mail attachment handling ***/    
                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment(strAttachment);
                    mailMsg.Attachments.Add(attachment);

                    SmtpClient smtpClient = new SmtpClient(strSmtpClient);
                    smtpClient.UseDefaultCredentials = true;
                    smtpClient.Port = 25;

                    smtpClient.Send(mailMsg);
                    mailMsg.Dispose();
                }
            }
            return "Message sent to " + strTo + " at " + DateTime.Now.ToString() + ".";
        }
        catch (Exception objEx)
        {
            return objEx.Message.ToString();
        }
    }

    protected void Submit_Click1(object sender, EventArgs e)
    {
        try
        {
            /*** Moved from SendMessage function ****/
            string strUpLoadDateTime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
            string strFileName1 = string.Empty;
            if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
            {
                string strUploadFileName1 = File1.PostedFile.FileName;
                strFileName1 = strUpLoadDateTime + "." + Path.GetFileNameWithoutExtension(strUploadFileName1) + Path.GetExtension(strUploadFileName1);
                strFileName1 = strFileName1.Replace("'", "");
                string strSaveLocation = Server.MapPath("") + "\\" + strFileName1;
                File1.PostedFile.SaveAs(strSaveLocation);
                txtComments.Text = "The file has been uploaded";
            }

            string dandt = System.DateTime.Now.ToString("yyyyMMddHHmmss");
            string strMessage = "Bug Name: " + txtBugName.Text.Trim() + "<br/>" +
                         "Module Name: " + ddlModule.SelectedValue + "<br/>" +
                         "Page Name: " + ddlPage.SelectedValue + "<br/>" +
                         "Description: " + txtComments.Text.Trim() + "<br/>" +
                          strSaveLocation + "<br/>" +
                          "Email is" + " " + txtemail.Text.Trim() + "<br/>" +
                          "The request was sent at" + dandt;


            SendMessage(ConfigurationManager.AppSettings["EmailAddrTo"],
                ConfigurationManager.AppSettings["EmailAddrFrom"],
                txtBugName.Text.Trim(),
                strMessage, strSaveLocation, "");
        }
        catch
        {
        }
    }

关于使用 StringBuilder 的说明,我同意,我会这样使用它:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("Bug Name: {0}<br/>", txtBugName.Text.Trim());
sb.AppendFormat("Module Name: {0}<br/>", ddlModule.SelectedValue);       

编辑添加:另外,请参阅上面关于使用 ContentDisposition 的 Brad 的回答。

于 2012-07-12T15:05:28.637 回答