C#发送邮件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using ShopMall.Model;
using System.Diagnostics;
namespace ShopMall.Common
{
public class sendEmail
{
private readonly SmtpClient Client;
public sendEmail(siteconfig site)
{
Client = new SmtpClient
{
Host = "smtp.qq.com",
Port =25,
DeliveryMethod = SmtpDeliveryMethod.Network
};
Client.UseDefaultCredentials = false;
Client.Credentials = new NetworkCredential("1002275364@qq.com","xxxxxxxxxxxxx");
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <returns></returns>
public bool SendMessage(string from, string to, string subject, string body)
{
MailMessage msg = null;
bool isSent = false;
using (msg = new MailMessage(from, to, subject, body))
{
try
{
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
Client.Send(msg);
isSent = true;
}
catch (Exception ex)
{
isSent = false;
Trace.TraceError(ex.Message);
}
return isSent;
}
}
}
}
在调用的时候提示mail from address must be same as authorization user,google后才知道 原来还需要邮箱开启smtp服务才行,于是乎(看下图)
保存后,ok啦