c#发送邮件.net1.1和.net2.0中的两个方法
2008-12-02 14:48
447 查看
.net1.1
using System.Web.Mail;
使用:
SendSMTPEMail("100.100.100.100", "someone@xxx.com", "xxxx", "someone@xxx.com", "webtest", TextBox1.Text, null, null);
方法体:
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, string bcc, string cc)
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.BodyFormat = MailFormat.Html;
mail.From = strFrom;
mail.To = strto; //多个收件人之间用分号分割
mail.Bcc = bcc;
mail.Cc = cc;
mail.Subject = "mail test!!!";
mail.Body = TextBox1.Text;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strFrom);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strFromPass);
System.Web.Mail.MailAttachment attachment = new System.Web.Mail.MailAttachment("c:\\log.log");
mail.Attachments.Add(attachment);
SmtpMail.SmtpServer = strSmtpServer;
SmtpMail.Send(mail);
}
或
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="to">收件人邮件地址</param>
/// <param name="from">发件人邮件地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="username">登录smtp主机时用到的用户名,注意是邮件地址'@'以前的部分</param>
/// <param name="password">登录smtp主机时用到的用户密码</param>
/// <param name="smtpHost">发送邮件用到的smtp主机</param>
public void Send(string to, string from, string subject, string body, string username, string password, string smtpHost)
{
MailMessage mail = new MailMessage();
mail.To = to;//设置收件人地址
mail.From = from;//设置发件人地址
mail.Subject = subject;//设置邮件主题
mail.BodyFormat = MailFormat.Html;//设置邮件以HTML格式发送
mail.Body = body;//设置邮件内容
//设置发送邮件时需要身份验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置登录邮件主机时的用户名,注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
//设置登录SMTP主机的用户密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
//设置发送邮件的SMTP主机
SmtpMail.SmtpServer = smtpHost;
//发送邮件,如果发送不成功会抛出异常
SmtpMail.Send(mail);
}
.net2.0
using System.Net.Mail;
使用:
SendSMTPEMail("100.100.100.100", "someone@xxx.com", "xxxx", "someone@xxx.com", "webtest", TextBox1.Text, null, null);
方法体:
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment("c:\\log.log");
message.Attachments.Add(attachment);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
using System.Web.Mail;
使用:
SendSMTPEMail("100.100.100.100", "someone@xxx.com", "xxxx", "someone@xxx.com", "webtest", TextBox1.Text, null, null);
方法体:
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, string bcc, string cc)
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.BodyFormat = MailFormat.Html;
mail.From = strFrom;
mail.To = strto; //多个收件人之间用分号分割
mail.Bcc = bcc;
mail.Cc = cc;
mail.Subject = "mail test!!!";
mail.Body = TextBox1.Text;
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strFrom);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strFromPass);
System.Web.Mail.MailAttachment attachment = new System.Web.Mail.MailAttachment("c:\\log.log");
mail.Attachments.Add(attachment);
SmtpMail.SmtpServer = strSmtpServer;
SmtpMail.Send(mail);
}
或
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="to">收件人邮件地址</param>
/// <param name="from">发件人邮件地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="username">登录smtp主机时用到的用户名,注意是邮件地址'@'以前的部分</param>
/// <param name="password">登录smtp主机时用到的用户密码</param>
/// <param name="smtpHost">发送邮件用到的smtp主机</param>
public void Send(string to, string from, string subject, string body, string username, string password, string smtpHost)
{
MailMessage mail = new MailMessage();
mail.To = to;//设置收件人地址
mail.From = from;//设置发件人地址
mail.Subject = subject;//设置邮件主题
mail.BodyFormat = MailFormat.Html;//设置邮件以HTML格式发送
mail.Body = body;//设置邮件内容
//设置发送邮件时需要身份验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置登录邮件主机时的用户名,注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
//设置登录SMTP主机的用户密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
//设置发送邮件的SMTP主机
SmtpMail.SmtpServer = smtpHost;
//发送邮件,如果发送不成功会抛出异常
SmtpMail.Send(mail);
}
.net2.0
using System.Net.Mail;
使用:
SendSMTPEMail("100.100.100.100", "someone@xxx.com", "xxxx", "someone@xxx.com", "webtest", TextBox1.Text, null, null);
方法体:
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment("c:\\log.log");
message.Attachments.Add(attachment);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
相关文章推荐
- c#发送邮件.net1.1和.net2.0中的两个方法
- c#发送邮件.net1.1和.net2.0中的两个方法
- 用C#发送邮件的编程方法及实例代码分享
- C# 发送邮件方法2
- C#发送邮件的方法及实例代码
- .Net锦囊-C#,.Net发送邮件三种方法…
- C#发送Email邮件方法总结 (转载)
- C#编写的邮件(Mail)发送发送人的信息从web.config或者从Appp.congfig中获取的方法
- C#发送邮件三种方法,Localhost,SMTP,SSL-SMTP
- C#发送邮件的方法及实例代码
- C# 使用系统方法发送异步邮件
- C# 邮件发送方法【NetMail方式】
- C#使用smtp发送带附件的邮件实现方法
- C#发送Email邮件三种方法的总结(转载)
- [转帖]C#发送Email邮件方法总结
- C#实现发送和接收pop3邮件方法
- C# 发送邮件的方法
- C#发送Email邮件方法总结
- C#发送邮件三种方法(Localhost,SMTP,SSL-SMTP)
- C#中利用Lotus notes公共邮箱发送邮件的方法