如何使用多行TextBox文本发送邮件? [英] How to send mail with the multiline TextBox text ?
问题描述
我正在尝试下面的代码,但它一次发送一封邮件。但我想拆分文本框,只需点击一下即可发送多封电子邮件......
private void btnsendnow_Click( object sender,EventArgs e)
{
// email_send
尝试
{
SmtpClient client = new SmtpClient( smtp.gmail.com, 587 跨度>);
client.EnableSsl = true ;
client.Timeout = 100000 ;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false ;
client.Credentials = new NetworkCredential( EMAIL-ID, PASSWORD);
MailMessage msg = new MailMessage();
msg.To.Add(txtto.Text);
msg.From = new MailAddress( aarnapraj@gmail.com跨度>);
msg.Subject = txtsubject.Text;
msg.Body = txtmessage.Text;
附件数据= 新附件(txtattachment.Text);
msg.Attachments.Add(data);
client.Send(msg);
// List< string> mail = txtto.Text.Trim()。分割(',')。ToList();
// client.Send(msg,mail);
MessageBox.Show( 成功发送.....);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
好吧,假设分隔线的东西是CRLF(\\\\ \\ n),试试这个:
替换:
msg.To.Add(txtto.Text);
使用:
foreach(txtto.Text.Split中的字符串地址(new [] {Environment.NewLine} ,StringSplitOptions.RemoveEmptyEntries))
{
msg.To.Add(address);
}
你需要使用拆分
拆分文本框文本,然后每行都有一个循环 - 即每个地址。
以下假设您在每个地址之间放置了一个回车换行符(即按下它们之间的Enter按钮)
< span class =code-keyword> var x = txtto.Text.Split(Environment.NewLine [ 0 ]);
foreach ( var s in x)
Console.WriteLine( | {0} |,s.Trim ());
注意在NewLine结尾处使用
- 这是因为我需要拆分[0]
char
而不是字符串。这也解释了当我显示结果时使用的.Trim()
... NewLine是CarriageReturn + LineFeed。
另请注意使用Environment.NewLine
而不是硬编码CRLF的ascii值
编辑 - 一个稍微整洁的解决方案
var x = textBox1.Text.Split( new [] {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
foreach ( var s in x)
Console.WriteLine( | {0} |,s);
I am trying with below code but it sends a single mail at a time.But i want to split the textbox for sending multiple emails at a single click......
private void btnsendnow_Click(object sender, EventArgs e)
{
//email_send
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("EMAIL-ID", "PASSWORD");
MailMessage msg = new MailMessage();
msg.To.Add(txtto.Text);
msg.From = new MailAddress("aarnapraj@gmail.com");
msg.Subject = txtsubject.Text;
msg.Body = txtmessage.Text;
Attachment data = new Attachment(txtattachment.Text);
msg.Attachments.Add(data);
client.Send(msg);
// List<string> mail = txtto.Text.Trim().Split(',').ToList();
//client.Send(msg,mail);
MessageBox.Show("successfully send.....");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Well, presuming the thing separating the lines is a CRLF (\r\n), try this:
Replace:
msg.To.Add(txtto.Text);
With:
foreach (string address in txtto.Text.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)) { msg.To.Add(address); }
[EDIT - Fixed incorrect split]
You need to useSplit
to split the text box text and then have a loop though each line - i.e. each address.
The following assumes that you have put a carriage-return-linefeed between each address (i.e. have hit the Enter button between them)
var x = txtto.Text.Split(Environment.NewLine[0]); foreach (var s in x) Console.WriteLine("|{0}|", s.Trim());
Note the use of[0]
at the end ofNewLine
- this is because I need to split on achar
not a string. This also explains the.Trim()
used when I'm displaying the results ... NewLine is CarriageReturn + LineFeed.
Also note the use ofEnvironment.NewLine
instead of hard-coding ascii values for CRLF
Edit - a slightly neater solution
var x = textBox1.Text.Split(new [] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries); foreach (var s in x) Console.WriteLine("|{0}|", s);
这篇关于如何使用多行TextBox文本发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!