locked
How to send an HTML mail attachment File RRS feed

  • Question

  • User-1193791088 posted
    protected void SendMessage() 
    { MailMessage mailMessage = new MailMessage { From = new MailAddress("r.hall@myhandymansolution.com", "My Handyman Solution") };
    mailMessage.To.Add(new MailAddress("r.hall@mycleaningsolution.com"));
    //mailMessage.CC.Add(new MailAddress(""));
    mailMessage.Subject = "Invoice #" + _InvoiceNumber + " ";


    mailMessage.Attachments.Add(new Attachment(Server.MapPath("Admin/Customer/Sales/Invoices/Forms/INVOICE" + _InvoiceNumber + "_" + _AccountName + "_SINGLE.pdf")));
    mailMessage.IsBodyHtml = true; mailMessage.Body = " " + "" + " " + " " + " " + " "; mailMessage.Priority = MailPriority.Normal;
    SmtpClient smtpClient = new SmtpClient();
    object UserState = mailMessage;
    smtpClient.SendAsync(mailMessage, UserState); }

    First: I get the email twice - Why?

    second: No attachment

    I am running it off a web development server. 

    Thursday, October 25, 2018 4:59 AM

All replies

  • User753101303 posted

    Hi,

    You are 100% sure SendMessage is called one time only ? What if adding a Guid in the message body to see if you get the same message twice or two messages one time each ?

    Not sure about the attachment. Could ie be currently ignored by your mail client rather than not being sent. If I remember I always add a mime type (here adding System.Net.Mime.MediaTypeNames.Application.Pdf for the 2nd constructor parameter).

    Thursday, October 25, 2018 11:12 AM
  • User-1193791088 posted

    I got it to work before sending an attachment, but that was so long ago  I can't remember how.

    It only sends twice when there is an error with the code, if I delete the attachment code it only sends once.

    if the file is in the wrong folder - I wish they would send an exception that would say file not found or file not found in C:\folder\.... But you have to guess at why it's not working

    Thursday, October 25, 2018 2:33 PM
  • User-893317190 posted

    Hi RobertH3,

    I have tried similar code and it works .Maybe you could change your smtp host  and try again.

    Below is my code.

     //Initialize a MailAddress instance with the specified email address
                MailAddress maddr = new MailAddress("ackerlyx@gmail.com");
               
                MailMessage myMail = new MailMessage();
                
                        myMail.To.Add("ackerlyx@gmail.com");
                
                myMail.From = maddr;
    
              
                myMail.Subject = "test";
    
             
                myMail.SubjectEncoding = Encoding.UTF8;
    
          
                myMail.Body = "<div style='color:red'>hello this is a test</div>";
    
           
                myMail.BodyEncoding = Encoding.UTF8;
    
                myMail.Priority = MailPriority.High;
    
                myMail.IsBodyHtml = true; // use html format
    
                //Add attachments if it exists
                if (File.Exists(Server.MapPath("/WebForm1.aspx")))
                {
                    Attachment attachFile = null;
    
                    attachFile = new Attachment(Server.MapPath("/WebForm1.aspx"));
                    myMail.Attachments.Add(attachFile);
                }
                        
                      
                SmtpClient smtp = new SmtpClient();
                smtp.EnableSsl = true;   // the port I use need ssl, if you don't use ssl, you could ignore this line
               
                smtp.Credentials = new System.Net.NetworkCredential("ackerlyx@gmail.com", "your password");
                
                smtp.Host = "smtp.gmail.com";
    
                smtp.Port = 587;
         
                smtp.Send(myMail);
                   
    

    The result.

    Best regards,

    Ackerly Xu

    Friday, October 26, 2018 5:22 AM
  • User753101303 posted

    I would be surprising to really not have an exception for non existing attached files. The weird behavior you see seems related to how you handle the exception.

    What if you drop your exception handling code to see what happens or show this code.

    Edit: done a quick test and i do have a FileNotFoundException if the file doesn't exists. Double check your exception handling code.

    Friday, October 26, 2018 6:52 AM