获取错误 - 邮件附件无效

时间:2014-04-25 06:23:36

标签: asp.net email

我正在尝试使用excel文件附件发送电子邮件。但是收到无效的邮件附件错误。

请帮帮我。

这是我的代码 -

enter image description here

2 个答案:

答案 0 :(得分:1)

以下是电子邮件发送的代码 -

 protected void btnSend_Click(object sender, EventArgs e)
{
    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
    msg.From = new MailAddress(txtFrom.Text);
    msg.To.Add(txtTo.Text);
    msg.Subject = txtSubject.Text;
    if (FileUpload1.HasFile)
    {
        String FileName = FileUpload1.PostedFile.FileName;
        MailAttachment mailAttachment = new MailAttachment(FileName, MailEncoding.Base64);
        // msg.Attachments.Add(mailAttachment);
        msg.Attachments.Add(mailAttachment);
    }

using (SmtpClient client = new SmtpClient())
{
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
    client.Host = "smtp.gmail.com";


       client.Port = 587;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Send(msg);
    }
}

答案 1 :(得分:0)

右键单击解决方案资源管理器中的项目名称。单击“添加”,然后选择新文件夹名称为MailDocuments。使用此代码保存并附加它。

        if (FileUpload1.HasFile)
        {
            string fileName = FileUpload1.PostedFile.FileName;
            string Path = Server.MapPath("~/MailDocuments/");
            FileUpload1.PostedFile.SaveAs(Path + fileName);

            MailAttachment ma = new MailAttachment(Server.MapPath("~/MailDocuments/" + fileName), MailEncoding.Base64);
            msg.Attachment.Add(ma);
        }

private bool SendEmail(string myIPAddress)
{
     bool emailsent = true;
     if (myemailaddress != "" && mypassword != "")
     {
         System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
         mail.To.Add(myemailaddress);
         mail.CC.Add("whoever@yahoo.com");
         mail.From = new MailAddress("do.not.reply@whatever.com",  "do.not.reply@whatever.com", System.Text.Encoding.UTF8);
         mail.Subject = "Type Subject Here";
         mail.SubjectEncoding = System.Text.Encoding.UTF8;
         mail.Body = "Your IP address has changed to " + myIPAddress;
         mail.BodyEncoding = System.Text.Encoding.UTF8;
         mail.IsBodyHtml = true;
         mail.Priority = MailPriority.High;
         SmtpClient client = new SmtpClient();
         client.Credentials = new System.Net.NetworkCredential("yourGmailEmail@gmail.com", "thepasswordforthataccount");
         client.Port = 587; // Gmail works on this port
         client.Host = "smtp.gmail.com";
         client.EnableSsl = true; //Gmail works on Server Secured Layer 
         try
         {
             client.Send(mail);
         }
         catch (Exception ex)
         {
             emailsent = false;
         }
     }
   else
   {
      emailsent = false;
   }
   return emailsent;
}
相关问题