Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.NETC中使用Web服务从数据库向多个收据发送邮件#_C#_Asp.net_Sql Server_Web Services - Fatal编程技术网

C# 在asp.NETC中使用Web服务从数据库向多个收据发送邮件#

C# 在asp.NETC中使用Web服务从数据库向多个收据发送邮件#,c#,asp.net,sql-server,web-services,C#,Asp.net,Sql Server,Web Services,我有一个sql server表,在其中插入邮件ID、主题和邮件正文。 我还在另一个页面的gridview中显示了邮件详细信息。每行都有一个复选框。在这里,当用户选中相应的复选框时,我想将这些邮件发送到相应的电子邮件ID。实际问题是我想创建一个web服务来发送邮件。请帮助我创建一个web服务。我试过一些方法。下面给出了创建web服务的最后一段代码 using System; using System.Collections.Generic; using System.Linq; using Sys

我有一个sql server表,在其中插入邮件ID、主题和邮件正文。 我还在另一个页面的gridview中显示了邮件详细信息。每行都有一个复选框。在这里,当用户选中相应的复选框时,我想将这些邮件发送到相应的电子邮件ID。实际问题是我想创建一个web服务来发送邮件。请帮助我创建一个web服务。我试过一些方法。下面给出了创建web服务的最后一段代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Mail;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MailService : System.Web.Services.WebService
{
    public MailService()
    {
        //InitializeComponent(); 
    }
    [WebMethod]
    public bool SendMail(string toAddress, string subject, string body)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("proc_MailData", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.Fill(ds);
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = "john@averla.in";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {                
                string subjct = ds.Tables[0].Rows[i]["MSubject"].ToString();
                string mail = ds.Tables[0].Rows[i]["MailID"].ToString();
                string bdy = ds.Tables[0].Rows[i]["Body"].ToString();
                msg.To = mail;
                msg.Body = bdy;
                msg.Subject = subjct;
            }
            SmtpMail.SmtpServer = "maildemo.averla.in";
            SmtpMail.Send(msg);
            return true;
        }
        catch (Exception exp)
        {
            return false;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
使用System.Data.SqlClient;
使用系统数据;
使用系统配置;
使用System.Web.Mail;
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
公共类邮件服务:System.Web.Services.WebService
{
公共邮件服务()
{
//初始化组件();
}
[网络方法]
public bool SendMail(字符串到地址、字符串主题、字符串正文)
{
SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“ApplicationServices”].ToString());
数据集ds=新数据集();
SqlDataAdapter da=新的SqlDataAdapter(“proc_MailData”,con);
da.SelectCommand.CommandType=CommandType.StoredProcess;
da.填充(ds);
尝试
{
MailMessage msg=新的MailMessage();
msg.From=”john@averla.in";
对于(int i=0;i
1-使用语句添加

using System.Net.Mail;
2-执行smtp客户端

SmtpClient client = new SmtpClient();
client.Credentials=new NetworkCredential(username,password);
client.Send(message);
3-编译并发布web服务后,您应该能够导航到url,它将向您显示像图像这样的操作

然后在你的应用程序中,你可以像我之前在评论中所说的那样添加引用

这里有一些可能对您有所帮助的链接

希望对你有帮助


关于

如果您试图向多个用户发送电子邮件,则需要调用SmtpMail。发送内部循环。@MairajAhmad SmtpMail我使用过并调用过,但没有工作。请参考它您遇到了什么错误?我遇到了类似这样的错误“在应用程序级别之外使用注册为allowDefinition='MachineToApplication'的节是错误的。此错误可能是由于未将虚拟目录配置为IIS中的应用程序造成的。“此错误与代码无关。我理解了逻辑并得到了结果..谢谢