Hi Guys, I'm quickl writing a little application which sends an email via SMTP and has the option to add a attachment. This is working fine however, When I went to send a email without attaching a file the Error Message I get is the following "The Parameter 'fileName' cannot be an empty string. Parameter: fileName"

I can understand that the system is expecting there to be a file to link (which does work when wanting to attach a file)

but I'm not to sure where to add the conditioning so that the attach method would only be called IF the 'Attach File' button is clicked.

Below is the Code I have so Far...

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Collections;
using System.Web;


namespace SendEmail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Enabled = false;

            //Lists Support Teams in DropDown Menu
            ArrayList supportTeam = new ArrayList();
            supportTeam.Add("Technical Support");
            supportTeam.Add("Software Support");


            foreach (string child in supportTeam)
            {
                comboBox1.Items.Add(child);
            }

        }

        private void Send_Click(object sender, EventArgs e)
        {


            try
            {
                SmtpClient client = new SmtpClient("smtp.********.com");



                MailMessage message = new MailMessage("************@********.com", "****.*******@********.com");
                message.Body = " \nDescription:\n  "+ contentBox.Text + "\n\n\nCompany Name:  " + compName.Text + "\nCustomer Contact:  " + personName.Text;
                Attachment data = new Attachment(textBox1.Text);
                message.Attachments.Add(data);
                client.Credentials = new System.Net.NetworkCredential("********.*******@*******.com", "*********");
                client.EnableSsl = true;
                client.Port = Convert.ToInt32("25");

                if (comboBox1.SelectedItem == "Technical Support".ToString())
                {
                    message.Subject = "Tech Support Issue from Qwik Tickets";
                }
                else if (comboBox1.SelectedItem == "Software Support".ToString())
                {
                    message.Subject = "Software Support Issue from Qwik Tickets!";
                }





                client.Send(message);

                MessageBox.Show("                                                      Message Sent...\n\n       A member of support team will be with you as soon as possible.");
                Application.Exit();

            }

            catch (Exception ex)
            {
                MessageBox.Show("Cannot Send Message: " + ex.Message);
            }

        }

        private void ClearFields_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                notifyIcon1.Visible = true;
                notifyIcon1.BalloonTipText = "I'm still here!";
                this.Hide();
            }
        }

        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = false;
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {}

        private void attach_Click(object sender, EventArgs e)
        {
            string Chosen_File = "";


            openFD.Title = "Add a File...";
            openFD.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFD.FileName = "";
            openFD.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|PDF File|*.pdf";

            openFD.ShowDialog();

            Chosen_File = openFD.FileName;
            textBox1.Text = Chosen_File.ToString();

        }

        private void compName_TextChanged(object sender, EventArgs e)
        {
        }

        private void compName_Leave(object sender, EventArgs e)
        {
            if (compName.Text == "")
            {
                MessageBox.Show("         Please Fill in the Company Name.");
                compName.Focus();
            }
        }

        private void personName_Leave(object sender, EventArgs e)
        {
            if (personName.Text == "")
            {
                MessageBox.Show("         Please fill in your Name.");
                personName.Focus();
            }
        }

        }


        }

any help would be realy great. Thanks.

Mark.

You can change line 56/57 to:

if (!string.IsNullOrEmpty(textBox1.Text))
{
    Attachment data = new Attachment(textBox1.Text);
    message.Attachments.Add(data);
}

Hi Pritaeas,

Many thanks for that. Worked a treat!

Now, just while your reading this is there any way of formatting the String information that's sent to our email??

as you can see the following is sent to us...

message.Body = " \nDescription:\n "+ contentBox.Text + "\n\n\nCompany Name: " + compName.Text + "\nCustomer Contact: " + personName.Text;

I just really wanted to make 'Description, Company Name and Customer Contact BOLD if at all possible?

Many thanks for your help so far.

Mark.

IIRC MailMessage has a property to use HTML for the body text. If you use that you can just send a HTML string, and format at will.

pritaeas, Many thanks once again! Your a Legend.. both anwsers worked perfecly.

Many thanks for your help on this! Your a Great help :)

Regards
Mark.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.