编程语言
首页 > 编程语言> > C#_发邮件

C#_发邮件

作者:互联网

 private void SendMail(string toAddress,string ccAddress,string subject,string body)
 {
     string[] arrToAddress = toAddress.Split(';');//多个收件人邮箱地址以分号隔开
     string[] arrCcAddress = ccAddress.Split(';');//多个抄送人邮箱地址以分号隔开
     
     MailMessage mailMessage = new MailMessage();//定义邮件
     SmtpClient smtpClient = new SmtpClient();//定义连接,发送邮件
     if (toAddress.Length > 0)
     {
         foreach (string item in arrToAddress)
         {
             if (!string.IsNullOrEmpty(item.Trim()))
             {
                 mailMessage.To.Add(item.Trim());//在邮件中添加收件人
             }
         }
     }
     if (ccAddress.Length > 0)
     {
         foreach (string item in arrCcAddress)
         {
             if (!string.IsNullOrEmpty(item.Trim()))
             {
                 mailMessage.To.Add(item.Trim());//在邮件中添加抄送人
             }
         }
     }
     try
     {
         mailMessage.From = new MailAddress("");//邮件发送人地址,如xxx@yyyy.com
         mailMessage.Subject = subject;//邮件主题
         mailMessage.Body = body;//邮件内容
         mailMessage.IsBodyHtml = true;//HTML格式
         mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//编码UTF8
         mailMessage.Priority = MailPriority.Normal;//邮件发送的优先性为正常
         
         //smtpClient.Credentials = new NetworkCredential(emailAcount, emailPassword);
         //该语句是设置发送人邮件的账号密码,由于本次连接的服务器不需账号密码,所以不需要此语句,若连接其他服务器请用该语句
         
         smtpClient.Host = "172.20.168.106";//发送连接服务器主机IP
         smtpClient.Port = 25;//端口号
         smtpClient.UseDefaultCredentials = false;//使用默认凭据
         smtpClient.EnableSsl = false;//启动SSL,即安全发送
         smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//传递电子邮件消息,通过网络发送电子邮件到SMTP
         smtpClient.Send(mailMessage);//确认发送按钮
     }
     catch (Exception e)
     {
         throw (e);
     }
 }

标签:发邮件,string,smtpClient,C#,mailMessage,发送,item,邮件
来源: https://www.cnblogs.com/diffknow/p/15774648.html