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.)
MY MAIL SENDING CODE (My file attach field is on form 1, my email sending process is on form 2);
Thank you very much in advance, I wish you good work.
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);
}
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.