日期:2014-05-18  浏览次数:20861 次

C#调用outlook发邮件
C#调用outlook发邮件,自动将收件人,主题,内容,附件添加进去。最好是不弹出哪个outlook对话框就能直接发送

------解决方案--------------------
C# code

//用vs2005,只需要加一个com组件,名称是:Microsoft outlook 11.0 object library 

using System.Net.Mail;

 /// <summary>

  /// 发送邮件的方法

  /// </summary>

  /// <param name="strSender">发件人邮箱地址</param>

 /// <param name="strPass">发件人邮箱密码</param>

/// <param name="Toer">收件人邮箱地址</param>

 public void MailMessage(string strSender, string strPass, string Toer)

 {

 try

{  MailAddress from = new MailAddress(strSender, "发件人的名称");//发件人邮箱地址

  MailAddress to = new MailAddress(Toer, "收件人的名称");//收件人邮箱地址

MailMessage mailMsg = new MailMessage(from, to);

mailMsg.Body = "我成功了! "; //所要发送的文本

mailMsg.Subject = "你好";//邮箱标题

SmtpClient client = new SmtpClient("smtp.163.com");//smtp服务器地址

// Include credentials if the server requires them.

client.Credentials = CredentialCache.DefaultNetworkCredentials;

client.Send(mailMsg);

MessageBox.Show("OK");

} catch (System.Exception ex)

 { MessageBox.Show("Error!"); }  

}

------解决方案--------------------
C# code
//判断是否连通邮件服务商 
    public static bool SmtpProviderStatus(string pingaddress) 
    { 
        Process p = new Process(); 

        p.StartInfo.FileName = "cmd.exe"; 
        p.StartInfo.UseShellExecute = false; 
        p.StartInfo.RedirectStandardInput = true; 
        p.StartInfo.RedirectStandardOutput = true; 
        p.StartInfo.RedirectStandardError = true; 
        p.StartInfo.CreateNoWindow = true; 
        string pingrst; 
        p.Start(); 
        p.StandardInput.WriteLine("ping -n 1 " + pingaddress); 
        p.StandardInput.WriteLine("exit"); 
        string strRst = p.StandardOutput.ReadToEnd(); 
        if (strRst.IndexOf("(0% loss)") != -1) 
            pingrst = "连接"; 

        else if (strRst.IndexOf("Destination host unreachable.") != -1) 
            pingrst = "无法到达目的主机"; 

        else if (strRst.IndexOf("Request timed out.") != -1) 
            pingrst = "超时"; 

        else if (strRst.IndexOf("Unknown host") != -1) 
            pingrst = "无法解析主机"; 

        else 
            pingrst = strRst; 
        if (pingrst != "连接") 
        { 
            return false; 
        } 
        else 
        { 
            return true; 
        } 
    } 
    //判断是否已经开启端口 
    public static bool Smtpport(string websiteaddress) 
    { 
        bool openstatus = false; 
        System.Net.IPAddress ip = System.Net.IPAddress.Parse(websiteaddress); 
        System.Net.IPEndPoint point = new System.Net.IPEndPoint(ip, 25); 
        try 
        { 
            System.Net.Sockets.TcpClient tc = new System.Net.Sockets.TcpClient(); 
            tc.Connect(ip, 25); 
            openstatus = true; 
        } 
        catch 
        { 
            openstatus = false; 
        } 
        return openstatus; 
    } 
    //返回电子邮件地址 
    public static string[] returnmailaddress(string name) 
    { 
        SqlParameter[] p ={ 
            new SqlParameter("name",name) 
        }; 
        DataTable dt = Oacommand.ExecuteTable("P_returnmailaddress", p); 
        string[] s = new string[dt.Rows.Count]; 
        for (int i = 0; i < dt.Rows.Count; i++) 
        { 
            s[i] = dt.Rows[i][0].ToString(); 
        } 
        return s; 
    } 
    //发送电子邮件 
    public static void Sendmail(string ReceiveName, string Topic, string Content) 
    { 
        if (ConfigurationManager.AppSettings["AllowsendEmail"].ToString() == "yes") 
        { 
            string smtpaddress = Configura