如果使用公司内部服务器发送邮件,需要搭建服务器
https://www.landui.com/help/show-1063.html
从数据库读取相关信息
CREATE TABLE [dbo].[Config](
[Category] [nvarchar](100) NOT NULL,
[Type] [nvarchar](200) NOT NULL,
[Value] [nvarchar](max) NOT NULL,
[Memo] [nvarchar](50) NULL,
CONSTRAINT [PK_Config_1] PRIMARY KEY CLUSTERED
(
[Category] ASC,
[Type] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
/// <summary>
/// 发送邮件,附件方面代码未添加
/// </summary>
/// <param name="mailto">目标邮箱 分号隔开可填写多个邮箱</param>
/// <param name="body">发送内容</param>
/// <returns></returns>
public static string SendMail(string mailto,string mailcc, string maildesc, string subject, string body)
{
try
{
MailMessage mailMsg = new MailMessage();
maildesc = maildesc.Trim() == "" ? Config.GetValue("Mail", "Desc") : maildesc.Trim();
//发送使用的邮箱
mailMsg.From = new MailAddress(Config.GetValue("Mail", "Mail"), maildesc);
foreach (string s in mailto.Split(';'))
{
if (s.Trim().Contains("@"))
{
mailMsg.To.Add(new MailAddress(s.Trim()));
}
}
foreach (string s in mailcc.Split(';'))
{
if (s.Trim().Contains("@"))
{
mailMsg.CC.Add(new MailAddress(s.Trim()));
}
}
mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.Priority = MailPriority.High;
//smtp服务器
SmtpClient spClient = new SmtpClient(Config.GetValue("Mail", "Server"));
//验证ID密码
spClient.Credentials = new System.Net.NetworkCredential(Config.GetValue("Mail", "UserName"), Config.GetValue("Mail", "Password"));
spClient.Send(mailMsg);
return "";
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
SendMail("1203765096@qq.com", "", "test", "c#邮件发送", content1.Text);
}
/// <summary>
/// 发送邮件,附件方面代码未添加
/// </summary>
/// <param name="mailto">目标邮箱 分号隔开可填写多个邮箱</param>
/// <param name="body">发送内容</param>
/// <returns></returns>
public static string SendMail(string mailto, string mailcc, string maildesc, string subject, string body)
{
try
{
MailMessage mailMsg = new MailMessage();
//maildesc = maildesc.Trim() == "" ? Config.GetValue("Mail", "Desc") : maildesc.Trim();
//发送使用的邮箱
mailMsg.From = new MailAddress("1203765096@qq.com", maildesc);
foreach (string s in mailto.Split(';'))
{
if (s.Trim().Contains("@"))
{
mailMsg.To.Add(new MailAddress(s.Trim()));
}
}
foreach (string s in mailcc.Split(';'))
{
if (s.Trim().Contains("@"))
{
mailMsg.CC.Add(new MailAddress(s.Trim()));
}
}
mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.Priority = MailPriority.High;
//smtp服务器
SmtpClient spClient = new SmtpClient("smtp.laibao.com.cn");
//验证ID密码
spClient.Credentials = new System.Net.NetworkCredential("SCP@laibao.com.cn", "SCPscp123");
spClient.Send(mailMsg);
MessageBox.Show("发送成功!");
return "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return ex.Message;
}
finally
{
}
}