zoukankan      html  css  js  c++  java
  • .net 发送邮件

    #region 发送指定邮箱publicstaticstring SendMail(stringfrom, string fromname, string to, string subject, string body, string username, string password, string server, string fujian)
        {
            try
            {
                //邮件发送类            MailMessage mail =new MailMessage();
                //是谁发送的邮件            mail.From =new MailAddress(from, fromname);
                //发送给谁            mail.To.Add(to);
                //标题            mail.Subject = subject;
                //内容编码            mail.BodyEncoding = Encoding.Default;
                //发送优先级            mail.Priority = MailPriority.High;
                //邮件内容            mail.Body = body;
                //是否HTML形式发送            mail.IsBodyHtml =true;
                //附件if (fujian.Length >0)
                {
                    mail.Attachments.Add(new Attachment(fujian));
                }
                //邮件服务器和端口            SmtpClient smtp =new SmtpClient(server, 25);
                smtp.UseDefaultCredentials =true;
                //指定发送方式            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                //指定登录名和密码            smtp.Credentials =new System.Net.NetworkCredential(username, password);
    
                //mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication 
                //mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", username); //set your username here 
                //mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); //set your password here
    
                //超时时间            smtp.EnableSsl =false;
                smtp.Timeout =10000;
                smtp.Send(mail);
                return"成功发送请注意查收";
            }
            catch (Exception exp)
            {
                return exp.Message;
            }
        }
     ///<summary>///   发送邮件
        ///</summary>///<param   name= "server "> smtp地址 </param>///<param   name= "username "> 用户名 </param>///<param   name= "password "> 密码 </param>///<param   name= "from "> 发信人地址 </param>///<param   name= "to "> 收信人地址 </param>///<param   name= "subject "> 邮件标题 </param>///<param   name= "body "> 邮件正文 </param>///<param   name= "IsHtml "> 是否是HTML格式的邮件 </param>publicstaticstring SendMail(stringfrom, string to, string subject, string body, string server, string username, string password, bool IsHtml)
        {
            try
            {
                //设置SMTP 验证,端口默认为25,如果需要其他请修改            SmtpClient mailClient =new SmtpClient(server, 25);
    
    
                //指定如何发送电子邮件。
                //Network   电子邮件通过网络发送到   SMTP   服务器。    
                //PickupDirectoryFromIis   将电子邮件复制到挑选目录,然后通过本地   Internet   信息服务   (IIS)   传送。    
                //SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。  
                mailClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    
    
                //创建邮件对象            MailMessage mailMessage =new MailMessage(from, to, subject, body);
    
                //定义邮件正文,主题的编码方式            mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
                mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
                //mailMessage.BodyEncoding = Encoding.Default;
                //获取或者设置一个值,该值表示电子邮件正文是否为HTML            mailMessage.IsBodyHtml = IsHtml;
    
                //指定邮件的优先级            mailMessage.Priority = MailPriority.High;
    
                //发件人身份验证,否则163   发不了
                //表示当前登陆用户的默认凭据进行身份验证,并且包含用户名密码            mailClient.UseDefaultCredentials =true;
                mailClient.Credentials =new System.Net.NetworkCredential(username, password);
    
                //发送            mailClient.Send(mailMessage);
                return"发送成功";
            }
            catch (Exception exp)
            {
                return exp.Message;
            }
        }
    
    
        //发送plaintxtpublicstaticvoid SendText(stringfrom, string to, string subject, string body, string server, string username, string password)
        {
            SendMail(from, to, subject, body, server, username, password, false);
        }
    
    
    在你的事件里面调用方法
    
     SendText("邮箱@163.com", txtMail.Text, "测试一下", "文本", "smtp.163.com", "邮箱@163.com", "密码");
  • 相关阅读:
    hdu 2485 Destroying the bus stations 迭代加深搜索
    hdu 2487 Ugly Windows 模拟
    hdu 2492 Ping pong 线段树
    hdu 1059 Dividing 多重背包
    hdu 3315 My Brute 费用流,费用最小且代价最小
    第四天 下载网络图片显示
    第三天 单元测试和数据库操作
    第二天 布局文件
    第一天 安卓简介
    Android 获取存储空间
  • 原文地址:https://www.cnblogs.com/soundcode/p/2719534.html
Copyright © 2011-2022 走看看