📄 mail20.cs
字号:
using System;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace Jessup.Comm
{
#region "邮件发送类"
/// <summary>
/// 邮件发送类
/// </summary>
public class SMTP20
{
private static bool common(string smtpHost, string smtpPort, string smtpTimeout, string smtpUserName, string smtpPassword, string strFrom, string strFromDisplayName, string strTo, string strCc, string strBcc, string strSubject, string strBody, string strEncoding)
{
bool bState = false;
try
{
MailMessage Message = new MailMessage(
new MailAddress(strFrom, strFromDisplayName, Encoding.GetEncoding(strEncoding)),
new MailAddress(strTo));
Message.SubjectEncoding = Encoding.GetEncoding(strEncoding);
Message.Subject = strSubject;
Message.BodyEncoding = Encoding.GetEncoding(strEncoding);
Message.Body = strBody;
SmtpClient smtpClient = new SmtpClient(smtpHost);
smtpClient.Credentials = new NetworkCredential(smtpUserName, smtpPassword);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
smtpClient.Send(Message);
bState = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return bState;
}
public static bool ServiceSendMail(string strTo, string strSubject, string strBody, string strEncoding)
{
return common(strTo, "", strSubject, strBody, strEncoding);
}
/// <summary>
/// 邮件发送(Service邮箱),默认编码为GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strCc"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strEncoding">编码,如果为空,默认为GB2312</param>
/// <returns></returns>
public static bool ServiceSendMail(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
return common(strTo, strCc, strSubject, strBody, strEncoding);
}
/// <summary>
/// 邮件发送默认编码为GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <returns></returns>
public static bool ServiceSendMail(string strTo, string strSubject, string strBody)
{
return common(strTo, "", strSubject, strBody, "");
}
//public static bool ServiceSendMail(string strTo, string strCc, string strSubject, string strBody)
//{
// return common(strTo, strCc, strSubject, strBody, "");
//}
//通用
private static bool common(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
bool bState = false;
try
{
//编码暂硬性规定为GB2312
Encoding encoding = Encoding.GetEncoding(936);
MailMessage Message = new MailMessage(
new MailAddress("yyw_84@yahoo.com.cn", "yyw_84", encoding),
new MailAddress(strTo));
Message.SubjectEncoding = encoding;
Message.Subject = strSubject;
Message.BodyEncoding = encoding;
Message.Body = strBody;
if (strCc != "")
{
Message.CC.Add(new MailAddress(strCc));
}
SmtpClient smtpClient = new SmtpClient("smtp.126.com");
smtpClient.Credentials = new NetworkCredential("yyw_84", "1238");
smtpClient.Timeout = 20000;
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
//smtpClient.UseDefaultCredentials = true;
//smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
smtpClient.Send(Message);
bState = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return bState;
}
}
#endregion
// ------------------------------------------------------------------- //
#region "邮件接收类"
//
// TODO: Add some code here
//
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -