使用asp.net邮件附件

时间:2015-05-20 09:38:27

标签: asp.net mailing

我正在尝试使用asp.net邮件附件。为此,我完成了以下代码:

Default.aspx.cs:

function otherText() {
        document.getElementById('amount').value = document.getElementById("other_amount").value;
}

Default.aspx的:

using (MailMessage mailMessage = new MailMessage())
        {
            mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
            mailMessage.Subject = "Test Subject";
            mailMessage.Body = myString.ToString();
            if (fuAttachment.HasFile)
            {
                string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
                mailMessage.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
            }
            mailMessage.IsBodyHtml = true;
            mailMessage.To.Add(new MailAddress(txtem.Text.ToString()));
            mailMessage.CC.Add(new MailAddress("sample@test.com"));
            mailMessage.Bcc.Add(new MailAddress("sample1@test.com"));

            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["Host"];
            smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
            NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            smtp.Send(mailMessage);
        }

现在使用此代码我可以正确发送电子邮件,但没有附件!是因为我必须在服务器中单独上传文件。在这一点上我真的无能为力,任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

将表单enctype设置为multipart / form-data。否则,该文件不会发布到服务器,也不会发送您的附件。

<form runat="server" enctype="multipart/form-data">