您好,向所有用户发送邮件时出现问题. [英] hello, problem with sending mail to all users.

查看:68
本文介绍了您好,向所有用户发送邮件时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我是一名学生,并且正在使用asp.net进行最终项目(网站).
我有一个表名为用户"的数据库.在此表中,每个"UserName"都是邮件和唯一标识.
我设法向我想要的任何用户发送邮件,但是不知道如何向数据库中的所有用户发送电子邮件.
我制作了存储过程,返回了我获得了多少用户,并考虑了调用函数
该发送每个用户的邮件,但我不知道如何使它跳到下一个UserName值.
用户表:

Hello everyone. i am a student and i am making a final project (website) using asp.net.
i have a database with table name "users". In this table every "UserName" is a mail and a unique identity.
i manage to send mail whatever user i want but don''t know how send e-mail to ALL users in database.
i made store procedure that returns how many users i got, thinking of calling function
That send mail per user but i don''t know how to make it skip to next UserName value.
user table :

    create TABLE Users
(
    [UserID] [int] IDENTITY(1,1) NOT NULL,
    UserName varchar(30) NOT NULL ,  -->Email, uniq
    UserPassword varchar(30)NOT NULL,
    PrivateName varchar(20) NOT NULL,
    FamilyName varchar(20) NOT NULL
)
GO


//------------------------------------------------ ---------------------
发送邮件的方法(其工作正常):
//------------------------------------------------ ---------------------


//---------------------------------------------------------------------
the method that sends mail (its working perfectly) :
//---------------------------------------------------------------------

MailMessage mm = new MailMessage("dira2team@gmail.com", *WHERE TO SEND*);  //send, receive.
 mm.Subject = txtSubject.Text;
 mm.Body = "name: " + txtName.Text + "<br /><br />mail: " + txtEmail.Text + "<br />" + txtBody.Text;
 if (FileUpload1.HasFile) //text box withuploading fields
 {
     string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
     mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
 }
 mm.IsBodyHtml = true;
 SmtpClient smtp = new SmtpClient();
 smtp.Host = "smtp.gmail.com";
 smtp.EnableSsl = true;
 System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
 NetworkCred.UserName = "dira2team@gmail.com";
 NetworkCred.Password = "****"; //the password
 smtp.UseDefaultCredentials = true;
 smtp.Credentials = NetworkCred;
 smtp.Port = 587;
 smtp.Send(mm);
 CaptchaMSG.Visible = false;
 txtCap.Text = " ";
 txtName.Text = " ";
 txtBody.Text = " ";
 txtEmail.Text = " ";
 txtSubject.Text = " ";

 lblMessage.Text = "thank you";



//------------------------------------------------ --------------------------
因此,我认为每次更改
时,用户的循环数量将与用户数量一样多 WHERE TO SEND值,将从SQL返回.
这是个好主意吗?
1.从存储过程中计数我有多少个用户.当正文是mail方法时,返回for循环中的值
for(int i = 0; i<返回值; i ++)
{
方法
}
2.每次将* WHERE TO SEND *新值放入->我该怎么做呢??
3.当我用新的用户名再次输入该方法时,

听起来不错吗?还有其他方法吗?我很困.
非常感谢.



//--------------------------------------------------------------------------
so i thought running it in a loop as many as Users i have every time changing the
WHERE TO SEND value, that will be returned from SQL.
is it a good idea??
1.count from store procedure how many users i have. Return the value in for loop when the body is the mail method
for (int i = 0 ; i < return value ; i++ )
{
the method
}
2. putting inside the *WHERE TO SEND* new value every time --> how do i do this??
3.when i++ entering the method again with new user name

is it sound right?? is there other ways? i am pretty stuck.
thank you a lot.

推荐答案

也许您可以看看SmtpClient.SendAsync.它看起来非常适合此目的,因为它不仅是异步的,而且还接受以下字符串:发件人,收件人,主题,正文和userToken(在完成时在回调中传递).
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendasync.aspx [ ^ ]

祝你好运!
Maybe you could have a look into SmtpClient.SendAsync. It looks very suitable for this purpose because it not only is asynchronous, but also takes string for: from, recipients, subject, body and userToken (passed in the callback on completion) .
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendasync.aspx[^]

Good luck!


在for循环中添加至"
in for loop add "To"
or (int i = 0 ; i < return value ; i++ )
{
  mm.To.Add(new MailAddress(emailid);
}


而不是获取结果计数,而是获取其中包含一行(用户名)的dataTable



您的查询=>获取具有所有用户电子邮件记录的dataTable,即
instead of getting the count of results get dataTable with one row in it (the UserName)
i.e


your query => get dataTable with all users email record i.e.
Select UserName from Users 




现在在代码中




now in the code

DataTable dt = objDAL.GetAllUsers() //for Example GetAllUsers is the method to execute the SQL query mentioned above.



现在




now


foreach (DataRow dr in dt.rows)
{
//now dr[0] is containing the email address
   SendEmailToSelectedUserName(dr[0]);
}



其中



where

SendEmailToSelectedUserName(string userEmail)
{
MailMessage mm = new MailMessage("dira2team@gmail.com", userEmail);
//rest of the code remain just as you written above
}


这篇关于您好,向所有用户发送邮件时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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