C# 邮件的发送与接收SMTP和POP3

开启配置

本文以QQ邮箱为例,在使用之前需要在QQ邮箱中开启POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务

1、点击设置

在这里插入图片描述

2、点击帐户

在这里插入图片描述

3、开启服务

在这里插入图片描述
选择要开启的服务,点击开启后按提示发送验证码
在这里插入图片描述
发送验证码后会有一个授权码,后续操作需要使用在这里插入图片描述
开启后授权码遗忘,可以直接点击生成授权码重新获取在这里插入图片描述

控件说明

使用控件:LumiSoft.Net
在NuGet包管理工具搜索安装即可
在这里插入图片描述

SMTP协议 发送邮件

//服务器地址
string server = "smtp.qq.com";
//端口号
int port = 465;
//发件人邮箱
string senderEmail = "aaa@qq.com";
//发件人密码
string mima = "填入授权码";
//收件人邮箱
string receiverEmail = "bbb@qq.com";

using (SMTP_Client client = new SMTP_Client())
{
    //连接到指定主机 参数:主机 端口 是否启用SSL
    client.Connect(server, port, true);

    //发送Ehlo/HELO命令
    client.EhloHelo("smtp.qq.com");

    AUTH_SASL_Client_Plain sasl = new AUTH_SASL_Client_Plain(senderEmail, mima);
    //向SMTP服务器发送AUTH命令
    client.Auth(sasl);
    //向SMTP服务器发送MAIL命令 参数:发件人地址,消息大小以字节表示 -1表示消息大小未知
    client.MailFrom(senderEmail, -1);

    Mail_Message mm = new Mail_Message();
    mm.MimeVersion = "1.0";
    mm.MessageID = MIME_Utils.CreateMessageID();
    mm.Date = DateTime.Now;
    mm.Subject = "张三";
    mm.From = new Mail_t_MailboxList() { new Mail_t_Mailbox("张三", senderEmail) };
    mm.To = new Mail_t_AddressList() { new Mail_t_Mailbox("李四", receiverEmail) };

    MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
    contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
    MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
    mm.Body = multipartMixed;

    using (MemoryStream stream = new MemoryStream())
    {
        mm.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
        stream.Position = 0;
        client.SendMessage(stream);
    }
}  

POP3协议 接收邮件

//主机名
string server = "pop.qq.com";
//端口
int port = 995;
//发件人邮箱
string user = "aaa@qq.com";
//发件人密码
string mima = "填入授权码";

POP3_Client pop3 = new POP3_Client();
//连接
pop3.Connect(server, port, true);
//登录
pop3.Login(user, mima);

//读取邮件
foreach (POP3_ClientMessage item in pop3.Messages)
{
    //获取消息头
    Mail_Message mime = Mail_Message.ParseFromByte(item.HeaderToByte());
    string subject = mime.Subject;
    //收件日期
    DateTime date = mime.Date;

    //获取消息
    Mail_Message mimeMessage = Mail_Message.ParseFromByte(item.MessageToByte());
    string body = mimeMessage.BodyText;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值