C#用QQ邮箱发送邮件代码

using System.Net.Mail;


   class SendMail { public static string Send(string stmp, string stmpUser, string stmpPwd,string fromMail,string
             toMail,string subject,string body,ArrayList attachment,string cc)
        {
            SmtpClient client = new SmtpClient();
            client.Host = stmp;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(stmpUser, stmpPwd);
         
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            System.Net.Mail.MailMessage message = new MailMessage(fromMail, toMail);
            message.Subject = subject;
            message.Body = body;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;

            if (!string.IsNullOrEmpty(cc)) { message.CC.Add(cc); } //添加附件
            foreach(object obj in attachment)
            {
                Attachment data = new Attachment(obj.ToString(), System.Net.Mime.MediaTypeNames.Application.Octet);
                message.Attachments.Add(data);
            }

            try
            {
                client.Send(message);
                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
    }
 
 
在QQ邮箱的设置->账户里,有一个选项"开启POP3/SMTP服务"

转载于:https://www.cnblogs.com/ninestart/p/4760844.html

你可能感兴趣的:(C#用QQ邮箱发送邮件代码)