Ø  发送邮件所用的核心知识点

  • 微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
  • 微软封装好的SmtpClient类:主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
  • SmtpClient主要进行了三层的封装:Socket --> TcpClient --> SmtpClient

Ø  具体代码请看如下所示:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Timers;
using System.Xml; namespace MyEmailTest
{
   class Program
{
     staticvoid Main(string[] args)
{
       try
{         string senderServerIp = "123.125.50.133";
        string toMailAddress = "mingmingruyuedlut@163.com";
        string fromMailAddress = "mingmingruyuedlut@163.com";
        string subjectInfo = "Test sending e_mail";
        string bodyInfo = "Hello Eric, This is my first testing e_mail";
        string mailUsername = "mingmingruyuedlut";
        string mailPassword = ".........."; //发送邮箱的密码()
        string mailPort = "25";
        string attachPath = "E:\\123123.txt; E:\\haha.pdf"; MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);
email.AddAttachments(attachPath);
email.Send();
}
       catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
} publicclass MyEmail
{
      private MailMessage mMailMessage; //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
      private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
      private int mSenderPort; //发送邮件所用的端口号(htmp协议默认为25)
      private string mSenderServerHost; //发件箱的邮件服务器地址(IP形式或字符串形式均可)
      private string mSenderPassword; //发件箱的密码
      private string mSenderUsername; //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)
      private bool mEnableSsl; //是否对邮件内容进行socket层加密传输
      private bool mEnablePwdAuthentication; //是否对发件人邮箱进行密码验证 ///<summary>
/// 构造函数
///</summary>
///<param name="server">发件箱的邮件服务器地址</param>
///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
///<param name="fromMail">发件人地址</param>
///<param name="subject">邮件标题</param>
///<param name="emailBody">邮件内容(可以以html格式进行设计)</param>
///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)</param>
///<param name="password">发件人邮箱密码</param>
///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param>
///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param>
    public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable)
{
      try
{
mMailMessage = new MailMessage();
mMailMessage.To.Add(toMail);
mMailMessage.From = new MailAddress(fromMail);
mMailMessage.Subject = subject;
mMailMessage.Body = emailBody;
mMailMessage.IsBodyHtml = true;
mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mMailMessage.Priority = MailPriority.Normal;           this.mSenderServerHost = server;
          this.mSenderUsername = username;
          this.mSenderPassword = password;
          this.mSenderPort = Convert.ToInt32(port);
          this.mEnableSsl = sslEnable;
          this.mEnablePwdAuthentication = pwdCheckEnable;
}
        catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} ///<summary>
/// 添加附件
///</summary>
///<param name="attachmentsPath">附件的路径集合,以分号分隔</param>
publicvoid AddAttachments(string attachmentsPath)
{
      try
{
        string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
Attachment data;
ContentDisposition disposition;
          for (int i = 0; i < path.Length; i++)
{
data = new Attachment(path[i], MediaTypeNames.Application.Octet);
disposition = data.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(path[i]);
disposition.ModificationDate = File.GetLastWriteTime(path[i]);
disposition.ReadDate = File.GetLastAccessTime(path[i]);
mMailMessage.Attachments.Add(data);
}
}
        catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} ///<summary>
/// 邮件的发送
///</summary>
      publicvoid Send()
{
       try
{
          if (mMailMessage != null)
{
mSmtpClient = new SmtpClient();
            //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
mSmtpClient.Host = this.mSenderServerHost;
mSmtpClient.Port = this.mSenderPort;
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.EnableSsl = this.mEnableSsl;
            if (this.mEnablePwdAuthentication)
{
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
              //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
              //NTLM: Secure Password Authentication in Microsoft Outlook Express
mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
}
            else
{
mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
}
mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mSmtpClient.Send(mMailMessage);
}
}
        catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}

C#实现邮件发送的功能的更多相关文章

  1. C# Email邮件发送,功能是密码找回或者重置功能。

    原文:C# Email邮件发送,功能是密码找回或者重置功能. 最近根据公司需求,写个邮件发送.   这里面的传入的地址信息的参数都是经过加密的.  主要是保证用户信息的安全. 帮助类   using ...

  2. C#、ASP.NET、WinForm - 实现邮件发送的功能

    转载自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html 发送邮件所用的核心知识点 微软封装好的Mail ...

  3. thinkphp 邮件发送

    最近项目上要求,要做个邮件发送的功能,因为用到的框架是ThinkPHP,于是就自己整理一下. 引入class.phpmailer.php,大家可以去这个链接去下载: http://pan.baidu. ...

  4. C# QQ & 163 邮件发送

    这篇文章的目的并不是说明如果进行右键的发送,因为在.net 坝坝的怀抱下邮件发送的功能实现并不会很难,当然邮件发送的代码,还是会贴上的,昨天在写一个邮件发送的功能,我直接找到了原来的代码,想着直接就可 ...

  5. SSH网上商城---邮件发送

    注册网站账号的时候,都需要发送激活邮件,然后让注册的用户点击激活链接方可完成注册,不过话说回来,为什么注册的时候需要发送邮件呢?为什么不注册的时候直接激活呢?一定要收一封激活帐号的邮件?网站这样做的好 ...

  6. SpringBoot项目实现文件上传和邮件发送

    前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...

  7. 玩转 SpringBoot2.x 之整合邮件发送

    序 在实际项目中,经常需要用到邮件通知功能.比如,用户通过邮件注册,通过邮件找回密码等:又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多. 原文地址:https://www.mm ...

  8. .NET开发邮件发送功能的全面教程(含邮件组件源码)

    今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知识 2)         ...

  9. 结合ABP源码实现邮件发送功能

    1. 前言 2. 实现过程 1. 代码图(重) 2.具体实现 2.1 定义AppSettingNames及AppSettingProvider 2.2 EmailSenderConfiguration ...

随机推荐

  1. 用Keras搞一个阅读理解机器人

    catalogue . 训练集 . 数据预处理 . 神经网络模型设计(对话集 <-> 问题集) . 神经网络模型设计(问题集 <-> 回答集) . RNN神经网络 . 训练 . ...

  2. 【C#】使用bat文件安装卸载Window服务

    1.安装服务 @echo off @title 安装windows服务path %SystemRoot%\Microsoft.NET\Framework\v4.0.30319echo========= ...

  3. 开源实时消息推送系统 MPush

    系统介绍 mpush,是一款开源的实时消息推送系统,采用java语言开发,服务端采用模块化设计,具有协议简洁,传输安全,接口流畅,实时高效,扩展性强,可配置化,部署方便,监控完善等特点.同时也是少有的 ...

  4. MyBatis-SqlSessionFactory的创建

    Main 方法,mybatis 版本为 3.5.0 解析配置文件的所有信息,保存在 Configuration 中,返回包含 Configuration 的 DefaultSqlSession Map ...

  5. layui(八)——轮播图常见用法总结

    carousel 是 layui 2.0 版本中新增的全新模块,主要适用于跑马灯/轮播等交互场景.它可以满足任何类型内容的轮播式切换操作,更可以胜任 FullPage (全屏上下轮播)的需求,简洁而不 ...

  6. Linux 内核里的数据结构:双向链表

    原文:https://blog.csdn.net/qq_33487044/article/details/78827260 双向链表 Linux 内核自己实现了双向链表,可以在 include/lin ...

  7. Web前端框架与移动应用开发第七章

    1.练习1:焦点图切换 html: <!doctype html><html><head> <meta charset="utf-8" / ...

  8. python 调用 java代码

    一.JPype简述 1.JPype是什么? JPype是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python 在某些领域(如服务器端编程)中的不足. 2.JPype ...

  9. sql server中的merge

    http://www.cnblogs.com/CareySon/archive/2012/03/07/2383690.html 简介 Merge关键字是一个神奇的DML关键字.它在SQL Server ...

  10. 六、文件IO——fcntl 函数 和 ioctl 函数

    6.1 fcntl 函数 6.1.1 函数介绍 #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd ...