• 个人写的一个用Jmail发邮件到类,测试成功


    using System;
    using System.IO;


    //杨春
    //2008-6-8

    /// <summary>
    /// Mail 的摘要说明
    /// </summary>
    public class MyMail
    {
        string publicMail = System.Configuration.ConfigurationSettings.AppSettings["PublicMail"].ToString();
        string pubicmMailPassword = System.Configuration.ConfigurationSettings.AppSettings["PublicMailPasword"].ToString();
        string mailUserName = System.Configuration.ConfigurationSettings.AppSettings["MailUserName"].ToString();
        public MyMail()
     {
      //
      // TODO: 在此处添加构造函数逻辑
      //
     }


        /// <summary>
        /// 发邮件
        /// </summary>
        /// <param name="reciver">收件人</param>
        /// <param name="title">主题</param>
        /// <param name="content">内容</param>
        /// <param name="attpath">附件</param>
        /// <returns>是否成功</returns>
        public bool SendMail(string reciver, string title, string content, string attpath)
        {
            try
            {
                if ("" == attpath)
                {
                    return SendMailByJmail(reciver, title, content);
                }
                else
                {
                    if (File.Exists(attpath) == true)
                    {
                        return SendMailByJmail(reciver, title, content, attpath);

                    }
                    else
                    {
                        return SendMailByJmail(reciver, title, content);
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
                Console.Write(ex.Message);
            }

        }


        /// <summary>
        /// 发送无附件邮件
        /// </summary>
        /// <param name="reciver">收件人</param>
        /// <param name="title">主题</param>
        /// <param name="content">内容</param>
        /// <returns>是否成功</returns>
        ///
        private bool SendMailByJmail(string reciver,string title,string content)
        {
            myJmail.MessageClass JmailMessage = new myJmail.MessageClass();
            JmailMessage.Charset = "GB2312";
            JmailMessage.Encoding = "BASE64";
            JmailMessage.ContentType = "text/html";
            JmailMessage.ISOEncodeHeaders = false;
            JmailMessage.Priority = Convert.ToByte(1);
            JmailMessage.From = publicMail;
            JmailMessage.FromName = mailUserName;
            JmailMessage.Subject = title;
            JmailMessage.MailServerUserName = GetLoginNameByEmail(publicMail);
            JmailMessage.MailServerPassWord = pubicmMailPassword;
            JmailMessage.AddRecipient(reciver, "", "");
            JmailMessage.Body = content;

            if (JmailMessage.Send(GetSmtpSererByEmail(publicMail), false))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 发送带附件的邮件
        /// </summary>
        /// <param name="reciver">收件人</param>
        /// <param name="title">主题</param>
        /// <param name="content">内容</param>
        /// <param name="attpath">附件路径</param>
        /// <returns>是否成功</returns>
        private bool SendMailByJmail(string reciver, string title, string content, string attpath)
        {
            myJmail.MessageClass JmailMessage = new myJmail.MessageClass();
            JmailMessage.Charset = "GB2312";
            JmailMessage.Encoding = "BASE64";
            JmailMessage.ContentType = "text/html";
            JmailMessage.ISOEncodeHeaders = false;
            JmailMessage.Priority = Convert.ToByte(1);
            JmailMessage.From = publicMail;
            JmailMessage.FromName = mailUserName;
            JmailMessage.Subject = title;
            JmailMessage.MailServerUserName = GetLoginNameByEmail(publicMail);
            JmailMessage.MailServerPassWord = pubicmMailPassword;
            JmailMessage.AddRecipient(reciver, "", "");
            JmailMessage.AddAttachment(attpath, true, attpath.Substring(attpath.LastIndexOf(".") + 1, 3));
            JmailMessage.Body = content;

            if (JmailMessage.Send(GetSmtpSererByEmail(publicMail), false))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 根据Email地址得到登录名
        /// </summary>
        /// <param name="email">Email</param>
        /// <returns>登录名</returns>
        private string GetLoginNameByEmail(string email)
        {
            string loginName = email.Substring(0,email.IndexOf("@"));
            return loginName;
        }

        /// <summary>
        /// 根据Email得到Smtp服务器地址
        /// </summary>
        /// <param name="email">Email</param>
        /// <returns>stmp服务器地址</returns>
        private string GetSmtpSererByEmail(string email)
        {
            int length = email.Length;
            string smtpServer = "smtp." + email.Substring(email.IndexOf("@")+1, length - email.LastIndexOf("@")-1);
            return smtpServer;
        }


    }

  • 相关阅读:
    20172315 2017-2018-2 《程序设计与数据结构》第一周学习总结
    预备作业03
    寒假作业02
    寒假作业01
    2017-2018-2 20172310『Java程序设计』课程 结对编程练习_四则运算_第二周
    20172310 2017-2018-2 《程序设计与数据结构》第八周学习总结
    2017-2018-2 20172310『Java程序设计』课程 结对编程练习_四则运算_第一周
    20172310 《程序设计与数据结构》实验二报告
    20172310 2017-2018-2 《程序设计与数据结构》第七周学习总结
    20172310 2017-2018-2 《程序设计与数据结构》第六周学习总结
  • 原文地址:https://www.cnblogs.com/88223100/p/1220993.html
Copyright © 2020-2023  润新知