using System;
using System.IO;
//杨春
//2008-6-8
/// <summary>
/// Mail 的摘要说明
/// </summary>
public class MyMail
{
string publicMail = System.Configuration.ConfigurationSettings.AppSettings["PublicMail"].ToString();
string pubicmMailPassword = System.Configuration.ConfigurationSettings.AppSettings["PublicMailPasword"].ToString();
string mailUserName = System.Configuration.ConfigurationSettings.AppSettings["MailUserName"].ToString();
public MyMail()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 发邮件
/// </summary>
/// <param name="reciver">收件人</param>
/// <param name="title">主题</param>
/// <param name="content">内容</param>
/// <param name="attpath">附件</param>
/// <returns>是否成功</returns>
public bool SendMail(string reciver, string title, string content, string attpath)
{
try
{
if ("" == attpath)
{
return SendMailByJmail(reciver, title, content);
}
else
{
if (File.Exists(attpath) == true)
{
return SendMailByJmail(reciver, title, content, attpath);
}
else
{
return SendMailByJmail(reciver, title, content);
}
}
}
catch (Exception ex)
{
return false;
Console.Write(ex.Message);
}
}
/// <summary>
/// 发送无附件邮件
/// </summary>
/// <param name="reciver">收件人</param>
/// <param name="title">主题</param>
/// <param name="content">内容</param>
/// <returns>是否成功</returns>
///
private bool SendMailByJmail(string reciver,string title,string content)
{
myJmail.MessageClass JmailMessage = new myJmail.MessageClass();
JmailMessage.Charset = "GB2312";
JmailMessage.Encoding = "BASE64";
JmailMessage.ContentType = "text/html";
JmailMessage.ISOEncodeHeaders = false;
JmailMessage.Priority = Convert.ToByte(1);
JmailMessage.From = publicMail;
JmailMessage.FromName = mailUserName;
JmailMessage.Subject = title;
JmailMessage.MailServerUserName = GetLoginNameByEmail(publicMail);
JmailMessage.MailServerPassWord = pubicmMailPassword;
JmailMessage.AddRecipient(reciver, "", "");
JmailMessage.Body = content;
if (JmailMessage.Send(GetSmtpSererByEmail(publicMail), false))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 发送带附件的邮件
/// </summary>
/// <param name="reciver">收件人</param>
/// <param name="title">主题</param>
/// <param name="content">内容</param>
/// <param name="attpath">附件路径</param>
/// <returns>是否成功</returns>
private bool SendMailByJmail(string reciver, string title, string content, string attpath)
{
myJmail.MessageClass JmailMessage = new myJmail.MessageClass();
JmailMessage.Charset = "GB2312";
JmailMessage.Encoding = "BASE64";
JmailMessage.ContentType = "text/html";
JmailMessage.ISOEncodeHeaders = false;
JmailMessage.Priority = Convert.ToByte(1);
JmailMessage.From = publicMail;
JmailMessage.FromName = mailUserName;
JmailMessage.Subject = title;
JmailMessage.MailServerUserName = GetLoginNameByEmail(publicMail);
JmailMessage.MailServerPassWord = pubicmMailPassword;
JmailMessage.AddRecipient(reciver, "", "");
JmailMessage.AddAttachment(attpath, true, attpath.Substring(attpath.LastIndexOf(".") + 1, 3));
JmailMessage.Body = content;
if (JmailMessage.Send(GetSmtpSererByEmail(publicMail), false))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 根据Email地址得到登录名
/// </summary>
/// <param name="email">Email</param>
/// <returns>登录名</returns>
private string GetLoginNameByEmail(string email)
{
string loginName = email.Substring(0,email.IndexOf("@"));
return loginName;
}
/// <summary>
/// 根据Email得到Smtp服务器地址
/// </summary>
/// <param name="email">Email</param>
/// <returns>stmp服务器地址</returns>
private string GetSmtpSererByEmail(string email)
{
int length = email.Length;
string smtpServer = "smtp." + email.Substring(email.IndexOf("@")+1, length - email.LastIndexOf("@")-1);
return smtpServer;
}
}