邮件附件文件中的问题 [英] problem in mail attachment file

查看:54
本文介绍了邮件附件文件中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,我必须通过我的网站发送电子邮件,附上多个文件,但是当我附加任何文件时,控件跳进catch块。另外它运行良好。

代码如下请看看。

hello friends, i have to send email via my website with multiple file attached, but when i'm attaching any file the control jump into catch block.otherwise it's working well.
code is below please have a look.

protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            string str = "";
            HttpFileCollection fileCollection = Request.Files;
            for (int i = 0; i < fileCollection.Count; i++)
            {
                HttpPostedFile uploadfile = fileCollection[i];

                string fileName = Path.GetFileName(uploadfile.FileName);
               // fileName = GetUniqueFileName(fileName);
                if (uploadfile.ContentLength > 0)
                {
                    uploadfile.SaveAs(Server.MapPath("~/Upload/MailAttachments/") + fileName);
                    str += fileName + ",";
                }
            }

            string BodyText = "";
            BodyText = "<b>Dear User,</b><br><br>";
            BodyText = BodyText + "Following are the details of the patient who has requested for a appointment you through your website 'ahms.co.in'<br /><br />";
            BodyText = BodyText + "Patient Name:  " + txtPatient.Text + "<br>";
            BodyText = BodyText + "Email :  " + txtEmail.Text + "<br>";
            BodyText = BodyText + "Mobile Number:  " + txtMobile.Text + "<br>";
            BodyText = BodyText + "Health issues:  " + txthealthissue.Text + "<br> <br />";
            BodyText = BodyText + "Appointment Date:  "+txtDate.Text+"<br/>";
            BodyText = BodyText + "Time Slot choosed: " + ddtimeslot.Text + "<br/>";
            BodyText = BodyText + "Thanks,<br />Web Admin";

           

            if (obj.SendMail("info@ahms.co.in", "mail@ahms.co.in ", "Request for Appointment from : ahms.co.in", BodyText, str) == "mailsend")
                {
                    lblMsg.Text = "Thankyou!!We'll get back to you soon";
                    Reset();
                }
                else
                {
                    lblMsg.Text = "Mail server encounter an error.Try again.";

                }
     }

        catch (Exception ex)
        {
            Response.Write(ex);
        }



    }



这里是邮件发送功能




here is mail sending function

public String SendMail(String MailTo, String MailFrom, String Subject, String MailBody, string pAttachmentPath)
    {
        try
        {
            MailMessage msg = new MailMessage();
            MailAddress mailaddressfrom = new MailAddress(MailFrom);
            MailAddress mailaddressTo = new MailAddress(MailTo);
            msg.From = mailaddressfrom;
            msg.To.Add(mailaddressTo);
            msg.Subject = Subject;
            msg.Body = MailBody;
            msg.IsBodyHtml = true;

            if (pAttachmentPath.Trim() != "")
            {
                System.Net.Mail.Attachment MyAttachment = new System.Net.Mail.Attachment(pAttachmentPath);
                msg.Attachments.Add(MyAttachment);
                msg.Priority = System.Net.Mail.MailPriority.High;
            }
            SmtpClient objSmtp = new SmtpClient();
            
            objSmtp.Send(msg);
            return "mailsend";
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

推荐答案

str += fileName + ",";





尝试删除 +,,看看会发生什么。这只是猜测,我们需要确切的异常消息,以及抛出它的行。



Try to remove + "," and see what happens. It's just a guess, we would need the exact exception message, and the line from where it has been thrown.






在这里使用...



Hi,

As you use here...

uploadfile.SaveAs(Server.MapPath("~/Upload/MailAttachments/") + fileName);
                    str += fileName + ",";





修改:





Modified :

string path = Server.MapPath("~/Upload/MailAttachments/") + fileName;
uploadfile.SaveAs(Server.MapPath(path);
str = "Upload/MailAttachments/" + fileName;





//这里你要添加多个文件,所以要注意最后一个逗号...(I使用它作为单个文件。首先尝试这个。)

//还有一个建议你使用文件名作为上传的文件名,但我想你可以随时生成一个随机字符串...





或其他方法也可以尝试我在我的博客 [ ^ ]。



希望它能为您提供帮助。



谢谢



// here you want to add multiple files so beware about last comma...(I use it as single file. first try this.)
// one more suggestion you use filename as uploaded file name, But I think you can generate a random string for this for all time...


or a alternate method also you can try I describe in my blog[^].

Hope it will help you.

Thanks


这篇关于邮件附件文件中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆