Question About the Problem of Attaching a File to a Mail Attachment

Sahin

New member
Joined
Dec 13, 2020
Messages
1
Programming Experience
Beginner
Hello,

I am developing a desktop application with Windows Form. In the first form that opens, the user fills in the form and adds an attachment (pdf, word, etc.), then saves and exits. I save the file path of this added file in the sql server.

Then, the user who will approve, enters the program and approves the created form. When the approval is given, the form is sent to the relevant person by e-mail. The problem is I can't add the file added in the first form to the mail. So the mail goes but I can't send the attachment.

My codes are as follows;

MY FILE ADD BUTTON (I put the file path to textbox and then save it to the sql server.)
C#:
private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dosya = new OpenFileDialog();
            dosya.Filter = "Tüm Dosyalar | *.*";
            dosya.ShowDialog();
            
            
            string dosyayolu = dosya.FileName;
            string yeniyol = @"\\DOSYA YOLUM" + Guid.NewGuid().ToString();
            File.Copy(dosyayolu, yeniyol);



            textBoxDosyaYolu.Text = Path.GetFileName(yeniyol);


        }
MY MAIL SENDING CODE (My file attach field is on form 1, my email sending process is on form 2);
C#:
public void mailGonderDenetci()
        {
            string[] mails = textBoxDenetciMailAdresleri.Text.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

            MailMessage mesaj = new MailMessage();//mesaj degiskenimiz, göndecegimiz e-posta mızın bütün elemanlarini bulundurmaktadir
            mesaj.From = new MailAddress("MAİL ADRESİM");//mesaj gönderen adres
            SmtpClient smtp = new SmtpClient(); //E-Posta’yi gönderen kullanicinin kimlik bilgilerini tutar
            foreach (var mail in mails)
            {
                mesaj.To.Add(mail);//mail gönderilcek mesaj adresi
            }


            mesaj.IsBodyHtml = true;
            mesaj.Subject = "DENETÇİ OLARAK ATANDINIZ."; //Mail Konusu

            mesaj.Attachments.Clear(); // mail eklerini temizledik
             Attachment attachment;
            attachment = new Attachment("@textBoxDosyaYolu.Text"); //burada ilk formda eklenen dosya yolunu formda textbox ekleyerek yeniden çağırdım
            mesaj.Attachments.Add(attachment);


            string htmlString = getHtml(dataGridView2);  //Mail İçeriği
            mesaj.Body =  htmlString;

            smtp.Credentials = new System.Net.NetworkCredential("MAİL ADRESİM", "ŞİFREM");//Mail gönderen hesabın kullanıcı adı ve şifresi
            smtp.Port = 587; //SMTP sunucusunun port bilgisini tutar.
            smtp.Host = "smtp.office365.com"; // SMTP sunucusunun isim bilgisini tutar.
            smtp.EnableSsl = true;  //:Sunucu SSL istiyorsa bu degeri true yapacagiz. smtp.EnableSsl = true;
            smtp.Send(mesaj); //Maili gönderme islemini yapar.
        }

Thank you very much in advance, I wish you good work.
 
Where is the path of the file actually stored? This makes no sense:
C#:
attachment = new Attachment("@textBoxDosyaYolu.Text");
The argument for that constructor is supposed to be the path of the file to attach. How is that literal value a file path? If you're actually trying to get the path from the same TextBox that you put it, why aren't you referring to the TextBox the same way you did when you put it in? Did you do this:
C#:
"@textBoxDosyaYolu.Text" = Path.GetFileName(yeniyol);
No, you did not. If you don't refer to it that way to get it in, why refer to it that way to get it out?

That said, I'm not even sure that that would work anyway, if the two methods are on different forms. Surely you should be storing the relevant data somewhere with the code in the first form, then reading it from that same location in the second form. Also, you need the path of the file, not just the name, to create the Attachment. It has to know where to find the file, not just what it's called. Also, you should be disposing Attachment objects after sending the message.
 
Back
Top Bottom