从表格而不是从表格发送邮件 - 新的C#aspnet [英] Sending mail form a table instead from a form - new to C# aspnet

查看:81
本文介绍了从表格而不是从表格发送邮件 - 新的C#aspnet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从表单中获取正常工作的代码段。



需要从带有注释字段的中应用它在sql server中



表:Mail.dbo



姓名电子邮件备忘录
----------------------------------------
eva aaa@gmail.com trialeva
sam bbb@gmail.com trialam
jon ccc@gmail.com trialjon
amy ddd@gmail.com trialamy





 EmailSubj =  试用消息 ; 
EmailMsg = Convert.ToString(txt_Trial_memo.Text);

string [] emails = new string [] {txt_Email.Text,txt_Zemail.Text,txt_Bemail.Text};
foreach var 电子邮件 in 电子邮件)
{
if (!string.IsNullOrWhiteSpace(email))
SendEmail(email,EmailSubj,EmailMsg);
}





请协助建立循环



或提供任何网络参考协助。





谢谢



我尝试过:



已在互联网上查找代码及其他语言的早期代码

解决方案

所以你要问的是如何使用c#连接到sql server并访问数据。我对您的项目/您打算如何访问数据一无所知。有许多不同的方法来访问sql server(普通的ado.net,实体框架,nhibernate等等)所以我假设你正在使用实体框架,因为这是回答这个问题最简单的方法。 />


你需要做的是首先研究你正在尝试做什么,因为这是C#中最基本的事情。对我来说,访问数据比Hello World高出一步,这是你应该熟悉的事情。



所以要获取数据,你会做类似的事情。< br $> b $ b

  //   I将IsSent添加到您的Mail表中,以过滤掉已发送的邮件。您也可以在发送邮件后从表中删除这些项目。 
var mailToSend = context.Mail.Where(m = < span class =code-keyword>> !m.IsSent);

foreach var mail in mailToSend)
{
SendEmail(mail.email, 你要' t有一个主题列,静态文本/传入变量这里,mail.memo);
}





mailToSend变量是数据库中项目的集合。您需要决定如何访问数据。有关使用实体框架(以及一般C#到sql连接)的更多信息,请参阅以下链接:



connect to sql server c - Google Search [ ^ ]



使用实体框架C# - Google搜索 [ ^ ]



入门(实体框架) [ ^ ]



Entity Framework 6入门Code First使用MVC 5 | ASP.NET站点 [ ^ ]



绝对初学者实体框架简介 [ ^ ]



实体框架教程 [ ^ ]



更新:



在关闭更改中,您要使用Sql Server DB Mail ...这里有一些链接。你需要评估你想要做什么,看看哪个选项最适合你的需要。



sp_send_dbmail(Transact-SQL) [ ^ ]



配置数据库邮件 [ ^ ]



SQL数据库邮件 - 从SQL Server发送电子邮件 - Windows Azure虚拟机上的Microsoft SQL Server [ ^


Have the undernoted code segment which is working ok from a form.

Need to apply it from a table with the undernoted fields in sql server

Table: Mail.dbo

name        email               memo
----------------------------------------
eva         aaa@gmail.com     trialeva
sam         bbb@gmail.com     trialsam
jon         ccc@gmail.com     trialjon
amy         ddd@gmail.com     trialamy



EmailSubj = "Trial Message";
EmailMsg = Convert.ToString(txt_Trial_memo.Text);

string[] emails = new string[] { txt_Email.Text, txt_Zemail.Text, txt_Bemail.Text };
foreach (var email in emails)
{
    if (!string.IsNullOrWhiteSpace(email))
        SendEmail(email, EmailSubj, EmailMsg);
}



Please assist in building the loop

or offer any web refereneces to assist.


Thanks

What I have tried:

Reviewed codes on the internet and earlier codes in another language

解决方案

So what you are asking is really how to connect to sql server using c# and access the data. I know nothing about your project/how you intend to access the data. There are many different ways to access sql server (normal ado.net, entity framework, nhibernate...etc.) so I'm going to assume you are using entity framework as that is the easiest way to answer this question.

What you need to do is first, research what it is you are trying to do as this is an extremely basic thing to do in C#. Accessing data, to me, is one step above Hello World and is something you should be familiar with already.

So to get the data you would do something like.

// I added IsSent to your Mail table as a way to filter out mail already sent. You could also just delete these items from your table after mail has been sent.
var mailToSend = context.Mail.Where(m=> !m.IsSent);

foreach(var mail in mailToSend)
{
     SendEmail(mail.email, "You don't have a subject column, static text/passed in variable here", mail.memo);
}



the mailToSend variable is a collection of items from your database. You need to decide how you intend on accessing your data. For more information on using entity framework (and general C# to sql connections) see below links:

connect to sql server c - Google Search[^]

use entity framework C# - Google Search[^]

Getting Started (Entity Framework)[^]

Getting Started with Entity Framework 6 Code First using MVC 5 | The ASP.NET Site[^]

An Introduction to Entity Framework for Absolute Beginners[^]

Entity Framework Tutorial[^]

UPDATE:

In the off change you want to use Sql Server DB Mail...here are some links. You need to evaluate what it is you are trying to do and see which option best fits your needs.

sp_send_dbmail (Transact-SQL)[^]

Configure Database Mail[^]

SQL Database Mail – Send Emails from SQL Server – Microsoft SQL Server on Windows Azure Virtual Machines[^]


这篇关于从表格而不是从表格发送邮件 - 新的C#aspnet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆