Azhar Shahid
Azhar Shahid

Reputation: 151

Getting Error- Invalid mail Attachment

I am trying to send email with excel file attachment. But getting invalid mail attachment error.

Please help me.

Here is my code-

enter image description here

Upvotes: 1

Views: 1821

Answers (2)

user3189944
user3189944

Reputation:

Here is code for Email sending-

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

Upvotes: 1

Raghubar
Raghubar

Reputation: 2788

Right click on project name in solution explorer . Click on Add then Choose new folder name it MailDocuments. Use this code to Save it and Attach It.

        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("[email protected]");
         mail.From = new MailAddress("[email protected]",  "[email protected]", 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("[email protected]", "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;
}

Upvotes: 0

Related Questions