C# 如何发送邮件消息
1.安装NUGET包
MailKit

2.代码如下
using MailKit.Net.Smtp;
using MimeKit;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks; namespace WebApplication1
{
public class MailHelper
{
/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="subj">邮件标题</param>
/// <param name="bodys">邮件内容</param>
/// <param name="mailData">发送邮件需要的基本参数信息</param>
/// <returns></returns> public async Task SendMail(string subj, string bodys, SaleMail mailData)
{
#region 发邮件需要的基本参数 var fromMail = mailData.LoginAccount; //发件人邮箱账号
var pwd = mailData.LoginPwd;//发件人邮箱登录密码 var toMail = mailData.ReceivedUser;//接收人邮箱账号,多个接收人用逗号分割开
var otherUser = mailData.ReceivedOtherUser;//抄送人邮箱账号,多个抄送人用逗号分割开 var smtpserver = mailData.MailServer;//邮箱服务地址 // smtp.mxhichina.com
var port = mailData.MailPort;//邮箱端口:25 if (string.IsNullOrWhiteSpace(smtpserver) ||
port <= 0 ||
string.IsNullOrWhiteSpace(fromMail) ||
string.IsNullOrWhiteSpace(pwd) ||
string.IsNullOrWhiteSpace(toMail))
{
return;
} #endregion #region 发送内容
var bodyType = "html";//暂时只支持html类型的,后续可以添加其余类型
var message = new MimeMessage
{
Subject = subj,//邮件标题
Body = new TextPart(bodyType)
{
Text = bodys
}
};
#endregion #region 发送人
message.From.Add(new MailboxAddress("", fromMail));//发送人
#endregion #region 收件人 var toMailList = new List<string>();
if (!string.IsNullOrWhiteSpace(toMail))
{
toMailList = toMail.Split(',').ToList();
}
foreach (var item in toMailList)
{
var curToMail = item.Trim();
if (string.IsNullOrWhiteSpace(curToMail))
{
continue;
}
message.To.Add(new MailboxAddress("", curToMail));//接收人 } #endregion #region 抄送人
var others = new List<string>();
if (!string.IsNullOrWhiteSpace(otherUser))
{
others = otherUser.Split(',').ToList();
}
foreach (var s in others)
{
var curOther = s.Trim();
if (string.IsNullOrWhiteSpace(curOther))
{
continue;
}
message.Cc.Add(new MailboxAddress("", curOther));
} #endregion #region 进行发送邮件 SmtpClient smtpClient = new SmtpClient();
smtpClient.Connect(smtpserver, port);//连接邮箱:邮箱服务地址与端口
smtpClient.Authenticate(fromMail, pwd);//发件人邮箱登录密码与登录账号
await smtpClient.SendAsync(message);//发送消息
smtpClient.Disconnect(true); #endregion } } /// <summary>
/// 发送邮件需要的基本参数信息实体
/// </summary>
public class SaleMail
{ /// <summary>
/// 邮箱登录人(发送人)账号
/// </summary>
[Required]
[StringLength(200)]
[DefaultValue("")]
public string LoginAccount { get; set; } /// <summary>
/// (发送人)邮箱登录密码
/// </summary>
[Required]
[StringLength(200)]
public string LoginPwd { get; set; } /// <summary>
/// 接收人(多个按照逗号隔开)
/// </summary>
[Required]
[StringLength(200)]
[DefaultValue("")]
public string ReceivedUser { get; set; } /// <summary>
/// 抄送人(多个按照逗号隔开)
/// </summary>
[Required]
[StringLength(200)]
[DefaultValue("")]
public string ReceivedOtherUser { get; set; } /// <summary>
/// 邮箱服务地址(smtp.mxhichina.com)
/// </summary>
[Required]
[StringLength(200)]
[DefaultValue("")]
public string MailServer { get; set; } /// <summary>
/// 邮箱端口(25)
/// </summary>
[Required]
public int MailPort { get; set; } }
}
3.常见企业邮箱POP/SMTP/IMAP服务器地址设置
原文:http://mail.lzcdc.com/v2/help/detail?id=57
| 全球云邮 | ||||
| 协议 | 无加密 | SSL / TSL | 服务器地址 | |
| POP3 | 110 | 995 | pop3.yunyou.top | |
| SMTP | 25 | 465 / 587 | smtp.yunyou.top | |
| IMAP | 143 | 993 | imap.yunyou.top | |
| 新浪企业邮箱 | ||||
| 协议 | 无加密 | 北京机房网通用户 | 北京机房电信用户 | 广州机房电信用户 |
| POP3 | 110 | pop3.sina.net | pop3x.sina.net | gzpop3.sina.net |
| SMTP | 25 | smtp.sina.net | smtpx.sina.net | gzsmtp.sina.net |
| IMAP | 143 | imap.sina.net | imap.sina.net | imap.sina.net |
| 搜狐企业邮箱 | ||||
| 协议 | 无加密 | 服务器地址 | ||
| POP3 | 110 | mail.sohu.net | ||
| SMTP | 25 | mail.sohu.net | ||
| IMAP | 143 | mail.sohu.net | ||
| 21CN企业邮箱 | ||||
| 协议 | 无加密 | SSL / TSL | 国内服务器 | 海外服务器 |
| POP3 | 110 | 995 | Pop.bnet.cn | Pop-enthk.bnet.cn |
| SMTP | 25 | 465 / 587 | smtp.bnet.cn | smtp-enthk.bnet.cn |
| IMAP | 143 | 993 | imap.bnet.cn | imap-enthk.bnet.cn |
| 阿里云企业邮箱(原万网邮箱) | ||||
| 协议 | 无加密 | SSL / TSL | 服务器地址 | |
| POP3 | 110 | 995 | pop3.mxhichina.com | |
| SMTP | 25 | 465 | smtp.mxhichina.com | |
| IMAP | 143 | 993 | imap.mxhichina.com | |
| 腾讯企业邮箱 | ||||
| 协议 | 无加密 | SSL / TSL | 国内服务器地址 | 海外服务器地址 |
| POP3 | 110 | 995 | pop.exmail.qq.com | hwpop.exmail.qq.com |
| SMTP | 25 | 465 | smtp.exmail.qq.com | hwsmtp.exmail.qq.com |
| IMAP | 143 | 993 | imap.exmail.qq.com | hwimap.exmail.qq.com |
| 163网易企业邮箱 | ||||
| 协议 | 无加密 | SSL / TSL | 服务器地址 | |
| POP3 | 110 | 995 | pop.qiye.163.com | |
| SMTP | 25 | 994 | smtp.qiye.163.com | |
| IMAP | 143 | 993 | imap.qiye.163.com |
|
C# 如何发送邮件消息的更多相关文章
- 为 Confluence 6 配置发送邮件消息
如何配置 Confluence 向外发送邮件: 进入 > 基本配置(General Configuration) > 邮件服务器(Mail Servers).这里列出了所有当前配置的 S ...
- 旧版asp.net 发送邮件代码
说到发送邮件发送,先提一下SMTP(呵呵,高手就跳过这一段吧!). SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的地址传 ...
- Dynamics 365使用代码发送邮件给指定邮箱地址
摘要: 微软动态CRM专家罗勇 ,回复303或者20190213可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 当然,首先要 ...
- iOS12系统应用开发发送邮件
iOS12系统应用开发发送邮件 消息分享是应用社交化和营销的重要途径.除了开发者自己搭建专有的消息分享渠道之外,还可以借助系统自带的各种途径.iOS提供了3种快速分享消息的方式,分别为发送邮件.发送短 ...
- django中向用户发送邮件信息
发送邮件来让用户激活,因此,邮件中需要包含用户信息.但用户信息需要加密才可以.因此加密采用的是itsdangerous中的TimedJSONWebSignatureSerializer. 参考链接:h ...
- grails email 发送邮件插件
1.配置email插件,在Config.groovy文件中配置: plugins { compile ":mail:1.0.5" } 2.配置Config.groovy文件: gr ...
- php引入PHPMailer发送邮件
昨天做了一个发送邮件的功能,如果直接用mail()函数,需要拥有自己的邮件服务器,所有引入PHPMailer类方便快捷,简单写一下开发步骤: 一.拥有自己的邮箱账号(作为发件人邮箱) 分两种情况: 1 ...
- android 发送邮件--实现 send email for android
Android 发送邮件消息 用途:发送验证码,通过邮箱找回密码 不需要调用客户端直接使用代码进行发送 本项目使用到的jar包–本文结尾会附带下载链接 activation.jar additionn ...
- ASP.NET免费发送邮件|
因为之前有做过邮件发送的项目,最近也看一些朋友问起这个的做法,现在拿来给大家查看下.因为那时候是公司的服务器配置的.所以后来自己便在网上找到了一个可以任何个人都是可以使用的邮件发送.小弟新手,高手看到 ...
- System.Web.mail ----虚拟发件人发送邮件
转载别人的 使用SMTP发送邮件 说到邮件发送,先提一下SMTP. SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的 ...
随机推荐
- C# RulesEngine 规则引擎:从入门到看懵
说明 RulesEngine 是 C# 写的一个规则引擎类库,读者可以从这些地方了解它: 仓库地址: https://github.com/microsoft/RulesEngine 使用方法: ht ...
- Vue中常用的几种传值方式
Vue中常用的几种传值方式 1. 父传子 父传子的实现方式就是通过props属性,子组件通过props属性接收从父组件传过来的值,而父组件传值的时候使用 v-bind 将子组件中预留的变量名绑定为da ...
- 46.drf过滤、搜索、排序
DRF的过滤类 drf过滤器在filters模块中,主要有四个类 BaseFilterBackend:过滤基类,留好占位方法待后续继承 SearchFilter:继承BaseFilterBackend ...
- 嵌入式-C语言基础:数组指针
定义一个数组指针,指向二维数组: int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; int (*p)[4];//定义二维指针数组 p=a;//指向二维数组 ...
- 链表实现-回文palindrome判断
1.数字回文判断(逆转,分离未位,砍掉个位,保存原来) s = s * 10 + a%10 a = a/10 2.字符串判断回文 package main //思路: 开发一个栈来来存放链表的上半段f ...
- 基于python的数学建模---图论模型(Dijkstra)
from collections import defaultdict from heapq import * # 堆--先进后出 inf = 99999 # 不连通值 mtx_graph = [[0 ...
- java集合类 collection接口,List集合
java集合类:collection接口,List集合 在java.util包中提供了一些集合类,集合类又被称为容器,常用的有List集合,Set集合,Map集合.下面将介绍collection接口和 ...
- 《回炉重造》——Lambda表达式
前言 Lambda 表达式(Lambda Expression),相信大家对 Lambda 肯定是很熟悉的,毕竟我们数学上经常用到它,即 λ .不过,感觉数学中的 Lambda 和编程语言中的 Lam ...
- 关于mysql远程连接失败的问题解决
解决办法 mysql 数据库user表配置密码 mysql 数据库user表配置plugin字段为mysql_native_password mysql 数据库user表host字段更改为% mysq ...
- 疫情可视化part3
前言 之前在part2中说的添加自定义主题配色已经开发完成了,除此之外我还添加了一些的3d特效. 前期文章 这是part1的文章:https://blog.csdn.net/xi1213/articl ...