locked
Email attachment parameter path wpf application RRS feed

  • Question

  • Hi all sorry to have to ask another question. I have started from scratch on my desktop app and finally got it to work exactly how I want it.

    The only problem I have with it now is that If I do not add an attachment to the email and push the send button the app crashes completely. In the debug in visual studio when it crashes this part of my code is highlighted 

    mail.Attachments.Add(new Attachment(attachment1.Text.ToString()));

    it says 

    "System.ArgumentException: 'The parameter 'fileName' cannot be an empty string.
    Parameter name: fileName' "

    Could someone please have a look and maybe suggest what I can do to stop it crashing and show a message saying why the email can't be sent.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Mail;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Net;
    using Microsoft.Win32;
    using System.IO;
    using System.Security;
    using System.Threading;
    using Xamarin.Forms;
    
    namespace email1
    {
    
    
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                System.Threading.Thread.Sleep(2500);
                InitializeComponent();
            }
            
            private void Send_Click(object sender, RoutedEventArgs e)
            {
                
                MailMessage mail = new MailMessage(ToTXT.Text, FromTXT.Text, SubjectTXT.Text, BodyTXT.Text + Company_Name1.Content
                                                                                                           + Company_Name.Text
                                                                                                           + Reference.Content
                                                                                                           + Refrence_Number.Text
                                                                                                           + Sent_Date.Content
                                                                                                           + this.Date.Text
    
                                                                                                           );
    
    
                    SmtpClient client = new SmtpClient(Smtp_TXT.Text);
                client.Port = 587;
                client.Credentials = new System.Net.NetworkCredential(Use_Txt.Text, Pass_Txt.Text);
                client.EnableSsl = true;
                mail.Attachments.Add(new Attachment(attachment1.Text.ToString()));
    
                if (File.Exists(""))
                {
                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment("");
                    
                }
                  
                client.Send(mail);
    
                MessageBox.Show(" Logged Successfully");
    
                BodyTXT.Text = "";
                Company_Name.Text = "";
                Refrence_Number.Text = "";
                this.Date.Text = "";
                attachment1.Text = "";
    
            }
    
            private void attach_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog dg = new OpenFileDialog();
                if (dg.ShowDialog() == DialogResult) ;
                {
    
                    string path = dg.FileName.ToString();
                    attachment1.Text = path;
                }
            }
    
            private void attachment1_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
            {
    
            }
        }
    }

    I have looked at this code so much I have gone code blind and I cant see a resolution to stop it crashing and send the email

    thanks if you can help 

     


    • Edited by Dazzz12345 Friday, September 4, 2020 12:07 AM
    Thursday, September 3, 2020 11:51 PM

Answers

  • Greetings Dazzz.

    The simplest fix would probably be to just not try to add an attachment if the name is blank

    if(attachment1.Text.Length > 0)
    {
       mail.Attachments.Add(new Attachment(attachment1.Text.ToString()));
    }

    • Marked as answer by Dazzz12345 Friday, September 4, 2020 12:00 PM
    Friday, September 4, 2020 1:04 AM

All replies

  • Greetings Dazzz.

    The simplest fix would probably be to just not try to add an attachment if the name is blank

    if(attachment1.Text.Length > 0)
    {
       mail.Attachments.Add(new Attachment(attachment1.Text.ToString()));
    }

    • Marked as answer by Dazzz12345 Friday, September 4, 2020 12:00 PM
    Friday, September 4, 2020 1:04 AM
  • Seems odd to have empty strings here


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, September 4, 2020 1:54 AM
  • Ante you absolute legend that done exactly the job.

    You wouldn't believe how many nights I have been up until 4am looking at that. i actually had a similar bit of code written and because it didn't work first time i binned it off and started to look else where for a fix.

    Thank you very much for your time in looking at it.

    The empty stings were a work around as you can see my code is not as pretty as it could be but it did what i needed it to do so i didn't correct it.

    Again thank you both for your help on my project.

    • Marked as answer by Dazzz12345 Friday, September 4, 2020 12:00 PM
    • Unmarked as answer by Dazzz12345 Friday, September 4, 2020 12:00 PM
    Friday, September 4, 2020 11:59 AM