第一种发送方式
```
public bool SendEmail(string UserEmail, string randcode)
{
string SendEmail = "邮箱账号";
string SendPwd = "邮箱密码/验证方式,具体查看下图";
SmtpClient _smtpClient = new SmtpClient();
_smtpClient.EnableSsl = true;
_smtpClient.UseDefaultCredentials = false;
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtpClient.Host = "smtp.qq.com";
_smtpClient.Port = 587;
_smtpClient.Credentials = new System.Net.NetworkCredential(SendEmail, SendPwd);
//密码不是QQ密码,是qq账户设置里面的POP3/SMTP服务生成的key
MailMessage _mailMessage = new MailMessage(SendEmail, UserEmail);
_mailMessage.Subject = "";//主题
_mailMessage.Body = "";//邮件正文
_mailMessage.BodyEncoding = Encoding.Default;//正文编码
_mailMessage.IsBodyHtml = true;//设置为HTML格式
_mailMessage.Priority = MailPriority.High;//优先级
try
{
_smtpClient.Send(_mailMessage);
Console.WriteLine("发送成功");
return true;
}
catch (Exception e)
{
Console.WriteLine("发送失败");
throw e;
}
}
```
第二种方式
需要引用CDO组件发送,Interop.CDO和Interop.ADODB
但是记住 CDO引用嵌入式必须修改为false否则会提示错误。
-----------------------------------------------------------------------------------------
public bool SendEmail(string UserEmail, string randcode)
{
bool result = false;
try
{
Message oMsg = new Message();
//Configuration conf = new ConfigurationClass();
oMsg.Configuration.Fields[CdoConfiguration.cdoSendUsingMethod].Value = CdoSendUsing.cdoSendUsingPort;
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPAuthenticate].Value = CdoProtocolsAuthentication.cdoBasic;
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPUseSSL].Value = true;
//QQ企业邮箱
/*
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServer].Value = "smtp.exmail.qq.com";//必填,而且要真实可用
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServerPort].Value = 465;//邮箱端口
*/
//网易邮箱
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServer].Value = "smtphm.qiye.163.com";//必填,而且要真实可用
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServerPort].Value = 994;//邮箱端口
string SendEmail = "邮箱账号";
string SendPwd = "邮箱密码";
oMsg.Configuration.Fields[CdoConfiguration.cdoSendEmailAddress].Value = SendEmail;//发送者邮箱
oMsg.Configuration.Fields[CdoConfiguration.cdoSendUserName].Value = SendEmail;//邮箱发送者名称
oMsg.Configuration.Fields[CdoConfiguration.cdoSendPassword].Value = SendPwd; //邮箱发送者密码,必须真实
oMsg.Configuration.Fields.Update();
//oMsg.Configuration = conf;
oMsg.TextBody = "";//邮件正文
oMsg.Subject = "";//主题
oMsg.From = SendEmail;//发送者
oMsg.To = UserEmail;//接收者
try
{
oMsg.Send();//发送
result = true;
}
catch
{
result = false;
}
}
catch
{
result = false;
}
return result;
}