c#发送邮件/消息/SMTP错误
我想在编码方面得到一些帮助。我正在尝试向电子邮件发送消息,但在尝试单击按钮并发送消息后,我不断收到错误消息。以下是编码:c#发送邮件/消息/SMTP错误,c#,email,smtp,msg,C#,Email,Smtp,Msg,我想在编码方面得到一些帮助。我正在尝试向电子邮件发送消息,但在尝试单击按钮并发送消息后,我不断收到错误消息。以下是编码: 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; usi
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;
using System.Net.Mail;
namespace CO6009DissertationV5
{
public partial class frmSendMsg : Form
{
public frmSendMsg()
{
InitializeComponent();
}
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
System.Net.NetworkCredential userpassword = new System.Net.NetworkCredential();
userpassword.UserName = "user@gmail.com";
userpassword.Password = "password";
client.Credentials = userpassword;
MailMessage msg = new MailMessage("user@gmail.com", "user@gmail.com");
**msg.To.Add(new MailAddress(txtBoxToEmail.Text));**
msg.Body = "<b> Sender's Name: </b>" + txtBoxName.Text + "<p><b> Sender's E-mail: </b>" + txtBoxEmail.Text + "<p><b>Sender's Subject: </b>" + txtBoxSubject.Text + "<p><b>Sender's Message: </b>" + "<p>" + txtBoxMsg.Text;
msg.Subject = txtBoxSubject.Text;
msg.IsBodyHtml = true;
try
{
client.Send(msg);
lblMsgStatus.Text = "<p> Message Successfully Sent </p>";
}
catch (Exception ex)
{
lblMsgStatus.Text = "Send failed";
}
}
}
}
当项目一直停在这里时,给出错误行:
{“参数'addresses'不能是空字符串。\r\n参数名称:addresses”}
任何帮助都将不胜感激。只有当txtBoxToEmail.Text不是有效的电子邮件地址时,此行才会失败
msg.To.Add(new MailAddress(txtBoxToEmail.Text));
如果仍然不清楚,请使用try-catch包装您的代码,并评估异常详细信息
编辑:我看到您的编辑,我认为异常消息“参数'addresses'不能是空字符串。\r\n参数名称:addresses”非常清楚只有当txtBoxToEmail.Text不是有效的电子邮件地址时,此行才会失败
msg.To.Add(new MailAddress(txtBoxToEmail.Text));
如果仍然不清楚,请使用try-catch包装您的代码,并评估异常详细信息
编辑:我看到您的编辑,我认为异常消息“参数'addresses'不能是空字符串。\r\n参数名称:addresses”非常清楚发送邮件之前最好验证您的EmailId。请添加以下方法 并在按钮单击事件中调用它,如下所示:
if (IsValidEmail(txtBoxToEmail.Text))
{
msg.To.Add(new MailAddress(txtBoxToEmail.Text));
}
验证电子邮件id的方法:
static bool IsValidEmail(string email)
{
try
{
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
最好在发送邮件之前验证您的EmailId。请添加以下方法 并在按钮单击事件中调用它,如下所示:
if (IsValidEmail(txtBoxToEmail.Text))
{
msg.To.Add(new MailAddress(txtBoxToEmail.Text));
}
验证电子邮件id的方法:
static bool IsValidEmail(string email)
{
try
{
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
错误消息非常清楚-收件人地址文本为空。这意味着
txtBoxToEmail.Text
为空
您可以通过在使用前检查地址来避免将来出现此类问题。您可以使用以下选项轻松检查空字符串,例如:
您可以添加更详细的检查,例如确保文本包含@
字符
不过,更好的解决方案是向控件和表单本身添加验证,这样用户就不能在没有输入正确输入的情况下发送电子邮件
Windows窗体提供了多种验证输入的方法,如中所述
您可以处理txtBoxToEmail
事件来检查有效地址,并防止用户离开控件,直到地址正确为止。事实上,文档的示例是电子邮件验证!:
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if(!ValidEmailAddress(textBox1.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textBox1.Select(0, textBox1.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textBox1, errorMsg);
}
}
private void textBox1_Validated(object sender, System.EventArgs e)
{
// If all conditions have been met, clear the ErrorProvider of errors.
errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
// Confirm that the e-mail address string is not empty.
if(String.IsNullOrWhitespace(emailAddress.Length))
{
errorMessage = "e-mail address is required.";
return false;
}
// Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
if(emailAddress.IndexOf("@") > -1)
{
if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
{
errorMessage = "";
return true;
}
}
errorMessage = "e-mail address must be valid e-mail address format.\n" +
"For example 'someone@example.com' ";
return false;
}
示例使用该组件在有问题的输入旁边显示感叹号和错误UI
您可以在中找到有关ErrorProvider的更多信息。错误消息非常清楚-收件人地址文本为空。这意味着
txtBoxToEmail.Text
为空
您可以通过在使用前检查地址来避免将来出现此类问题。您可以使用以下选项轻松检查空字符串,例如:
您可以添加更详细的检查,例如确保文本包含@
字符
不过,更好的解决方案是向控件和表单本身添加验证,这样用户就不能在没有输入正确输入的情况下发送电子邮件
Windows窗体提供了多种验证输入的方法,如中所述
您可以处理txtBoxToEmail
事件来检查有效地址,并防止用户离开控件,直到地址正确为止。事实上,文档的示例是电子邮件验证!:
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if(!ValidEmailAddress(textBox1.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textBox1.Select(0, textBox1.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textBox1, errorMsg);
}
}
private void textBox1_Validated(object sender, System.EventArgs e)
{
// If all conditions have been met, clear the ErrorProvider of errors.
errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
// Confirm that the e-mail address string is not empty.
if(String.IsNullOrWhitespace(emailAddress.Length))
{
errorMessage = "e-mail address is required.";
return false;
}
// Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
if(emailAddress.IndexOf("@") > -1)
{
if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
{
errorMessage = "";
return true;
}
}
errorMessage = "e-mail address must be valid e-mail address format.\n" +
"For example 'someone@example.com' ";
return false;
}
示例使用该组件在有问题的输入旁边显示感叹号和错误UI
您可以在中找到有关ErrorProvider的更多信息,最好的方法是检查txtBoxToEmail.Text的值。可能是空的或空的。
然后使用.To.Add(新邮件地址(txtBoxToEmail.Text)) 好方法是检查txtBoxToEmail.Text的值。可能是空的或空的。
然后使用.To.Add(新邮件地址(txtBoxToEmail.Text)) 切勿在此处使用您的用户/密码粘贴代码!你能添加你收到的错误信息的文本吗?还有检查和许多其他问题-很可能有人以前遇到过你的错误。这个错误是不言自明的。似乎您正在添加一封空邮件…错误很明显-文本框为空。发送消息前请检查文本框内容,例如使用
String.IsNullOrWhitespace
。更好的方法是,使用Windows窗体验证来防止用户使用空地址框发送。检查。请不要在此处使用您的用户/密码粘贴代码!你能添加你收到的错误信息的文本吗?还有检查和许多其他问题-很可能有人以前遇到过你的错误。这个错误是不言自明的。似乎您正在添加一封空邮件…错误很明显-文本框为空。发送消息前请检查文本框内容,例如使用String.IsNullOrWhitespace
。更好的方法是,使用Windows窗体验证来防止用户使用空地址框发送。检查。这与原始代码相同,包装在try/catch中。这不会执行任何类型的验证。OP已经将代码包装在一个try/catch
块中。@Panagiotis Kanavos你能告诉原代码中的空检查吗?@Panagiotis Kanavos我发布的答案将检查空。我已经解释过了。这与OP使用的代码相同。它不能解决问题。OP已接收到异常并已生成消息。验证意味着实际检查值是否为OKPanagiotis Kanavo:fine。你能告诉我在原始代码中哪里可以找到电子邮件id的空检查吗。我发布的上述代码将检查null。这也许是个好办法。如果你仍然认为这个解决方案被否决了。谢谢我试图在特性方面给出一些更好的解决方案。这与原始代码相同,包装在try/catch中。这不会执行任何类型的验证。OP已将代码包装在try/catch
块中。@Panagiotis Kanavos您能告诉原始代码中的空检查吗?@Panagiotis Kanavos