快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

c#使用SMTPClient发送邮箱

一:问题在于???

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的王益网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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);
}
}
}


标题名称:c#使用SMTPClient发送邮箱
网站网址:http://6mz.cn/article/cgigpj.html