Email attachment parameter path wpf application

  • Thread starter Thread starter Dazzz12345
  • Start date Start date
D

Dazzz12345

Guest
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

Continue reading...
 
Back
Top