Question Errors when sending SMTP attachments from a DataGridView

chairmanPC

Active member
Joined
Apr 19, 2021
Messages
38
Programming Experience
10+
I have created an SMTP program that sends messages with optional multiple attachments. The attachments are listed in a DataGridView.

Here is the code below:

C#:
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage message = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                message.From = new MailAddress(textBoxUserEmail.Text);
                message.To.Add(new MailAddress(textBoxCustEmail.Text));
                message.Subject = "Subject";
                message.IsBodyHtml = true;
                message.Body = textBoxMessage.Text;
                for(int i=0;i<attachmentGridView.Rows.Count;i++)
                {
                    message.Attachments.Add(new Attachment(attachmentGridView.Rows[I].Cells[1].Value.ToString()));
                }
                smtp.Port = 587;
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(textBoxUserEmail.Text, textBoxPassword.Text);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                smtp.Send(message);

                this.Close();
            }catch (SmtpException sendingEx)
            {
                ErrorForm error = new ErrorForm();
                error.label3.Text = sendingEx.Message;
                error.ShowDialog();
                this.Close();

            }catch (Exception otherEx)
            {
                ErrorForm error = new ErrorForm();
                error.label3.Text = otherEx.Message;
                error.ShowDialog();
                this.Close();

            }
        }
Do note that attachment files that I want to send will be listed in a DataGridView. The DGV's name is attachmentGridView.

This is the function that uploads the file.
Open File Code:
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (attachmentGridView.SelectedCells.Count == 1 && attachmentGridView.SelectedCells[0].ColumnIndex == 1)
                {
                    UploadAttachment(attachmentGridView.SelectedCells[0]);
                }
                else
                {
                    ErrorForm error = new ErrorForm();
                    error.label3.Text = "Select a single cell from Attachment column";
                    error.ShowDialog();
                }
            }
            catch (Exception fileEx)
            {
                ErrorForm error = new ErrorForm();
                error.label3.Text = fileEx.Message;
                error.ShowDialog();
            }
        }


private void UploadAttachment(DataGridViewCell dgvCell)
        {
            using (OpenFileDialog fileDialog = new OpenFileDialog())
            {
                //Set File dialog properties
                fileDialog.CheckFileExists = true;
                fileDialog.CheckPathExists = true;
                fileDialog.Filter = "All Files|*.*";
                fileDialog.Title = "Select a file";
                fileDialog.Multiselect = false;

                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                    byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
                    attachmentGridView.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;

                    if (_myAttachments.ContainsKey(dgvCell.RowIndex))
                        _myAttachments[dgvCell.RowIndex] = binaryData;
                    else
                        _myAttachments.Add(dgvCell.RowIndex, binaryData);
                }
            }
        }

However, I hit some problems:

1. If I have no files that I want to send, I get this error:
"The parameter 'fileName' cannot be an empty string.
Parameter name: fileName"
- Problem: I am trying to make uploading the file optional. But the program INSISTS that I must upload an attachment.

2. If some files are uploaded and listed in the DGV, then this error happens:
"Could not find file 'C:\Users\U\source\repos\<Project name>\bin\Release\net46\<FileName>.
- Problem: I uploaded one file from the Documents and Music folder. Example: the file name is called "James Bond.png", uploaded from Pictures folder. And then the error says file 'C:\Users\U\source\repos\<Project name>\bin\Release\net46\James Bond.png' is not found. The program acts as if all files MUST BE UPLOADED in the .net folder of my project directory. No, I want to upload files from different locations.

Is something wrong with my code? How should I fix this?[/I]
 
Last edited:
First things first, please keep each thread to a single topic and each topic to a single thread. You have asked two basically unrelated questions there, so you should have created two threads and asked question in each one. What happens in situations like this is that you end up getting multiple conversations on different subjects crossing over each other and also someone can provide a complete solution to one or the other of the problems and yet the thread cannot be marked resolved, so people then have to read the whole thing to work out what you actually still need help with.

Please edit your post so that you ask about a single issue and provide all and only the information relevant to that issue, then create a new thread and do the same for the other issue. If you can't edit (not sure how many reputation points you require to be able to) then let me know and I'll do the editing for you.
 

Latest posts

Back
Top Bottom