zoukankan      html  css  js  c++  java
  • .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;
                }
            } 
        }
    }
  • 相关阅读:
    SCCM 2012系列之新特性
    本地用户管理
    ISA中的WEB链
    Windows Server 2012远程刷新客户端组策略,IE代理设置
    关于单一网络适配器拓扑TMG
    IP及DNS设置(Netsh)
    MIPI接口
    液晶屏MIPI接口与LVDS接口区别(总结)
    色彩和光的知识
    LED全彩显示屏色度空间
  • 原文地址:https://www.cnblogs.com/skyshenwei/p/1654085.html
Copyright © 2011-2022 走看看