C#发送邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

/// <summary>
/// Summary description for sendMail
/// </summary>
///

namespace mailSystem
{
    public class sendMail
    {
        public sendMail()
        {
            //
            // TODO: Add constructor logic here
            //

        }

        public bool send(String[] to, String subject, String body, String file)
        {
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.From = new MailAddress("[email protected]", "IC General Office", System.Text.Encoding.UTF8);
            // message.To.Add(new MailAddress("[email protected]"));
            message.Subject = subject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            //message.CC.Add(new MailAddress("[email protected]"));
            //message.Bcc.Add(new MailAddress("[email protected]"));
            for (int i = 0; i < to.Length; i++)
            {
                message.Bcc.Add(to[i]);
            }
            message.Body = body;
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Priority = System.Net.Mail.MailPriority.High;
            client.Host = "smtp.polyu.edu.hk";
            client.Port = 25;
//由于是用的内网的邮件服务器,就没有用ssl,如果要免费的公共邮箱,一般要加上ssl,而且端口要改
            // client.Credentials = new System.Net.NetworkCredential("[email protected]", "general");
            // client.EnableSsl = true;
            //client.UseDefaultCredentials = true;
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            //object userState = message;
            //client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

            string[] files = file.Split(',');
            for (int i = 0; i < files.Length; i++)
            {
                file = files[i];
                if (file != "")
                {
                    Attachment data = new Attachment(file);

                    //add marks
                    //ContentDisposition dispostion = data.ContentDisposition;
                    //dispostion.CreationDate = file.GetCreationTime(file);
                    //dispostion.ModificationDate = file.
                    //dispostion

                    message.Attachments.Add(data);
                }
            }

            try
            {
                // userState = (userState == null) ? Guid.NewGuid() : userState;
                // client.SendAsync(message, userState);
                client.Send(message);
                Console.WriteLine("Send Success!");
                return true;

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return false;
            }
        }
    }
}

你可能感兴趣的:(C++,c,.net,C#,LINQ)