博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.net c# 发送邮件代码

Posted on 2010-11-23 20:44  我的风之子  阅读(4990)  评论(0编辑  收藏  举报

转载自:http://blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx

一、 发送邮件程序:(使用System.Web.Mail下的类)

public static bool SendMail(string mailhead, string mailbody, string emailadd)
...{
if (emailadd != "")
...{
//创建MailMessage对象
System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
//设置收件人的邮件地址
mailMsg.To = emailadd;
//设置发送者的邮件地址
mailMsg.From = "wicresoft@126.com";
//设置邮件主题
mailMsg.Subject = mailhead;
//设置邮件内容
mailMsg.BodyFormat = System.Web.Mail.MailFormat.Html;

if (mailbody != "")
...{
mailMsg.Body
= mailbody;

//设置支持服务器验证
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//设置用户名
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "wicresoft");
//设置用户密码
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "wangwei");
try
...{
//设置发送邮件服务器 202.108.5.142
System.Web.Mail.SmtpMail.SmtpServer = "202.108.5.142";
//发送邮件
System.Web.Mail.SmtpMail.Send(mailMsg);
return true;
}
catch
...{
return false;
}
}
else
return false;
}
else
return false;
}


本文来自CSDN博客,转载请标明出处:http:
//blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx

 

二、Net2.0又提供了一个替代方法(使用System.Nw)

 

using System;
using System.Collections.Generic;
using System.Text;

using System.Net.Mail;

namespace MailSender
...{
  
class Program
  ...{
    
static string strHost = string.Empty;
    
static string strAccount = string.Empty;
    
static string strPwd = string.Empty;
    
static string strFrom = string.Empty;

    
static void Main(string[] args)
    ...{
      strHost
= "smtp.163.com";  //STMP服务器地址
      strAccount = "jailu";    //SMTP服务帐号
      strPwd = "123456789";    //SMTP服务密码
      strFrom = "jailu@163.com"; //发送方邮件地址

      Console.WriteLine(sendMail(
"jailu@qq.com", "这是一封测试邮件", "这是一封测试邮件的正文内容") ? "Success" : "Unsuccess");

      Console.ReadLine();
    }

    
/**//// <summary>
    
/// 发送邮件
    
/// </summary>
    
/// <param name="to">接收方邮件地址</param>
    
/// <param name="title">邮件标题</param>
    
/// <param name="content">邮件正文内容</param>
    
/// <returns></returns>
    static bool sendMail(string to, string title, string content)
    ...{
      SmtpClient smtpClient
= new SmtpClient();
      smtpClient.DeliveryMethod
= SmtpDeliveryMethod.Network;//指定电子邮件发送方式
      smtpClient.Host = strHost; ;//指定SMTP服务器
      smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码

      MailMessage mailMessage
= new MailMessage(strFrom,to);
      mailMessage.Subject
= title;//主题
      mailMessage.Body = content;//内容
      mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
      mailMessage.IsBodyHtml = true;//设置为HTML格式
      mailMessage.Priority = MailPriority.High;//优先级

      
try
      ...{
        smtpClient.Send(mailMessage);
        
return true;
      }
      
catch
      ...{
        
return false;
      }
    }
  }
}


本文来自CSDN博客,转载请标明出处:http:
//blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx