tnblog
首页
视频
资源
登录

使用OutLook发送邮件

3687人阅读 2018/12/20 14:25 总访问:35815 评论:0 收藏:0 手机
分类: C#
  public static void Outlook(string Subject, string TextBody, string FromAdd, string FromPass, string To, string CC, List<string> attach)
        {
            try
            {
                System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("outlook");
                if (MyProcess.Length == 0)
                {
                    throw new System.Exception("请先启动OutLook!");
                }
            }
            catch (System.Exception)
            {
                throw;
            }

            Microsoft.Office.Interop.Outlook.Application app = null;
            NameSpace ns = null;
            Microsoft.Office.Interop.Outlook._MailItem message = null;
            try
            {

                app = new Microsoft.Office.Interop.Outlook.Application();
                ns = app.GetNamespace("mapi");

                ns.Logon(FromAdd, FromPass, false, true);
                message = (Microsoft.Office.Interop.Outlook._MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
                message.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
                
                message.To = To;
                message.CC = CC;
                message.Subject = Subject;
                message.HTMLBody = TextBody;
                
                if (attach != null)
                {
                    foreach (string att in attach)
                    {
                        message.Attachments.Add(att);
                    }
                }
                message.Send();
                ns.Logoff();
            }
            catch
            {
                throw;
            }
            finally
            {
                
                message = null;
                ns = null;
                app = null;
                
            }
        }
    }
}


评价
如果帅也是一种错,那我宁愿一错再错。。。。
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术