您的位置:首页 > 编程语言 > C#

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

2015-08-26 16:26 531 查看
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服务"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: