zoukankan      html  css  js  c++  java
  • 使用微软的组件发邮件

            /// C#发送邮件函数 该方法调用的是微软的.NET内部组件
            /// </summary>
            /// <param name="from">发送者邮箱</param>
            /// <param name="fromer">发送人</param>
            /// <param name="to">接受者邮箱</param>
            /// <param name="toer">收件人</param>
            /// <param name="Subject">主题</param>
            /// <param name="Body">内容</param>
            /// <param name="file">附件</param>
            /// <param name="SMTPHost">smtp服务器</param>
            /// <param name="SMTPuser">邮箱</param>
            /// <param name="SMTPpass">密码</param>
            /// <returns></returns>
            public bool Sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
            {
                ////设置from和to地址
                MailMessage oMail = new MailMessage();
                try
                {
                    MailAddress from = new MailAddress(sfrom, sfromer);
                    MailAddress to = new MailAddress(sto, stoer);
                    ////创建一个MailMessage对象
                    oMail = new MailMessage(from, to);
                    //// 添加附件
                    if (sfile != "")
                    {
                        oMail.Attachments.Add(new System.Net.Mail.Attachment(sfile));
                    }
                    ////邮件标题
                    oMail.Subject = sSubject.ToString();
                    ////邮件内容
                    oMail.Body = sBody;
                    ////邮件格式
                    oMail.IsBodyHtml = true;
                    ////邮件采用的编码
                    oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    ////设置邮件的优先级为高
                    oMail.Priority = MailPriority.High;
                    ////发送邮件
                    SmtpClient client = new SmtpClient();
                    ////client.UseDefaultCredentials = false;
                    //smtp服务器
                    client.Host = sSMTPHost;
                    //身份验证
                    if (!sfrom.ToLower().Contains("hotmail") && !sfrom.ToLower().Contains("hexun"))
                    {
                        client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
                    }
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    if (sfrom.ToLower().Contains("gmail"))
                    {
                        client.EnableSsl = true;
                        client.Port = 587;
                    }
                    if (sfrom.ToLower().Contains("hexun"))
                    {
                        client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
                       // client.Credentials = new NetworkCredential(sfrom, sSMTPpass);
                    }
                    if (sfrom.ToLower().Contains("yahoo"))
                    {
                        client.Port = 587;
                    }
                    if (sfrom.ToLower().Contains("hotmail"))
                    {
                        client.EnableSsl = true;
                        client.Credentials = new NetworkCredential(sfrom, sSMTPpass);
                        client.Port = 25;
                    }
                    client.Send(oMail);
                    return true;
                }
                catch (Exception err)
                {
                    return false;
                }
                finally
                {
                    ////释放资源
                    oMail.Dispose();
                }
            }
    

      

  • 相关阅读:
    821. 字符的最短距离
    1122. 数组的相对排序
    258. 各位相加
    C++常见问题之二#define使用中的陷阱
    python进阶二_基本数据类型与操作
    DirectX10一变换(三)
    Android中编译工具链的改动----LLVM份量的增加
    DirectX10一矩阵代数(二)
    DirectX10一向量代数(一)
    基于asp.net + easyui框架,一步步学习easyui-datagrid——实现添加、编辑、删除(三)
  • 原文地址:https://www.cnblogs.com/maijin/p/2813375.html
Copyright © 2011-2022 走看看