本文介绍了使用c#将附件文件发送到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的代码如下 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( [email protected], 提醒邮件); mail.To.Add( [email protected]); 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( [email protected], 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 解决方案 My code as followsprivate void btn_Sendmail_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("[email protected]", "reminder mail"); mail.To.Add("[email protected]"); 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("[email protected]", "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 followsSystem.Net.Mail.Attachment' is a 'type' but is used like a 'variable'error shows in below line as followsmail.Attachments.Add(Attachment);please help me.what is the problem. in my code.Rgds,Narasiman P 解决方案 这篇关于使用c#将附件文件发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 07:59