Curtis Hagen
Curtis Hagen

Reputation: 17

Sending hardcoded email with attachment

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="[email protected]";
string attachment1=@"c:\image1.jpg";

They give me errors.

Upvotes: 1

Views: 693

Answers (1)

idmadj
idmadj

Reputation: 2645

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

Like this:

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

Upvotes: 0

Related Questions