C#利用smtp服务器发送邮件简介

在命名空间using System.Net.Mail中提供方法根据指定的smtp服务器来发送邮件。下面说说如何实现:

  1、首先要发送邮件,需要有一个邮箱帐号,比如网易邮箱、新郎邮箱、qq邮箱等,我以网易的163邮箱为例。然后我们需要知道163邮箱的smtp服务器地址:smtp.163.com。一般常用的Smtp服务器地址为:
    网易126:smtp.126.com
    网易163:smtp.163.com
    搜狐:smtp.sohu.com
    新浪:smtp.sina.com.cn
    雅虎:smtp.mail.yahoo.com

  2、现在我们可以开始实现了。在新建的C# Console Application中,需要加入两个命名空间:

        using  System.Net.Mail;   // 新建邮件、发送邮件需要用到
        using  System.Net;        // 建立认证帐号需要用到

  3、下面是发送邮件的函数:

  
  
public static void SendEmail(
string userEmail, // 发件人邮箱
string userPswd, // 邮箱帐号密码
string toEmail, // 收件人邮箱
string mailServer, // 邮件服务器
string subject, // 邮件标题
string mailBody, // 邮件内容
string [] attachFiles // 邮件附件
)
{
//邮箱帐号的登录名
string username = userEmail.Substring(0, userEmail.IndexOf('@'));
//邮件发送者
MailAddress from = new MailAddress(userEmail);
//邮件接收者
MailAddress to = new MailAddress(toEmail);
MailMessage mailobj
= new MailMessage(from, to);
// 添加发送和抄送
// mailobj.To.Add("");
// mailobj.CC.Add("");

//邮件标题
mailobj.Subject = subject;
//邮件内容
mailobj.Body = mailBody;
foreach (string attach in attachFiles)
{
mailobj.Attachments.Add(
new Attachment(attach));
}

//邮件不是html格式
mailobj.IsBodyHtml = false;
//邮件编码格式
mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
//邮件优先级
mailobj.Priority = MailPriority.High;

//Initializes a new instance of the System.Net.Mail.SmtpClient class
//that sends e-mail by using the specified SMTP server.
SmtpClient smtp = new SmtpClient(mailServer);
//或者用:
//SmtpClient smtp = new SmtpClient();
//smtp.Host = mailServer;

//不使用默认凭据访问服务器
smtp.UseDefaultCredentials = false;
smtp.Credentials
= new NetworkCredential(username, userPswd);
//使用network发送到smtp服务器
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
//开始发送邮件
smtp.Send(mailobj);
}

catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}

}

 4、好了,你也可以去试试给自己的应用程序加上发送邮件的功能了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值