使用c#将附件文件发送到电子邮件 [英] sending attachment file to email using c#
本文介绍了使用c#将附件文件发送到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下
private void btn_Sendmail_Click( object sender,EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtpserver = new SmtpClient( smtp .gmail.com跨度>);
mail.From = new MailAddress( noreply@himtmarine.com, 提醒邮件);
mail.To.Add( ns@himtmarine.com);
mail.Subject = 提醒;
mail.Body = 带附件的邮件;
System.Net.Mail.Attachment附件;
attachment = new System.Net.Mail.Attachment( E:/Projects/Book1.xls跨度>);
mail.Attachments.Add(附件);
smtpserver.Credentials = new NetworkCredential( ns@himtmarine.com, himt1234);
smtpserver.Port = 587 ;
smtpserver.EnableSsl = true ;
smtpserver.Send(mail);
MessageBox.Show( mail send);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
当我运行此代码时说错误如下
System.Net.Mail.Attachment'是'类型',但用作'变量'
错误显示在下面的行如下
mail.Attachments.Add(附件);
请帮帮我。
有什么问题。在我的代码中。
Rgds,
Narasiman P
解决方案
< pre lang =c#> mail.Attachments.Add(附件);
应该是:
mail.Attachments.Add(附件);
记住c#区分大小写。
My code as follows
private void btn_Sendmail_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("noreply@himtmarine.com", "reminder mail");
mail.To.Add("ns@himtmarine.com");
mail.Subject = "Reminder";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("E:/Projects/Book1.xls");
mail.Attachments.Add(Attachment);
smtpserver.Credentials = new NetworkCredential("ns@himtmarine.com", "himt1234");
smtpserver.Port = 587;
smtpserver.EnableSsl = true;
smtpserver.Send(mail);
MessageBox.Show("mail send");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
when i run this code says error as follows
System.Net.Mail.Attachment' is a 'type' but is used like a 'variable'
error shows in below line as follows
mail.Attachments.Add(Attachment);
please help me.
what is the problem. in my code.
Rgds,
Narasiman P
解决方案
mail.Attachments.Add(Attachment);
Should be:
mail.Attachments.Add(attachment);
Remember c# is case sensitive.
这篇关于使用c#将附件文件发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文