C# ASP.NET发送电子邮件System.Net.Mail

ASP.NET邮件发送教程
本文介绍了使用ASP.NET发送电子邮件的方法,包括使用POP3和SMTP服务器的基础知识,System.Net.Mail命名空间下的关键类详解,以及通过邮件服务提供商的SMTP服务器或本机SMTP虚拟服务器发送邮件的具体步骤。

1.补充知识

(1)POP3和SMTP服务器是什么?

简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件。

(1)POP3具体指什么?

POP3(Post Office Protocol 3)即邮局协议的第3个版本,它是规定个人计算机如何连接到互联网上的邮件服务器进行收发邮件的协议。它是因特网电子邮件的第一个离线协议标准,POP3协议允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时根据客户端的操作删除或保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。POP3协议是TCP/IP协议族中的一员,,由RFC 1939 定义

(2)SMTP具体是指什么?

SMTP的全称是"Simple Mail Transfer Protocol",即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。

 

2.System.Net.Mail

使用ASP.NET发送电子邮件,需要引用System.Net.Mail命名空间。System.Net.Mail 命名空间包含用于将电子邮件发送到简单邮件传输协议 (SMTP) 服务器进行传送的类。

(1)命名空间下有三个比较主要的类:

MailMessage:提供属性和方法来创建一个邮件消息对象,即邮件内容。

Attachment:提供属性和方法来创建一个邮件附件对象,即邮件附件。

SmtpClient:将电子邮件传输到您指定用于邮件传送的 SMTP 主机。

(2)MailMessage类:

From:发送邮件的地址 
To:接收邮件的地址 
Subject:邮件的标题 
Priority:邮件的优先级(分别为为High,Low,Normal) 
Attachments:电子邮件的数据的附件集合
Bcc:密送地址 
Cc:抄送地址 
Body:邮件正文
SubjectEncoding:电子邮件的主题内容使用的编码

IsBodyHtml:邮件正文是否为 Html 格式的值

详细参考:MailMessage

(3)Attachment类:

详细参考:Attachment

(4)SmtpClient类:

DeliveryMethod:指定如何处理待发的电子邮件

Host:SMTP 事务的主机的名称或 IP 地址

Credentials:设置用于验证发件人身份的凭据

详细参考:SmtpClient

 

 3.ASP.NET发送邮件两种方式

(1)通过邮件服务提供商的SMTP来发送邮件

首先需要注册对应服务提供商免费邮箱,因为你要使用邮件服务提供商的SMTP,他们需要对身份进行验证,这样可以避免产生大量的垃圾邮件。

有三个是重要的信息:SMTP服务器、用户名、密码。从网上收集了几个类,可以参考下。

 

#region
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="mailTo">要发送的邮箱</param>
    /// <param name="mailSubject">邮箱主题</param>
    /// <param name="mailContent">邮箱内容</param>
    /// <returns>返回发送邮箱的结果</returns>
    public static bool SendEmail(string mailTo,string mailSubject,string mailContent)
    {
        // 设置发送方的邮件信息,例如使用网易的smtp
        string smtpServer = "smtp.163.com"; //SMTP服务器
        string mailFrom = "XXX@163.com"; //登陆用户名
        string userPassword = "XXX";//登陆密码

        // 邮件服务设置
        SmtpClient smtpClient=new SmtpClient();
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
        smtpClient.Host = smtpServer; //指定SMTP服务器
        smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码

        // 发送邮件设置        
        MailMessage mailMessage = new MailMessage(mailFrom, mailTo); // 发送人和收件人
        mailMessage.Subject = mailSubject;//主题
        mailMessage.Body = mailContent;//内容
        mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
        mailMessage.IsBodyHtml = true;//设置为HTML格式
        mailMessage.Priority = MailPriority.Low;//优先级

        try
        {
            smtpClient.Send(mailMessage); // 发送邮件
            return true;
        }
        catch (SmtpException ex)
        {
            return false;
        }
    }

  

 

(2)利用本机的SMTP虚拟服务器的SMTP来发送邮件

需要进行SMTP配置,还是第一种简单实用。

 

4.参考资料:常用邮件服务器

gmail.com:
POP3服务器地址:pop.gmail.com
SMTP服务器地址:smtp.gmail.com

qq.com:

POP3服务器地址:pop.qq.com
SMTP服务器地址:smtp.qq.com

163.com: 
POP3服务器地址:pop.163.com 
SMTP服务器地址:smtp.163.com 

sina.com: 

POP3服务器地址:pop3.sina.com.cn 
SMTP服务器地址:smtp.sina.com.cn 

yahoo.com: 
POP3服务器地址:pop.mail.yahoo.com 
SMTP服务器地址:smtp.mail.yahoo.com 

sohu.com: 
POP3服务器地址:pop3.sohu.com 
SMTP服务器地址:smtp.sohu.com 

china.com: 
POP3服务器地址:pop.china.com 
SMTP服务器地址:smtp.china.com 


21cn.com: 
POP3服务器地址:pop.21cn.com 
SMTP服务器地址:smtp.21cn.com sina.com: 

实例下载

转载于:https://www.cnblogs.com/Jeremy2001/p/6870175.html

C# 开发的邮件服务器 Features supports pop3 smtp imap etc. supports ssl/tls for pop3, smtp and imap. supports multi mail vitural server, can bind multi domains. supports run as windows service, winform application with or without tray icon control. supports remote management using mailservermanager.exe supports mail recycle bin supports plugins using api interfaces Build and release download source code or use the releases i provided. if from source code you should run afterbuild.bat after build the solution successfuly then you get the debug or release version from application folder. Installation run MailServerLauncher.exe to install as windows service or just run as desktop application with or without tray icon. Configuration run MailServerManager.exe at the machine runs mailserver service or app. Connect to server press connect button from menu. type server localhost or 127.0.0.1 or leave it empty type username Administrator with case sensitive type password emtpy press ok to connect with saving or not Add virtual server type name and select storage api [your vitural server]>system>general, add dns servers for query mailto's domain mx record. [your vitural server]>system>services, enable smtp and pop3 services and set ipaddress binding with or without ssl/tls. the host name is required when set bindings. eg. bind smtp service to smtp.[your.domain] + IPAddress.Any [your vitural server]>system>service>relay, 'send email using DNS' and set at least a local ipaddress binding for email sending. the name of the binding here only a name,not mean domain. [your vitural server]>system>logging, enable logging for all services when something error you can see the details from 'Logs and Events' node [your vitural server]>domains, set email host domain, eg. if your email will be xyz@abc.com then the domain should be abc.domain, description is optional [your vitural server]>security, !!! important, add rules for your service to allow outside access like email client. eg. add a rule 'Smtp Allow All' for smtp service with ip allows between 0.0.0.0 to 255.255.255.255 to enable smtp service for outside access add 'Pop3 Allow All' and 'Rlay Allow All' like that too. [your vitural server]>filters, there are two types of filter named 'DnsBlackList' and 'VirusScan' its configurable by run it's executable from mail server install path. Domain name resolution Add smtp.[your.domain], pop3.[your.domain], imap.[your.domain] resolution to your server public ip address with A record or to your server domain with CNAME record. mx record is optional if [your.domain] has a A record.if not, you shoud add a mx record point to your server ip. Remote management to enable remote management you must add ACL to allow mail server managers connect from outside network. use MailServerAccessManager.exe to add management users or just use administrator. add rules to allow access from specific IPs or ip ranges The users here only for management cases.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值