问题描述
这里是我的代码。当我在from,to,cc和bcc字段中输入电子邮件地址时,我的网络应用程序会发送电子邮件。但是当我只输入电子邮件地址时,要将cc和bcc字段设为空它给我错误
异常详细信息:System.ArgumentException:参数'address'不能是空字符串。
参数名称:地址
来源错误:
Here my code.when i enter email address in from,to,cc and bcc field my web app send the email.but when i only enter email address in from,to and make cc and bcc field empty it give me error
Exception Details: System.ArgumentException: The parameter 'address' cannot be an empty string.
Parameter name: address
Source Error:
Line 89: foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
Line 90: {
Line 91: mail.CC.Add(new MailAddress(emailCC));
Line 92: }
Line 93:
这里我的代码请帮我一个谷歌很多但我找不到
here my code please help me a google alot but i can not find the
private void SendEmail()
{
MailMessage mail = new MailMessage();
if (txtTo.Text != null)
{
char[] splitterrr = { ';' };
foreach (string emailTo in txtTo.Text.ToString().Split(splitterrr))
{
mail.To.Add(new MailAddress(emailTo));
}
}
//mail.To.Add(EmailAddress);
mail.From = new MailAddress(txtFrom.Text.ToString());
mail.Subject = txtSubject.Text;
mail.Body = edtrMessage.Content.ToString();
if (txtCc.Text != null)
{
char[] splitter = { ';' };
foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
{
mail.CC.Add(new MailAddress(emailCC));
}
}
else if (null == txtCc.Text || txtCc.Text.Length == 0 || txtCc.Text == String.Empty)
{
mail.CC.Add(new MailAddress(txtCc.Text.ToString()));
}
if (txtBcc.Text != null)
{
char[] splitterr = { ';' };
foreach (string emailBB in txtBcc.Text.ToString().Split(splitterr))
{
mail.Bcc.Add(new MailAddress(emailBB));
}
}
else if (null == txtBcc.Text || txtBcc.Text.Length == 0 || txtBcc.Text == String.Empty)
{
mail.Bcc.Add(new MailAddress(txtBcc.Text.ToString()));
}
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
mail.Attachments.Add(new Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType));
}
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential(txtFrom.Text.ToString(), txtPass.Text.ToString());
smtp.EnableSsl = true;
smtp.Send(mail);
}
推荐答案
foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
{
if(!string.IsNullOrWhitespace(emailCC)){
mail.CC.Add(new MailAddress(emailCC.Trim()));
}
}
或者,您可以通过使用.Split重载之一来提前捕获它: br $> b $ b
or, you can catch it earlier by using one of the .Split overloads:
foreach (string emailCC in txtCc.Text.ToString().Split(splitter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
{
if(emailCC.Trim() != ""){
mail.CC.Add(new MailAddress(emailCC.Trim()));
}
}
这篇关于cc和bcc发送错误的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!