huakaiyueluo

导航

统计

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("695467782@qq.com", "管理员 邀请参加会议通知");
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("695467782@qq.com", "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("695467782@qq.com", "管理员 邀请参加会议通知");
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("695467782@qq.com", "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; }
}

 


}

posted on   huakaiyueluo  阅读(420)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示