c# 发送邮件保存服务器,C#通过SMTP服务器发送邮件

一、发送邮件的简单方式

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using .Mail;

using ;

namespace MailSender

{

class Program

{

static void Main(string[] args)

{

//SMTP服务器

SmtpClient smtp = new SmtpClient("");

// SMTP服务器用户名,密码

smtp.Credentials = new NetworkCredential("okbase", "abcdefg");

MailMessage mail = new MailMessage();

mail.From = new MailAddress("123456@");// 发件人

mail.To.Add("okbase@");// 收件人

mail.Subject = "文件已经发给你了";

mail.Body = "请及时查收";

mail.BodyEncoding = Encoding.UTF8;

mail.IsBodyHtml = false;

mail.Priority = MailPriority.Normal;

try

{

smtp.Send(mail);

Console.WriteLine("发送成功");

}

catch (.Mail.SmtpException ex)

{

Console.WriteLine(ex.Message);

}

}

}

}

二、利用GMail发送邮件的例子

(代码来自网络)

using .Mail;

MailMessage msg = new MailMessage();

msg.To.Add("a@,b@");//收件者,以逗号分隔不同收件者

//.Add("c@");//副本

//msg.Bcc.Add("d@");//密件副本

//3个参数分别是发件人地址(可以随便写),发件人姓名,编码

msg.From = new MailAddress("abc@", "发件人姓名", System.Text.Encoding.UTF8);

msg.Subject = "密码确认信";//邮件标题

msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码

msg.Body = "您的密码为︰";//邮件内容

msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码

msg.IsBodyHtml = false;//是否是HTML邮件

msg.Priority = MailPriority.Normal;//邮件优先级

try

{

//建立 SmtpClient 物件 并设定 Gmail的smtp主机及Port

SmtpClient MySmtp = new SmtpClient("smtp.", 587);

//设定你的帐号密码

MySmtp.Credentials = new .NetworkCredential("帐号","密码");

//Gmial 的 smtp 使用 SSL

MySmtp.EnableSsl = true;

//发送Email

//MySmtp.Send("", "", "C# Gmail发信测试", "文件内容");

MySmtp.Send(msg);

Page.ClientScript.RegisterStartupScript(this.GetType() , "MyScript" , "alert('发送成功!');" , true);

}

catch (.Mail.SmtpException ex)

{

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('发送失败!');", true);

}

三、异步发送方式

(代码来自网络)

using .Mail;

...

MailMessage mailMsg = new MailMessage();

mailMsg.From = new MailAddress("你的email地址");

mailMsg.To.Add("接收人1的email地址");

mailMsg.To.Add("接收人2的email地址");

mailMsg.Subject = "邮件主题";

mailMsg.Body = "邮件主体内容";

mailMsg.BodyEncoding = Encoding.UTF8;

mailMsg.IsBodyHtml = false;

mailMsg.Priority = MailPriority.High;

SmtpClient smtp = new SmtpClient();

// 提供身份验证的用户名和密码

// 网易邮件用户可能为:username password

// Gmail 用户可能为:username@ password

smtp.Credentials = new NetworkCredential("用户名", "密码");

smtp.Port = 25; // Gmail 使用 465 和 587 端口

smtp.Host = "SMTP 服务器地址"; // 如 smtp., smtp.

smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true

smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted);

try

{

smtp.SendAsync(mailMsg, mailMsg);

}

catch (SmtpException ex)

{

Console.WriteLine(ex.ToString());

}

...

void SendMailCompleted(object sender, AsyncCompletedEventArgs e)

{

MailMessage mailMsg = (MailMessage)e.UserState;

string subject = mailMsg.Subject;

if (e.Cancelled) // 邮件被取消

{

Console.WriteLine(subject + " 被取消。");

}

if (e.Error != null)

{

Console.WriteLine("错误:" + e.Error.ToString());

}

else

{

Console.WriteLine("发送完成。");

}

}

(全文完)

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 [email protected] 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、付费专栏及课程。

余额充值