Giter VIP home page Giter VIP logo

mafly / mail Goto Github PK

View Code? Open in Web Editor NEW
97.0 10.0 48.0 602 KB

基于 Net.Mail 封装的发送邮件工具类。仅需一行代码,发送邮件。支持自定义邮件发出邮箱、发出方名字等。 支持SSL加密发送。 多个接收人、抄送人。支持群发独显。 支持添加附件、多个附件。 目前大部分主流邮箱全支持。

Home Page: https://www.nuget.org/packages/Mafly.Mail/

C# 100.00%
c-sharp mail email smtp

mail's Introduction

一行代码,发送邮件

====

由于几乎在项目开发中离不开发送邮件功能,所以,我们反感反复造轮子,就基于Net.Mail封装的发送邮件代码。可以用于在.Net项目中发送邮件,只需一行代码,真的是一行。支持多附件、多接收人、多抄送人。

注:欢迎大家使用及提Bug.


特性

  • 支持自定义邮件发出邮箱、发出方名字等。
  • 支持SSL加密发送。
  • 多个接收人、抄送人。
  • 支持群发独显模式(适用于推广、会员群发等)。
  • 支持添加附件、多个附件。
  • 目前大部分主流邮箱全支持。

安装

via NuGet:

1.打开程序包管理器控制台,执行命令:
  Install-Package Mafly.Mail
或:
2.在Project右键,选择“管理NuGet程序包”,在右上角搜索框搜索“Mafly.Mail”。点击安装

via 源代码:

引用如下两个 .dll 文件
  Newtonsoft.Json.dll
  Mafly.Mail.dll
然后在项目目录下建立一个名字为 Config 文件夹,放入 MailSetting.config 文件,根据自己的邮箱服务器进行配置

注:以上两种方式完成后,都要把MailSetting.config复制到输出目录设置为始终复制,或者把生成操作设置为嵌入的资源。 这一步不能少!不能少!!不能少!!!

用法

          var mailService = new Mafly.Mail.Mail();

          //参数:接收者邮箱、内容
          mailService.Send("[email protected]", "测试邮件发送!");
		  
		  //群发单显参数:多接收者邮箱、内容
          mailService.Send("[email protected],[email protected]", "测试【群发单显】邮件发送!", true);

          //参数:接收者邮箱、接收者名字、内容
          mailService.Send("[email protected]", "mafly", "测试邮件发送!");

          //参数:接收者邮箱、接收者名字、邮件主题、内容
          mailService.Send("[email protected]", "mafly", "邮件发送", "测试邮件发送!");

          //使用MailInfo对象模式  参数:接收者邮箱、接收者名字、邮件主题、内容
          mailService.Send(new MailInfo
          {
              Receiver = "[email protected]",
              ReceiverName = "mafly",
              Subject = "邮件发送",
              Body = "测试邮件发送!"
          });

          //使用MailInfo对象模式  参数:接收者邮箱、接收者名字、邮件主题、内容、附件路径
          mailService.Send(
              new MailInfo
              {
                   Receiver = "[email protected]",
                  ReceiverName = "mafly",
                  Subject = "带附件邮件发送",
                   Body = "测试带附件邮件发送!"
              }, "../../Program.cs");

          //使用MailInfo对象模式  参数:接收者邮箱、接收者名字、邮件主题、内容、多附件路径
          mailService.Send(
              new MailInfo
              {
                Receiver = "[email protected]",
                  ReceiverName = "mafly",
                  Subject = "带附件邮件发送",
                  Body = "测试带附件邮件发送!"
              }, new Attachment("../../Program.cs"), new Attachment("../../App.config"));

API

方法 <void> Send(参数...)

该项目对外只有一个Send(...)公开方法,但该方法包含6个重载。几乎可以满足您的全部日常开发需求!

参数 <sting> receiver

接收人邮箱地址,支持多个email地址。 如需多个接收人请用英文逗号,进行分隔。

参数 <string> receiverName

接收人名字,可不填写,默认为null。

参数 <string> body

邮件内容。可以是纯文本,也可以包含 html 代码。

参数 <sting> subject

邮件主题,可不填写,默认为取邮件内容的前15个字符。

参数 <string> filePath

附件路径。发送带附件的邮件时,请填写要发送附件相对于程序运行目录的路径。

参数 <object> MailInfo

邮件信息对象。其中包含以下属性:

  • Receiver 接收者邮箱(多个用英文“,”号分割)
  • ReceiverName 接收者名字
  • Subject 邮件的主题
  • Body 正文内容
  • CC 抄送人集合(多个用英文“,”号分割)
  • Replay 回复地址

参数 <object> Attachment

附件对象。支持多个!具体请点击官方文档:Attachment Class

参数 <object> MailMessage

邮件信息对象。具体请点击官方文档:MailMessage Class

Q&A

你封装的这一个其实并没有什么卵用,很简单阿,有这个必要吗?

是的,你说的对。

为什么不使用现成的那些、在线服务呢?如:SendCloud

你管。

真的是一行代码么?我看到好多行阿。

现在这个社会,没有噱头谁会看呢,您说是吧?

我能再问最后一个问题吗?

不能。

mail's People

Contributors

mafly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mail's Issues

发送者信息在哪里配置?

我是从163邮箱向qq邮箱中发送邮件,163邮箱账号为[email protected];qq邮箱的账号为113@qq。com。我登录qq邮箱查看发送的邮件,邮件的发件人是“NuGet_Mafly ”。

在MailSetting.config中 我配置的DisplayName为【DisplayName:"Cnaylor"】。

catch 方式建議

有 catch 到 exception 建議不要吃掉
比方說 MailConfig 裡面讀不到 設定檔會預設 return new MailConfig()
但是讀檔的地方雖然是過了, 但是發送信件的時候沒有值還是會 exception

以一個套件的使用者來說, 會希望知道真正錯誤的地方在哪, 然後才能解決問題

而您的方案雖然最後會因為發送不出去 , 而導向 163 的 smtp server
對某些 user 來說會誤打誤撞的發送出去 (雖然他們永遠不知道其實設定檔裡面的東西是沒有作用的, 因為根本沒讀到)

再說如果使用者的環境是連不到163 的呢?

感觉不够成熟

最好能用一个邮件队列,所有邮件发送都加到这个队列中,队列的话最好用文本文件去实现。参考 Equeue
最好能有一个发送失败的次数,比如5次,发生失败5次后就不必再发了,然后加入日志。
最好能有一个发送成功和发送失败的表,或者文本,如果用表,那就会依赖数据库,最好用文本。
最好能有一个初始化的方法来设置使用者的信息,比如邮件的host,port,userName等等。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.