C#发送邮件,生成VCard

#region 发送邮件

/// <summary>
/// 系统发送
/// </summary>
public static bool AutoSendMail(MailAddress toAddress, string bodyHtml, string subject)
{
  bool flag = false;

  try
  {
    MailAddress fromAddress = new MailAddress("sender@meihua.info", "明道", Encoding.GetEncoding("utf-8"));
    MailMessage message = new MailMessage(fromAddress, toAddress);
    message.Subject = subject;
    message.Body = bodyHtml;
    message.IsBodyHtml = true;
    message.BodyEncoding = Encoding.GetEncoding("utf-8");
    message.SubjectEncoding = Encoding.GetEncoding("utf-8");

    SmtpClient client = new SmtpClient("mail.meihua.info");
    client.Credentials = new System.Net.NetworkCredential("sender", "111111");

    client.Send(message);
    flag = true;
  }
  catch
  {

  }

  return flag;
}
/// <summary>
/// 自定义发送
/// </summary>
public static bool SendMail(MailMessage message, bool isBodyHtml)
{
  bool flag = false;

  try
  {
    MailAddress from = new MailAddress(message.From.Address, message.From.DisplayName, Encoding.GetEncoding("utf-8"));
    message.From = from;
    message.IsBodyHtml = isBodyHtml;
    message.BodyEncoding = Encoding.GetEncoding("utf-8");
    message.SubjectEncoding = Encoding.GetEncoding("utf-8");

    SmtpClient client = new SmtpClient("mail.meihua.info");
    client.Credentials = new System.Net.NetworkCredential("sender", "111111");

    client.Send(message);

    flag = true;
  }
  catch
  {

  }

  return flag;
}

#endregion

#region 生成VCard

/// <summary>
/// 生成VCard
/// </summary>
public static string CreateVCard(string fullName, string company, string job, string workPhone, string mobilePhone, string email, string workAddress)
{
  string httpPath = "../tempfiles/";
  string absolutePath = HttpContext.Current.Server.MapPath(httpPath);
  string folderName = Function.CreateRandomDirectoryFolder(absolutePath);
  string fileName = absolutePath + folderName + "\\" + fullName + ".vcf";
  System.IO.StreamWriter stringWrite = new System.IO.StreamWriter(fileName, true, System.Text.Encoding.Default);
  stringWrite.WriteLine("BEGIN:VCARD");
  stringWrite.WriteLine("VERSION:2.1");
  stringWrite.WriteLine("FN:" + fullName);
  stringWrite.WriteLine("ORG;CHARSET=gb2312:" + company);
  stringWrite.WriteLine("TITLE:" + job);
  stringWrite.WriteLine("TEL;WORK;VOICE:" + workPhone);
  stringWrite.WriteLine("TEL;CELL;VOICE:" + mobilePhone);
  stringWrite.WriteLine("EMAIL;PREF;INTERNET:" + email);
  stringWrite.WriteLine("ADR;WORK:;;" + workAddress + ";;;" + string.Empty);
  stringWrite.WriteLine("END:VCARD");
  stringWrite.Close();
  stringWrite.Dispose();
  string httpURL = httpPath + folderName + "\\" + fullName + ".vcf";

  return httpURL;
}

#endregion

posted @ 2011-04-22 09:13  web开发  阅读(1653)  评论(0编辑  收藏  举报