附件是已经封装好的类和这段代码的结构稍微有点不同-20171222 1550
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="host">服务器地址</param>
/// <param name="port">服务器端口</param>
/// <param name="user">发送者邮箱账号</param>
/// <param name="password">发送者邮箱密码</param>
/// <param name="to">接收者账号</param>
/// <param name="title">标题</param>
/// <param name="body">内容</param>
/// <returns></returns>
public string MailSend(string host, string port, string user, string password, string to, string title, string body)
{
if (host == "" || port == "" || user == "" || user == "" || password == "" || to == "" || title == "" || body == "")
{
return "The request parameters are incorrect";//请求参数不正确
}
int _port = 25; try { _port = Convert.ToInt32(port); } catch { }//如果传递的端口不是数字的将使用默认端口
MailMessage message = new MailMessage();
message.From = new MailAddress(user);//设置发件人
message.To.Add(to);//设置收件人
message.Subject = title;//设置邮件标题
message.Body = body;//设置邮件内容
SmtpClient client = new SmtpClient(host, _port);//设置邮件发送服务器
client.EnableSsl = true;//Ssl状态
client.UseDefaultCredentials = false;//默认凭证
client.DeliveryMethod = SmtpDeliveryMethod.Network;//设置邮件发送方式
client.Credentials = new NetworkCredential(user, password);//设置发送人的邮箱账号和密码
try
{
client.Send(message);//发送邮件
return "Submitted successfully";//提交成功
}
catch (Exception e)
{
return e.Message;//返回错误原因
}
}
以上代码本人已经测试可以使用,如果有什么不懂请提出来!
附件下载:GoMail.cs
版权属于:zgcwkj
本文链接:https://blog.zgcwkj.cn/archives/6.html
转载声明:请注明本文章的标题及内容的出处和声明,谢谢
评论已关闭