用C#调用发送邮箱代码之前需要邮箱开通SMTP/POP3及设置授权码,开通及获取方法如下:
1、打开邮箱,登录邮箱,进入设置-》帐户
2、在“帐户”设置中,找到服务设置项,进行设置,如下:
开启POP3/SMTP服务器,验证密保
3、用正确的手机好发送正确的验证内容到指定的号码,成功获取授权码
4、C#代码发送源码如下:
/// <summary>
/// 向指定邮箱发送信息
/// </summary>
/// <param name="recipientMail">接收者邮箱</param>
/// <param name="subject">邮件主题</param>
/// <param name="mailContent">邮件内容</param>
public void SendInfoToMail(string recipientMail, string subject, string mailContent)
{
string SendMailbox = "xxxx@163.com"; //SendMailbox:发送信息的邮箱
string SMIPServiceCode = "XXXXXXX";//SMIPServiceCode:邮箱smtp服务密码,确保邮箱已经开启了SMTP服务,开启后会给出一串编码就是smtp服务密码,后台填入编码 SmtpClient client = new SmtpClient("smtp.163.com",25); //邮箱服务器类型,我这里用的是163邮箱:smtp.163.com //Port:邮箱服务器端口 qq端口 25
MailMessage msg = new MailMessage(SendMailbox, recipientMail, subject, mailContent);
client.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo =
new System.Net.NetworkCredential(SendMailbox, SMIPServiceCode);
client.Credentials = basicAuthenticationInfo;
client.EnableSsl = true;
client.Send(msg);
}