c# 邮件发送
2010-05-12 12:52
211 查看
public enum MailFormat
{
/// <summary>
/// None
/// </summary>
None = 0,
/// <summary>
/// Text
/// </summary>
Text = 1,
/// <summary>
/// Html
/// </summary>
Html = 2
}
public static void SendMail(ArrayList to, string from, string subject, string body)
{
if (to.Count <= 0)
throw new ArgumentException("The adressee of mail is not specified", "to");
if (string.IsNullOrEmpty(subject))
throw new ArgumentException("The subject of mail is not specified", "subject");
if (string.IsNullOrEmpty(body))
throw new ArgumentException("The body of mail is not specified", "body");
string From = from.Length > 0 ? from : "hkwong1003@163.com";
string SMTPServer = "smtp.163.com";
int SMTPPort = 25;
bool SMTPAuthentictation = true;
bool SMTPUseSSL = false;
string SMTPUsername = "ddddd@163.com";
string SMTPPassword = "************";
SendMail(to, From, subject, body, System.Text.Encoding.UTF8,
MailPriority.Normal, MailFormat.Html, SMTPServer, SMTPPort,
SMTPUseSSL, SMTPAuthentictation, SMTPUsername, SMTPPassword);
}
/// <summary>
/// Sends mail using the standard .net 2.0 System.Net.Mail classes
/// </summary>
/// <param name="to">To.</param>
/// <param name="from">From.</param>
/// <param name="subject">The subject.</param>
/// <param name="body">The body.</param>
/// <param name="bodyEncoding">The body encoding.</param>
/// <param name="priority">The priority.</param>
/// <param name="bodyFormat">The body format.</param>
/// <param name="smtpServer">The SMTP server.</param>
/// <param name="smtpPort">The SMTP port.</param>
/// <param name="smtpUseSSL">if set to <c>true</c> [SMTP use SSL].</param>
/// <param name="smtpAuthentication">if set to <c>true</c> [SMTP authentication].</param>
/// <param name="smtpUsername">The SMTP username.</param>
/// <param name="smtpPassword">The SMTP password.</param>
public static void SendMail(ArrayList to, string from, string subject, string body, System.Text.Encoding bodyEncoding, MailPriority priority, MailFormat bodyFormat,
string smtpServer, int smtpPort, bool smtpUseSSL,
bool smtpAuthentication, string smtpUsername, string smtpPassword)
{
if (to.Count <= 0)
throw new ArgumentException("The receiver of the mail message is not specified", "to");
if (string.IsNullOrEmpty(from))
throw new ArgumentException("The sender of the mail message is not specified", "from");
if (string.IsNullOrEmpty(subject))
throw new ArgumentException("The subject of the mail message is not specified", "subject");
if (string.IsNullOrEmpty(body))
throw new ArgumentException("The body of the mail message is not specified", "body");
if (string.IsNullOrEmpty(smtpServer))
throw new ArgumentException("The smtp server is not specified", "smtpServer");
try
{
MailMessage msg = new MailMessage();
foreach (string tos in to)
{
msg.To.Add(new MailAddress(tos.ToString()));
}
msg.From = new MailAddress(from);
msg.Priority = priority;
msg.IsBodyHtml = bodyFormat == MailFormat.Html ? true : false;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Body = body;
msg.Subject = subject;
SmtpClient smtp = new SmtpClient();
smtp.Host = smtpServer;
smtp.Port = smtpPort;
smtp.EnableSsl = smtpUseSSL;
if (smtpAuthentication)
smtp.Credentials = new NetworkCredential(smtpUsername, smtpPassword);
smtp.Send(msg);
}
catch (Exception ex)
{
}
}
{
/// <summary>
/// None
/// </summary>
None = 0,
/// <summary>
/// Text
/// </summary>
Text = 1,
/// <summary>
/// Html
/// </summary>
Html = 2
}
public static void SendMail(ArrayList to, string from, string subject, string body)
{
if (to.Count <= 0)
throw new ArgumentException("The adressee of mail is not specified", "to");
if (string.IsNullOrEmpty(subject))
throw new ArgumentException("The subject of mail is not specified", "subject");
if (string.IsNullOrEmpty(body))
throw new ArgumentException("The body of mail is not specified", "body");
string From = from.Length > 0 ? from : "hkwong1003@163.com";
string SMTPServer = "smtp.163.com";
int SMTPPort = 25;
bool SMTPAuthentictation = true;
bool SMTPUseSSL = false;
string SMTPUsername = "ddddd@163.com";
string SMTPPassword = "************";
SendMail(to, From, subject, body, System.Text.Encoding.UTF8,
MailPriority.Normal, MailFormat.Html, SMTPServer, SMTPPort,
SMTPUseSSL, SMTPAuthentictation, SMTPUsername, SMTPPassword);
}
/// <summary>
/// Sends mail using the standard .net 2.0 System.Net.Mail classes
/// </summary>
/// <param name="to">To.</param>
/// <param name="from">From.</param>
/// <param name="subject">The subject.</param>
/// <param name="body">The body.</param>
/// <param name="bodyEncoding">The body encoding.</param>
/// <param name="priority">The priority.</param>
/// <param name="bodyFormat">The body format.</param>
/// <param name="smtpServer">The SMTP server.</param>
/// <param name="smtpPort">The SMTP port.</param>
/// <param name="smtpUseSSL">if set to <c>true</c> [SMTP use SSL].</param>
/// <param name="smtpAuthentication">if set to <c>true</c> [SMTP authentication].</param>
/// <param name="smtpUsername">The SMTP username.</param>
/// <param name="smtpPassword">The SMTP password.</param>
public static void SendMail(ArrayList to, string from, string subject, string body, System.Text.Encoding bodyEncoding, MailPriority priority, MailFormat bodyFormat,
string smtpServer, int smtpPort, bool smtpUseSSL,
bool smtpAuthentication, string smtpUsername, string smtpPassword)
{
if (to.Count <= 0)
throw new ArgumentException("The receiver of the mail message is not specified", "to");
if (string.IsNullOrEmpty(from))
throw new ArgumentException("The sender of the mail message is not specified", "from");
if (string.IsNullOrEmpty(subject))
throw new ArgumentException("The subject of the mail message is not specified", "subject");
if (string.IsNullOrEmpty(body))
throw new ArgumentException("The body of the mail message is not specified", "body");
if (string.IsNullOrEmpty(smtpServer))
throw new ArgumentException("The smtp server is not specified", "smtpServer");
try
{
MailMessage msg = new MailMessage();
foreach (string tos in to)
{
msg.To.Add(new MailAddress(tos.ToString()));
}
msg.From = new MailAddress(from);
msg.Priority = priority;
msg.IsBodyHtml = bodyFormat == MailFormat.Html ? true : false;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Body = body;
msg.Subject = subject;
SmtpClient smtp = new SmtpClient();
smtp.Host = smtpServer;
smtp.Port = smtpPort;
smtp.EnableSsl = smtpUseSSL;
if (smtpAuthentication)
smtp.Credentials = new NetworkCredential(smtpUsername, smtpPassword);
smtp.Send(msg);
}
catch (Exception ex)
{
}
}
相关文章推荐
- C#中如何利用Gmail账户发送邮件
- c#.net smtp 发送邮件
- C#发送邮件
- C# 发送邮件
- c#.net 下的邮件发送(含附件)
- 通过C#发送自定义的html格式邮件
- C#编程实现发送邮件的方法(可添加附件)
- 20101015 学习记录2:c# 发送邮件
- C# - MailSender 邮件发送组件源代码 (支持ESMTP, 附件)
- C#邮件发送代码实现(MailSender.cs)
- c#发送邮件之System.Net.Mail方法
- C# 发送邮件
- C#System.Net.Mail采用简单邮件传输协议发送邮件
- 用C#.net发送邮件
- C#邮件发送
- c# 邮件发送 发送人带昵称
- C#使用smtp发送邮件
- C#简单邮件发送
- C#封装发送邮件SendMail函数
- C#通过Outlook发送邮件