C#调用QQ_Mail发送邮件实例代码两例
作者:農碼一生 发布时间:2023-11-10 17:27:05
标签: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;
/// <summary>
/// 发送者邮箱
/// </summary>
public string From { get; set; }
/// <summary>
/// 接收者邮箱列表
/// </summary>
public List<string> To { get; set; }
/// <summary>
/// 抄送者邮箱列表
/// </summary>
public string[] Cc { get; set; }
/// <summary>
/// 秘抄者邮箱列表
/// </summary>
public string[] Bcc { get; set; }
/// <summary>
/// 邮件主题
/// </summary>
public string Subject { get; set; }
/// <summary>
/// 邮件内容
/// </summary>
public string Body { get; set; }
/// <summary>
/// 是否是HTML格式
/// </summary>
public bool IsBodyHtml { get; set; }
/// <summary>
/// 附件列表
/// </summary>
public string[] Attachments { get; set; }
/// <summary>
/// 邮箱服务类型(Pop3,SMTP,IMAP,MAIL等),默认为SMTP
/// </summary>
public string ServiceType
{
get { return _serviceType; }
set { _serviceType = value; }
}
/// <summary>
/// 邮箱服务器,如果没有定义邮箱服务器,则根据serviceType和Sender组成邮箱服务器
/// </summary>
public string Host
{
get { return _host; }
set { _host = value; }
}
/// <summary>
/// 邮箱账号(默认为发送者邮箱的账号)
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 邮箱密码(默认为发送者邮箱的密码),默认格式GB2312
/// </summary>
public string Password { get; set; }
/// <summary>
/// 邮箱优先级
/// </summary>
public MailPriority MailPriority { get; set; }
/// <summary>
/// 邮件正文编码格式
/// </summary>
public Encoding Encoding { get; set; }
/// <summary>
/// 构造参数,发送邮件,使用方法备注:公开方法中调用
/// </summary>
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;
}
}
}
}
来源:https://www.cnblogs.com/wml-it/p/14814297.html
0
投稿
猜你喜欢
- 在使用spring提供的JpaTemplate进行查询时,如果数据量超过100 条,查询效率就会明显降低。由于开始时使用JPA内部的双向关联
- 参考链接:狂神的Swagger笔记号称世界上最流行的API框架Restful Api 文档在线自动生成器 => API 文档 与API
- using Word;下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:(例子中代码有些涉及数据信息部分被省略,重要是介绍
- 应用情景:很多标准的方法都是利用Object.Equals方法来做对比的,例如LIst.Remove假设 某些情景下我们希望引用类型判断&a
- 一、常见游戏规则从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要
- 抛出问题:Long a = 4l;Long b = 4l;a == b //trueLong a = 128l;Long b = 128l;
- 方法一第一步:添加引用using System.Printing;第二步:代码public static List<string>
- 楼主大菜鸟一只,第一次写技术博客,如果有概念错误或代码不规范的地方,还请各位多多批评指正。话不多说,来看题:前一阵子开发了一个用户控件,里面
- 1.概述当Java 8发布时,其中最令人兴奋的特性之一就是 Lambda 表达式。Lambda 表达式是一种轻量级、简洁和高效的函数式编程方
- 概述动态SQL:SQL语句会随着用户输入或外部条件的变化而变化 。例如:我们在做多条件查询的时候,编写SQL语句的查询操作,我们并不知道用户
- 1.概述在本快速教程中,我们将学习如何设置Spring Security LDAP。在我们开始之前,了解一下LDAP是什么? - 它代表轻量
- 在进行C#应用程序开发的过程中,经常需要多窗体之间进行数据通信,本文举几个例子,把几种常用的通信方式总结一下,窗体界面如下图所示: 
- 本文主要给大家介绍java的InputStream 流的使用。(1)FileInputstream: 子类,读取数据的通道使用步骤:1.获取
- 1,验证传入路径是否为正确的路径名(Windows系统,其他系统未使用)Java代码 // 验证字符串是否为正确路径名的正则表达式 
- 面向对象编程(Object Oriented Programming)有三大特性:封装、继承、多态。在这里,和大家一起加深对三者的理解。封装
- Java程序设计 图形用户界面 【十】复选框复选框JCheckBoxJCheckBox类方法作用public JCheckBox(Icon
- 一、前导知识串行口是计算机的标准接口,现在的PC机(个人电脑)一般至少有两个串行口COM1和COM2。串行口应用广泛,在数据通信、计算机网络
- 前言在 C# 编程中,管道式编程(Pipeline Style programming)其实存在已久,最明显的就是我们经常使用的 LINQ。
- 本文实例为大家分享了java实现简单的猜数字的具体代码,供大家参考,具体内容如下题目描述:猜数字(又称 Bulls and Cows )是一
- 目录一、ThreadLocal简介二、ThreadLocal简单使用三、ThreadLocal的实现原理1、set方法源码2、get方法源码