Email won't work with attachments

Discussion in 'Email' started by accruesolut, Feb 8, 2010.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I have a mail message web service that works without attachments but won't work with attachments.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Net.Mail;
    using System.Web.Hosting;

    namespace AMS_SL3_new.Web.Services
    {
    [WebService(Namespace = "http://www.accrue-solutions.net/Services/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class MailService : System.Web.Services.WebService
    {
    [WebMethod]
    public bool SendMail(string fromAddress, string toAddress, string subject, string body)
    {
    try
    {
    SmtpClient smtp = new SmtpClient("localhost");
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress(fromAddress);
    msg.To.Add(new MailAddress(toAddress));
    msg.Subject = subject;
    msg.Body = body;
    msg.IsBodyHtml = false;

    // add attachments
    string folder = HostingEnvironment.MapPath(Config.StorageFolder);
    System.IO.DirectoryInfo xMainDir = new System.IO.DirectoryInfo(folder);
    if (xMainDir.Exists)
    {
    //*****if this section gets processed, the email fails ******
    if (xMainDir.GetFiles().Length > 0)
    {
    System.IO.FileInfo[] filein_array = xMainDir.GetFiles();
    foreach (System.IO.FileInfo fi in filein_array)
    {
    msg.Attachments.Add(new Attachment(fi.Name));
    }
    }
    }

    smtp.Send(msg);

    // empty the folder of all attached files

    return true;
    }
    catch (Exception ex)
    {
    ///Log.Write(ex)
    return false;
    }
    }
    }
    }
     
  2. Additional Information

    I just realized I should probably include my send button method in case the problem might lie there...

    void sendButton_Click(object sender, RoutedEventArgs e)
    {
    checkFields();
    BasicHttpBinding bind = new BasicHttpBinding();
    EndpointAddress endpoint = new EndpointAddress("http://www.accrue-solutions.net/Services/MailService.asmx");
    ServiceProxy.MailServiceSoapClient mailSrv = new AMS_SL3_new.ServiceProxy.MailServiceSoapClient(bind,endpoint);
    mailSrv.SendMailAsync(txtFromEmail.Text, txtToEmail.Text, txtFirst.Text, txtLast.Text);
    mailSrv.SendMailCompleted += new EventHandler<AMS_SL3_new.ServiceProxy.SendMailCompletedEventArgs>(mailSrv_SendMailCompleted);

    }
     
  3. Bruce

    Bruce DiscountASP.NET Staff

    any error?
     
  4. In Visual Studio it says the response is null and unfortunately, on the host it just says email not sent. I tried turning on i.e. debugging but that doesn't help and Fiddler doesn't show me anything. (if you know of another tool I can try that might show me what's going on that would be great)

    The documents folder sits at the root of my application and is accessed through the web.config.

    If I make sure that folder is empty, the email sending works perfectly. If I upload a file to the folder, the file uploads perfectly and I can see it in the folder.

    If the folder has a file in it (no matter what the size - the last file I tried was only 29k), the email will not send.
     
  5. I found the answer

    Hi there,

    If anyone else has the same issue as I did - they may have the same problem as I did. After I finished doing my local testing, I changed all references to localhost to my host address but I neglected to update the service references so they were still pointing to the old addresses. Once I did that and uploaded again - everything is working as expected.

    I also had an issue with the address of the attachment and changed it like so:

    // add attachments
    string folder = HostingEnvironment.MapPath(Config.StorageFolder);

    System.IO.DirectoryInfo xMainDir = new System.IO.DirectoryInfo(folder);
    if (xMainDir.Exists)
    {
    if (xMainDir.GetFiles().Length > 0)
    {
    System.IO.FileInfo[] filein_array = xMainDir.GetFiles();
    foreach (System.IO.FileInfo fi in filein_array)
    {
    string file = xMainDir + "\\" + fi.Name; <--- changed attachment address like this
    Attachment attach = new Attachment(file);
    msg.Attachments.Add(attach);

    }
    }
    }
     
  6. mjp

    mjp

    Thanks for posting the follow-up!
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page