c# 发邮件功能

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using TeaDapper.CodeGen.Base;
using TeaDapper.CodeGen.Entities;
using TeaDapper.CodeGen.Tools;
using pt.Model;
using TeaDapper.CodeGen.CRUD;
using System.Collections.Specialized;
using System.Net.Mail;
using System.Net.Mime;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.IO;
using System.Threading;
using System.Globalization;
namespace TeaDapper.CodeGen.Entities
{
/// <summary>
/// User custom methods for MT_ContentDap
/// </summary>
public class MT_Email
{
//流程的发送邮件
public string SentEmail(string f_guid)
{
//根据id得到此条会议的具体信息
MT_Content mt = new MT_ContentDap().GetByf_GUID(f_guid);
//会议的内容
string content = "您好,请您准时参加会议室" + mt.RoomName + ",会议主题:" + mt.Title + ",会议时间:" + mt.MtTime + "," + mt.StartTime.Substring(16, 8) + "--" + mt.EndTime.Substring(16, 8) + "的会议。请准时参加!";//内容
string title = mt.Title;//标题
string ids = mt.PeopleId;//参加人
string upload = "";//附件信息

/******************************发邮件****************************/
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("[email protected]", "管理员 邀请参加会议通知");
message.Body = content;
message.Subject = "会议邀请";
/***********分割教师id字符串,关联教师基本信息表取出教师email地址值*******/
ids = ids.Replace("$$", "$");
string[] PeopleIds = ids.Split(new char[] { '$' });
string sendIds = "";
foreach (string id in PeopleIds)
{
if (id.Trim() != "")
{
sendIds += "'" + id + "',";
}
}
sendIds = sendIds.Substring(0, sendIds.Length - 1);
string sql = SqlConfigHelper.GetSql("MT_Email.GetEmails");
List<T_teacher> ts = new T_teacherDap().Query<T_teacher>(string.Format(sql, sendIds)).ToList();
if (ts.Count < 1)
{
return "";
//return "{\"Success\":false, \"Message\":\"您选中的参会人员没有填写邮件地址。\", \"Data\":\"-1\"}";
}
foreach (T_teacher info in ts)
{
if (info.Email != null && info.Email.Trim() != "")
{
message.To.Add(info.Email);
}
}

message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.qq.com");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "1010197091confid");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Send(message);
client.Dispose();
message.Dispose();

return Constants.Success;
}


//js发送邮件
public string SendEmail(EmailInfo email)
{
string content = email.f_Content;
string title = email.f_Title;
string ids = email.f_PeopleId;
string upload = email.strex3;
/******************************发邮件****************************/
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("[email protected]", "管理员 邀请参加会议通知");
message.Body = email.f_Content;
message.Subject = "会议邀请";
/***********分割教师id字符串,关联教师基本信息表取出教师email地址值*******/
ids = ids.Replace("$$", "$");
string[] PeopleIds = ids.Split(new char[] { '$' });
string sendIds = "";
foreach (string id in PeopleIds)
{
if (id.Trim() != "")
{
sendIds += "'" + id + "',";
}
}
sendIds = sendIds.Substring(0, sendIds.Length - 1);
string sql = SqlConfigHelper.GetSql("MT_Email.GetEmails");
List<T_teacher> ts = new T_teacherDap().Query<T_teacher>(string.Format(sql, sendIds)).ToList();
if (ts.Count < 1)
{
return "{\"Success\":false, \"Message\":\"您选中的参会人员没有填写邮件地址。\", \"Data\":\"-1\"}";
}
foreach (T_teacher info in ts)
{
if (info.Email != null && info.Email.Trim() != "")
{
message.To.Add(info.Email);
}
}
if (!String.IsNullOrEmpty(upload))
{

List<Add_AddFile> eylist = Utils.ParseListFromJson<Add_AddFile>(upload).ToList();
foreach (Add_AddFile file in eylist)
{
string Path = HttpContext.Current.Server.MapPath(file.filepath);
Attachment myAttachment = new Attachment(Path, MediaTypeNames.Application.Octet);
myAttachment.Name = file.filesname;
message.Attachments.Add(myAttachment);
}

}
/******************************end*****************************************/
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.qq.com");
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "1010197091confid");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Send(message);
client.Dispose();
message.Dispose();
/****************************end*********************************************/
// 删除服务器中的附件
if (!String.IsNullOrEmpty(upload))
{
List<Add_AddFile> eylist = Utils.ParseListFromJson<Add_AddFile>(upload).ToList();
foreach (Add_AddFile file in eylist)
{
string Path = HttpContext.Current.Server.MapPath(file.filepath);

if (File.Exists(Path))
{
File.Delete(Path);
}
}
}
return Constants.Success;
}
}

public class EmailInfo
{
public string strex3 { get; set; }
public string f_PeopleId { get; set; }
public string f_Title { get; set; }
public string f_Content { get; set; }
}

 


}

你可能感兴趣的:(C#)