0

I am trying to write a code for sending hard coded email with attachment i-e I don't want to use the buttons and text fields. I want when the program runs it should automatically go to location in my drive and attach some files and email it to the email address which I have already told that program while coding.

The normal code with buttons and text fields does not work. See below the normal code

MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
mail.Attachments.Add(new Attachment(attachment1.Text));

SmtpClient client = new SmtpClient(smtp.Text);
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);

I have tried replacing from.Text, to.Text, subject.Text, body.Text and attachment1.Text with a string as

string from="abc@gmail.com";
string attachment1=@"c:\image1.jpg";

They give me errors.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • 2
    What, specifically, is the error(s) you are seeing? Have you tried disabling SSL? (The mail server may not be configured to use SSL) Also, be sure that the TO and FROM addresses are valid addresses. – Heather Mar 24 '14 at 20:54
  • 1
    Need the exact error really.. – Alec. Mar 24 '14 at 20:56
  • Are you using gmail for the smtp server? if you are you might want to check out: http://stackoverflow.com/questions/4677258/send-email-using-system-net-mail-through-gmail-c – PCG Mar 24 '14 at 21:01
  • mail.Attachments.Add(data); – Curtis Hagen Mar 24 '14 at 21:27

1 Answers1

0

Remove the .Text after each variable, as strings don't have a Text property.

Like this:

MailMessage mail = new MailMessage(from, to, subject, body);

idmadj
  • 2,565
  • 2
  • 19
  • 23