简体   繁体   中英

Sending hardcoded email with attachment

I am trying to write a code for sending hard coded email with attachment ie 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.

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

Like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM