发新话题 回复该主题

想使用c#代码发邮件的同时带上指定的附件,但运行时报错,请帮忙看看 [复制链接]

1#
银光图片



在网上找了许多C#发邮件同时带附件的代码,但都不能正常运行,以下为其中的一段代码,也报错了,麻烦懂C#的老大,帮忙看看如何修改,谢谢。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Mail;
  6. using System.Net;
  7. using System.Diagnostics;

  8. namespace Test28
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Email email = new Email()
  15.             {
  16.                 fromEmail = "发送人邮箱",
  17.                 fromPerson = "发件人",
  18.                 toEmail = "接收人邮箱",
  19.                 toPerson = "接收人",
  20.                 encoding = "UTF-8",
  21.                 smtpServer = "选用的邮件服务器平【比如:mail.163.com】",
  22.                 userName = "你邮箱的用户名【比如:xxxxx@163.com】",
  23.                 passWord = "你的邮箱的密码",
  24.                 emailTitle = "邮件标题",
  25.                 emailContent = "邮件内容"
  26.             };
  27.             SendEmail(email);
  28.         }

  29.         #region 邮件发送代码
  30.         /// <summary>
  31.         /// 邮件发送代码
  32.         /// </summary>
  33.         /// <param name="email"></param>
  34.         public static void SendEmail(Email email)
  35.         {
  36.             //try
  37.             //{
  38.             //设置发件人信箱,及显示名字
  39.             MailAddress from = new MailAddress(email.fromEmail, email.fromPerson);
  40.             //设置收件人信箱,及显示名字
  41.             MailAddress to = new MailAddress(email.toEmail, email.toPerson);
  42.             //创建一个MailMessage对象
  43.             MailMessage oMail = new MailMessage(from, to);
  44.             oMail.Subject = email.emailTitle; //邮件标题
  45.             oMail.Body = email.emailContent; //邮件内容
  46.             oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
  47.             System.Net.Mail.Attachment mailAttach_1 = new   Attachment(@"c:\UserCenterLog.txt");//附件
  48.             oMail.Attachments.Add(mailAttach_1);
  49.             oMail.BodyEncoding = System.Text.Encoding.GetEncoding(email.encoding);//邮件采用的编码
  50.             oMail.Priority = MailPriority.High;//设置邮件的优先级为高
  51.             //发送邮件服务器
  52.             SmtpClient client = new SmtpClient();
  53.             client.Host = email.smtpServer; //指定邮件服务器
  54.             client.Credentials = new NetworkCredential(email.userName, email.passWord);//指定服务器邮件,及密码
  55.             //发送
  56.             client.Send(oMail); //发送邮件
  57.             oMail.Dispose(); //释放资源
  58.             //}
  59.             //catch(Exception ex)
  60.             //{
  61.             //    StreamWriter writer = File.AppendText(@"c:\00.txt");
  62.             //    writer.WriteLine(ex.Message);
  63.             //    writer.Close();
  64.             //    writer.Dispose();
  65.             //}
  66.             //finally
  67.             //{

  68.             //}
  69.         }
  70.         #endregion
  71.     }

  72.     class Email
  73.     {
  74.         public string fromEmail { get; set; }
  75.         public string fromPerson { get; set; }
  76.         public string toEmail { get; set; }
  77.         public string toPerson { get; set; }
  78.         public string encoding { get; set; }
  79.         public string smtpServer { get; set; }
  80.         public string userName { get; set; }
  81.         public string passWord { get; set; }
  82.         public string emailTitle { get; set; }
  83.         public string emailContent { get; set; }
  84.     }
  85. }
复制代码
分享 转发
TOP
2#

执行C#那里不能直接粘贴带命名空间或类的代码的,只能执行某个函数里面的代码,下次升级我再加入邮件附件功能吧
TOP
3#

非常期待这个加入这个邮件附件功能,弱弱的问一下,下一次升级大约是什么时候。
TOP
发新话题 回复该主题