C#发送邮件

//1.MailMessage
static void sendEmail(string fromAddress, string toAddress,ArrayList ccAddress, string bccAddress,string subject,string body)
        {
            //MailMessage message = new MailMessage(fromAddress, toAddress);

            MailMessage message = new MailMessage();

            message.From = new MailAddress(fromAddress, "OSD hardcopy filing system");
            message.To.Add(toAddress);

            if(ccAddress!=null)
            {
                foreach (string cc in ccAddress)
                {
                    message.CC.Add(cc);
                }
            }            
            message.Bcc.Add(bccAddress);
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("mailserv.mmm.com");
            client.UseDefaultCredentials = true;
            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString());
            }
        }

//2.EmailProperties
       static void sendEmail(ClientRuntimeContext context,string fromAddress,string[] toAddress,subject,body)
        {
                EmailProperties emailProperties = new EmailProperties();
                emailProperties.To = toAddress;
                emailProperties.From = fromAddress;
                emailProperties.Body = body;
                emailProperties.Subject = subject;
                try
                {
                    Utility.SendEmail(context, emailProperties);
                    context.ExecuteQuery();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString());
                }         
        }

 

posted on 2019-04-17 13:21  赢在当下_Victor  阅读(129)  评论(0编辑  收藏  举报

导航