Email邮件发送程序
来源:互联网 发布:校园二手交易 php源码 编辑:程序博客网 时间:2024/06/11 01:01
方法一:向单个地址发送邮件,不设置web.config文件
public void SendMail()
{
string mailto = "to@company.com";
string mailfrom = "from@company.com";
System.Net.NetworkCredential credential = new System.Net.NetworkCredential("from_username", "from_password");
SmtpClient smtp = new SmtpClient("smtp.company.com");
smtp.Credentials = credential;
MailMessage message = new MailMessage(mailfrom, mailto);
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "subject here";
message.Body = "body here";
smtp.Send(message);
message.Dispose();
}
方法二、向单个地址发送邮件,设置web.config文件
public void SendMail()
{
string mailto = "to@company.com";
string mailfrom = "from@company.com";
MailMessage message = new MailMessage(mailfrom, mailto);
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "subject here";
message.Body = "body here";
smtp.Send(message);
message.Dispose();
}
在web.config中添加如下:
<system.net>
<mailSettings>
<smtp from="from@company.com">
<network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
</smtp>
</mailSettings>
</system.net>
方法三:群发邮件,设置web.config文件
public void SendEmail()
{
string mailto = "to1@company.com,to2@company.com";
string title = "mail title here";
string content = "mail content here";
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
MailAddressCollection address = new MailAddressCollection();
string[] mailtos = mailto.Split(',');
for (int i = 0; i < mailtos.Length; i++)
{
address.Add(mailtos[i]);
}
foreach (MailAddress add in address)
{
message.To.Add(add);
}
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = title;
message.Body = content;
smtp.Send(message);
message.Dispose();
address.Clear();
}
在web.config中添加如下:
<system.net>
<mailSettings>
<smtp from="from@company.com">
<network host="smtp.company.com" port="25" userName="from_username" password="from_password"/>
</smtp>
</mailSettings>
</system.net>
采用以上方法,如果运行发信程序的计算机上装有邮件监控等杀毒软件,会有失败的警告,但实际已发送成功。解决办法是关闭杀毒软件的监控功能。
方法四
using System.Web.Mail;
public void sendMail()
{
MailMessage mail1 = new MailMessage();
mail1.Body="body here";
mail1.From="xxx@xxx.com";
mail1.To="yyy@yyy.com";
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername","xxx@xxx.com");
mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","********");
SmtpMail.SmtpServer="mail.xxx.com";
SmtpMail.Send(mail1);
}
以上添加的几个 Fields 是用来作SMTP发信认证的,如果你的发信服务器不需要认证,就可以省略这几句。
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
- Email邮件发送程序
- Send Email 发送邮件
- java发送Email邮件
- .NET 发送EMAIL邮件
- C#发送Email邮件
- Spring+Email 发送邮件
- commons-email 发送邮件
- email linux 发送邮件
- JAVA发送Email邮件
- php发送邮件email
- C#发送邮件Email
- Java发送email邮件
- php发送邮件email
- php发送邮件email
- php发送邮件email
- php发送邮件email
- php发送邮件email
- php发送邮件email
- 简单的坐标系旋转关系
- Windows系统维护的完美方案
- ASP连接数据库的11种方法[转]
- 客户端xmlhttp调用webservice
- NET面试题目
- Email邮件发送程序
- 七种武器——.NET工程师求职面试必杀技
- com组件注册与卸载
- Ubuntu Linux系统中使用命令行修改时区
- 中英文字符串截取
- 在VC中进行计时操作(可精确到毫秒)
- FileAttributes 枚举
- 第六章 GDI
- sql server系统表详细说明