使用SQL数据库信息在C#中发送电子邮件 [英] Send a email in c# using sql database info

查看:153
本文介绍了使用SQL数据库信息在C#中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#,我想发送一封电子邮件,其值将在SQL数据库中获得.我从一个简单的代码开始发送电子邮件,直到工作正常.但我想选择"数据库中的值,然后将该值放入电子邮件中.仅供参考,我不是程序员, 我了解一点,但了解的不是很多...

C#, i want to send a email with value that i will get in a SQL database. I've start with a simple code to send a email, wich is working. But I want to "SELECT" a values in a database and put that values in the email. FYI, I'm not a programmer, I know a little bit, but not that much...

感谢您的帮助.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;
using System.Data.SqlClient;

namespace login
{
    public partial class Oublie : Form
    {
        public Oublie()
        {
            InitializeComponent();
        }

        private void btnEnvoyer_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=SQLSERVER\\SQLCOMMTECH;Initial Catalog=login1;User ID=sa;Password=1234");
            cn.Open();
            SqlCommand cmdObl = new SqlCommand("select password from users where username = '" + txtBoxUsername2.Text + "'", cn);            
            cmdObl.ExecuteReader();
                

            MailMessage mail = new MailMessage("reset@commtech.ca","martin.bourassa@commtech.ca");
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = "relais.videotron.ca";
            mail.Subject = "Password.";
            mail.Body = "this is your password : ";
            client.Send(mail);
        }
    }
}

推荐答案

您需要将查询结果设置为数据集或数据表,然后可以将结果引用为需要并传递到您的电子邮件代码.
You will want to set your query results to a Dataset or DataTable, then you can reference your results as needed and pass along to your email code.


这篇关于使用SQL数据库信息在C#中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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