C#怎么调用QQ_Mail发送邮件
发表于:2025-11-12 作者:千家信息网编辑
千家信息网最后更新 2025年11月12日,本篇内容主要讲解"C#怎么调用QQ_Mail发送邮件",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#怎么调用QQ_Mail发送邮件"吧!代码案例一:pr
千家信息网最后更新 2025年11月12日C#怎么调用QQ_Mail发送邮件
本篇内容主要讲解"C#怎么调用QQ_Mail发送邮件",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#怎么调用QQ_Mail发送邮件"吧!
代码案例一:
private void button1_Click(object sender, EventArgs e) { string myMaillAdress = ""; string myMaillPassword = ""; string myMaillMessage = ""; string toMaillAdress = ""; QQEmail qq_mial = new QQEmail(); bool send_status = qq_mial.IsSendEmail(myMaillAdress, toMaillAdress, myMaillMessage, myMaillPassword); } class QQEmail { public bool IsSendEmail(string FromAddress, string ToAddress, string Message, string Pwd) { MailAddress Address = new MailAddress(FromAddress); MailMessage mail = new MailMessage(); mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Subject = "这是" + FromAddress + "发送的一段信息:"; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.Body = Message; mail.IsBodyHtml = true; mail.Priority = System.Net.Mail.MailPriority.Low; mail.To.Add(ToAddress); mail.From = Address; SmtpClient smtp = new SmtpClient("smtp.qq.com", 25);//smtp支持的服务器是smtp.qq.com,服务器端口是25,587也行 smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential(FromAddress, Pwd);//切记这两个数据一定要填对 try { smtp.Send(mail); return true; } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return false; } } }代码案例二:
using log4net;using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Mail;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Text;namespace BLL{ public class emailHandle { private string _serviceType = "SMTP"; private string _host; /// /// 发送者邮箱 /// public string From { get; set; } /// /// 接收者邮箱列表 /// public List To { get; set; } /// /// 抄送者邮箱列表 /// public string[] Cc { get; set; } /// /// 秘抄者邮箱列表 /// public string[] Bcc { get; set; } /// /// 邮件主题 /// public string Subject { get; set; } /// /// 邮件内容 /// public string Body { get; set; } /// /// 是否是HTML格式 /// public bool IsBodyHtml { get; set; } /// /// 附件列表 /// public string[] Attachments { get; set; } /// /// 邮箱服务类型(Pop3,SMTP,IMAP,MAIL等),默认为SMTP /// public string ServiceType { get { return _serviceType; } set { _serviceType = value; } } /// /// 邮箱服务器,如果没有定义邮箱服务器,则根据serviceType和Sender组成邮箱服务器 /// public string Host { get { return _host; } set { _host = value; } } /// /// 邮箱账号(默认为发送者邮箱的账号) /// public string UserName { get; set; } /// /// 邮箱密码(默认为发送者邮箱的密码),默认格式GB2312 /// public string Password { get; set; } /// /// 邮箱优先级 /// public MailPriority MailPriority { get; set; } /// /// 邮件正文编码格式 /// public Encoding Encoding { get; set; } /// /// 构造参数,发送邮件,使用方法备注:公开方法中调用 /// public int Send() { var mailMessage = new MailMessage(); //读取To 接收者邮箱列表 try { if (this.To != null && this.To.Count > 0) { foreach (string to in this.To) { if (string.IsNullOrEmpty(to)) continue; mailMessage.To.Add(new MailAddress(to.Trim())); } } //读取Cc 抄送者邮件地址 if (this.Cc != null && this.Cc.Length > 0) { foreach (var cc in this.Cc) { if (string.IsNullOrEmpty(cc)) continue; mailMessage.CC.Add(new MailAddress(cc.Trim())); } } //读取Attachments 邮件附件 if (this.Attachments != null && this.Attachments.Length > 0) { foreach (var attachment in this.Attachments) { if (string.IsNullOrEmpty(attachment)) continue; mailMessage.Attachments.Add(new Attachment(attachment)); } } //读取Bcc 秘抄人地址 if (this.Bcc != null && this.Bcc.Length > 0) { foreach (var bcc in this.Bcc) { if (string.IsNullOrEmpty(bcc)) continue; mailMessage.Bcc.Add(new MailAddress(bcc.Trim())); } } //读取From 发送人地址 mailMessage.From = new MailAddress(this.From); //邮件标题 Encoding encoding = Encoding.GetEncoding("GB2312"); mailMessage.Subject = this.Subject; //邮件正文是否为HTML格式 mailMessage.IsBodyHtml = this.IsBodyHtml; //邮件正文 mailMessage.Body = this.Body; mailMessage.BodyEncoding = this.Encoding; //邮件优先级 mailMessage.Priority = this.MailPriority; //发送邮件代码实现 var smtpClient = new SmtpClient { Host = this.Host, EnableSsl = true, Credentials = new NetworkCredential(this.UserName, this.Password) };//加这段之前用公司邮箱发送报错:根据验证过程,远程证书无效//加上后解决问题 ServicePointManager.ServerCertificateValidationCallback =delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }; //认证 smtpClient.Send(mailMessage); return 1; } catch (Exception ex) { var loger = LogManager.GetLogger(typeof(emailHandle)); loger.Info(string.Format("发送邮件异常,收信邮箱:{0}", this.To[0]), ex); return -1; } } }} 到此,相信大家对"C#怎么调用QQ_Mail发送邮件"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
邮箱
邮件
服务
服务器
格式
C#
代码
内容
发送者
地址
方法
正文
优先级
密码
接收者
案例
账号
附件
学习
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库实体图软件
服务器最新配置
数据库文件锁定
数据库的物理文件名是什么
确保网络安全的原则
文件服务器有病毒吗
图形数据库neo4j
计算机三级网络技术没时间
数据库主键外键数据集
软件开发到底是什么学历
台州物流软件开发工具
新疆网络安全工作责任制
国家网络安全宣传周活动小结
怎么购买服务器d盘
数据库备份软件技术
数据库 date类型
geo数据库的图能不能直接用
急聘软件开发
逆战哪个服务器人少
武装突袭进不去服务器咋办
成都商城软件开发公司
知网 网络安全治理
列表获取数据库数据
网络技术中的bgp
怎么看目标站点是数据库
网络安全大检查报道
保定勇奔网络技术有限公司
做软件开发的女生
网络安全专科有哪些专业
亳州网络安全考试价格