c#使用SMTPClient发送邮箱

  • 来源:新网
  • 更新日期:2018-03-13

摘要:一:问题在于??? 1:163邮箱,qq邮箱默认未开通第三方邮箱,需要自己去开通 2:开通之后会获得授权码,不可以使用原来的密码,密码改为授权码 二:解决方法 1:在qq邮箱的设置中找到如图,开启smpt服务 2:设置UseDefaultCredentials 为false  ,创建,smtpClient.Credentials = new Network

一:问题在于???

1:163邮箱,qq邮箱默认未开通第三方邮箱,需要自己去开通

2:开通之后会获得授权码,不可以使用原来的密码,密码改为授权码

二:解决方法

1:在qq邮箱的设置中找到如图,开启smpt服务

2:设置UseDefaultCredentials 为false ,创建,smtpClient.Credentials = new NetworkCredential(emailSetting.Username, Password(这里是授权码));

三:代码如下

public class EmailSettings
{
public string MailToAddress = "7126252@qq.com";
public string MailFromAddress = "9067447@qq.com";
public bool UseSsl = true;
public string Username = "9067447@qq.com";
public string Password = "zdyzwmlndsdsdsad"; //要使用qq的 或者163邮箱的授权码
public string ServerName = "smtp.qq.com";
public int ServerPort = 25;
public bool WriteAsFile = false; //这里要false
public string FileLocation = "";
}
public class EmailOrderProcess:IOrderProcess
{
private EmailSettings emailSetting;
public EmailOrderProcess(EmailSettings setting)
{
emailSetting = setting;
}
public void ProcessOrder(Cart cart,ShoppingDetials shoppingDetials)
{
using(var smtpClient=new SmtpClient())
{
smtpClient.EnableSsl = emailSetting.UseSsl;
smtpClient.Host = emailSetting.ServerName;//smtp.qq.com
smtpClient.Port = emailSetting.ServerPort;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(emailSetting.Username, emailSetting.Password);
if (emailSetting.WriteAsFile) { }
StringBuilder body = new StringBuilder().Append("A new order has been submitted");
var mailMessage = new MailMessage(emailSetting.MailFromAddress, emailSetting.MailToAddress, "A new order", body.ToString());
smtpClient.Send(mailMessage);
}
}
}