第一种方法:
转自:https://blog.csdn.net/u013571196/article/details/78376343
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;
}
}
Foxmail设置:
第二种方法:
需要引用CDO组件发送,Interop.CDO和Interop.ADODB
但是记住 CDO引用嵌入式必须修改为false否则会提示错误。
邮箱说明:
1. 其它QQ、阿里云等等邮箱 配置各自独立的代理服务器和端口号+自己邮箱账号密码即可;
2.其它类型发送协议建议参考官网配置;http://mailhelp.mxhichina.com/smartmail/detail.vm?knoId=5872033
ResultModel SendEmial(string EmailTitle, string EmialContent, string Email, string MsgCode, bool Iseg)
{
var result = new ResultModel() { State = false, Msg = Iseg ? "Send failed!" : "发送失败!" };
try
{
Message oMsg = new Message();
oMsg.Subject = EmailTitle;
oMsg.HTMLBody = EmialContent;
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;//邮箱端口
*/
//阿里邮箱服务器
/*
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServer].Value = "smtp.mxhichina.com";//必填,而且要真实可用
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServerPort].Value = 465;//邮箱端口
*/
//GoDaddy邮箱
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServer].Value = "smtpout.secureserver.net";//必填,而且要真实可用
oMsg.Configuration.Fields[CdoConfiguration.cdoSMTPServerPort].Value = 465;//邮箱端口
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.From = SendEmail;//发送者
oMsg.To = Email;//接收者
oMsg.Send();//邮箱发送
result.State = true;
result.Msg = Iseg ? "The mail was sent successfully! Valid for 180S" : "邮件发送成功!有效期为180S";
}
catch (Exception ex)
{
result.State = false;
result.Msg = PackageErrorMsg.IGetErrorMsg(new RefactoryErrorMsg(), ex);
}
return result;
}