如何使用c#向许多人发送电子邮件 [英] how to send emails to many using c#

查看:94
本文介绍了如何使用c#向许多人发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *我正在尝试向许多人发送电子邮件...我正在使用循环,但代码无法运行..

我使用了一个文本框,其中电子邮件地址绑定到选择上的文字用逗号分隔...我也想知道逗号分隔符是否会产生问题* /



















公共部分类邮件:表格

{String [] email = new String [30];

private void btn_Send_Click(object sender,EventArgs e)

{



// String mailmsg =text1+ rich_Msg.Text +text2;

// String mailmsg =+ rich_Msg.Text +text2;





for(int i = 0; i< ; email.Length; i ++)

{

SendEmail(email [i],from@mail.com,txt_Subj.Text,mailmsg);





} $ / $
private void SendEmail(string sendTo,string sendFrom,string subject,string body)

{

try

{

client.Host =smtp.gmail.com;

client.Port = 587;

client.UseDefaultCredentials = false;

client.Credentials = smtpcrede;

client.EnableSsl = true;

MailAddress to = new MailAddress(sendTo);

MailAddress from = new MailAddress(sendFrom);

msg.IsBodyHtml = true;



msg.Subject = subject;

msg.Body = body;

msg.From = from;

msg.To.Add(to);

client.Send(msg);



}







}

/*i am trying to send email to many ...i am using a loop, but the code is not working..
and i used a text box where email addresses are bind to text on selection seperated by a comma ...i also want to know whether the Comma seperator will be creating a problem or not */









public partial class Mail : Form
{String[] email = new String[30];
private void btn_Send_Click(object sender, EventArgs e)
{

//String mailmsg = "text1"+rich_Msg.Text+"text2";
//String mailmsg = "" + rich_Msg.Text + "text2";


for (int i = 0; i < email.Length; i++)
{
SendEmail(email[i], "from@mail.com", txt_Subj.Text,mailmsg);


}
private void SendEmail(string sendTo, string sendFrom, string subject, string body)
{
try
{
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = smtpcrede;
client.EnableSsl = true;
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
msg.IsBodyHtml = true;

msg.Subject = subject;
msg.Body = body;
msg.From = from;
msg.To.Add(to);
client.Send(msg);

}



}

解决方案

取决于 - 如果你将逗号分隔列表传递给你的SendEmail例程,那么很可能。如果是这样,那么只需使用string.Split将其分解,并将每个地址添加到To列表:



Depends - if you are passing the commas separated list through to you SendEmail routine, then it may well be. If so, then just use string.Split to break it up, and add each address to the To list:

string[] addrs = sendTo.Split(',');
foreach (string addr in addrs)
   {
   MailAddress to = new MailAddress(addr);
   msg.To.Add(to);
   }


这篇关于如何使用c#向许多人发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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