.net 发送电子邮件

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace LFSYNC
{
    
class EMail
    {
        
private string _from;
        
public string From
        {
            
get { return _from; }
            
set { _from = value; }
        }

        
private string _to;
        
public string To
        {
            
get { return _to; }
            
set { _to = value; }
        }

        
private string _subject;
        
public string Subject
        {
            
get { return _subject; }
            
set { _subject = value; }
        }

        
private string _body;
        
public string Body
        {
            
get{return _body;}
            
set{_body=value;}
        }

        
private string _server;
        
public string Server
        {
            
get { return _server; }
            
set { _server = value; }
        }

        
private string _pwd;
        
public string Pwd
        {
            
get { return _pwd; }
            
set { _pwd = value; }
        }

        
/// <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 EMail(string from, string to, string subject, string body, string server, string pwd)
        {
            
this.From = from;
            
this.To = to;
            
this.Subject = subject;
            
this.Body = body;
            
this.Server = server;
            
this.Pwd = pwd;
        }

        
/// <summary> 
        
/// 发送邮件 
        
/// </summary> 
        public  void Send()
        {
          
            
try
            {
                MailAddress mailfrom 
= new MailAddress(this.From);
                MailAddress mailto 
= new MailAddress(this.To);
                MailMessage message 
= new MailMessage(mailfrom, mailto);
                message.Subject 
= this.Subject;//设置邮件主题 
                message.IsBodyHtml = false;//设置邮件正文为html格式 
                message.Body = this.Body;//设置邮件内容 
                SmtpClient client = new SmtpClient(this.Server);
                
//设置发送邮件身份验证方式 
                
//注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com 
                client.Credentials = new NetworkCredential(this.From.Split(new char[] { '@' })[0], this.Pwd);
                client.Send(message);
            }
            
catch(Exception ex)
            {
                
throw ex;
            }
        } 
    }
}
原文地址:https://www.cnblogs.com/skyshenwei/p/1654085.html