C#编程实现发送邮件的方法(可添加附件)

所属分类: 软件编程 / C#教程 阅读数: 86
收藏 0 赞 0 分享

本文实例讲述了C#编程实现发送邮件的方法。分享给大家供大家参考,具体如下:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsFormsApplication63
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  //打开上传附件的对话框
  private void btnUP_Click(object sender, EventArgs e)
  {
   oFDialogSFile.InitialDirectory = "C:\\";//设置对话框的初始目录为C盘
    oFDialogSFile.Filter = "all files (*.*)|*.*";//筛选字符串为所有文件
    oFDialogSFile.RestoreDirectory = true;
   oFDialogSFile.ShowDialog();
   cboxAccessories.Items.Add(oFDialogSFile.FileName.Trim());//当选择好文件后将文件名赋值给下拉框
  }
  //发送邮件
  private void btnSend_Click(object sender, EventArgs e)
  {
    try
    {
     string file = Application.StartupPath + "testXML.xml";
     //SmtpClient下的一个对象,用以设置邮件的主题和内容
      System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
     //发送端到接收端的邮箱地址
      myMail = new System.Net.Mail.MailMessage(txtSEmail.Text.Trim(), txtCEmail.Text.Trim());
     myMail.Subject = txtETitle.Text.Trim();
     myMail.Body = txtEContent.Text.Trim();
     if (cboxAccessories.Items.Count > 0)
     {
      for (int i = 0; i < cboxAccessories.Items.Count; i++)
      { //建立邮件附件类的一个对象,语法格式为System.Net.Mail.Attachment(文件名,文件格式)
       System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(
       cboxAccessories.Items[i].ToString(), System.Net.Mime.MediaTypeNames.Application.Octet);
       //MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间
       System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
       disposition.CreationDate = System.IO.File.GetCreationTime(file);
       disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
       disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
       //用smtpclient对象里attachments属性,添加上面设置好的myattachment
       myMail.Attachments.Add(myAttachment);
      }
     }
     //建立发送对象client,验证邮件服务器,服务器端口,用户名,以及密码
     System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(txtSService.Text.Trim(), Convert.ToInt32(txtServicePort.Text.Trim()));
     client.Credentials = new System.Net.NetworkCredential(txtUPwd.Text.Trim(), txtCEmail.Text.Trim());
     client.Send(myMail);
     MessageBox.Show("邮件发送成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
     MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
   }
  private void 删除上传文件_Click(object sender, EventArgs e)
  {
   if (cboxAccessories.Text == "")
   {
    MessageBox.Show("没有附件可删!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
   else
   {
    cboxAccessories.Items.Remove(cboxAccessories.Text.Trim());
   }
  }
  }
}

效果图如下:

点击发送,效果如下

总结一下,发送邮件的过程如下:

1.建立System.Net.Mail.MailMessage下的对象,设置邮件的内容和主题。
2.如果要添加附件,建立System.Net.Mail.Attatch下的对象,用1中的对象中的添加附件的属性读取它。
3.SEND邮件(MAIL+附件)。

如果要实现群发,则可以将发送邮件的代码(TRY附近的几行)写成方法,循环调用即可。

希望本文所述对大家C#程序设计有所帮助。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多