[英]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 我曾尝试更换
from.Text
, to.Text
, subject.Text
, body.Text
和attachment1.Text
用字符串作为
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. 删除每个变量后的
.Text
,因为字符串没有Text
属性。
Like this: 像这样:
MailMessage mail = new MailMessage(from, to, subject, body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.