当我不会浏览发送电子邮件的文件时如何解决问题....? [英] How To Solve The Problem At When I Will Not Browse The File For Sending Email....?
问题描述
我正在做如何发送电子邮件,下面的代码是成功的工作但是当我没有浏览任何文件发送然后它发生运行时异常...所以如何解决它...提前感谢。 .......
private void btnsendnow_Click(object sender,EventArgs e)
{
// email_send
试试
{
SmtpClient客户端=新的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(EMAIL-ID);
msg.Subject = txtsubject.Text;
msg.Body = txtmessage.Text;
附件数据=新附件(txtattachment.Text);
if(txtattachment.Text ==)
{
msg .Attachments.Add(data);
client.Send(msg);
}
else
{
msg.Attachments.Add(data);
client.Send(msg);
}
/ / List< string> mail = txtto.Text.Trim()。分割(',')。ToList();
//client.Send(msg,mail);
MessageBox。显示(成功发送.....);
}
catch(例外情况)
{
MessageBox.Show(ex.Message);
}
}
private void btnbrowse_Click(object sender,EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if(dlg.ShowDialog()== DialogResult.OK)
{
txtattachment.Text = dlg.FileName.ToString();
}
}
I am doing how to send the emails,The below code is successfully work but when i am not browse any file to send then it occures a run time exception...so how to solve it...thanks in advance......
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("EMAIL-ID");
msg.Subject = txtsubject.Text;
msg.Body = txtmessage.Text;
Attachment data = new Attachment(txtattachment.Text);
if (txtattachment.Text == "")
{
msg.Attachments.Add(data);
client.Send(msg);
}
else
{
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);
}
}
private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
txtattachment.Text = dlg.FileName.ToString();
}
}
推荐答案
问题在于:
The problem is here:
Attachment data = new Attachment(txtattachment.Text);
if (txtattachment.Text == "")
{
msg.Attachments.Add(data);
client.Send(msg);
}
else
{
msg.Attachments.Add(data);
client.Send(msg);
}
即使用户没有选择文件,你也要创建一个 Attachmment
这将抛出 ArgumentNullException
。然后你有一个 if..else
块来测试用户是否选择了一个文件,但两个分支的内容是相同的。
试试这个:
You're creating an Attachmment
even if the user hasn't selected a file, which will throw an ArgumentNullException
. You then have an if..else
block to test whether the user has selected a file, but the content of both branches is identical.
Try this:
if (!string.IsNullOrEmpty(txtattachment.Text))
{
Attachment data = new Attachment(txtattachment.Text);
msg.Attachments.Add(data);
}
client.Send(msg);
非常感谢你.......它工作正常........
Thank u very much.......it works properly........
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");
client.Credentials = new NetworkCredential("EMAIL-ID", "PASSWORD");
MailMessage msg = new MailMessage();
msg.To.Add(txtto.Text);
msg.From = new MailAddress("EMAIL-ID");
msg.Subject = txtsubject.Text;
msg.Body = txtmessage.Text;
if(!string.IsNullOrEmpty(txtattachment.Text))
{
if(System.IO.File.Exists(txtattachment.Text))
{
Attachment data = new Attachment(txtattachment.Text);
msg.Attachments.Add(data);
}
}
client.Send(msg);
// List mail = txtto.Text.Trim().Split(',').ToList();
//client.Send(msg,mail);
MessageBox.Show("successfully send.....");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
txtattachment.Text = dlg.FileName.ToString();
}
}
检查后创建附件文件路径是正确的并将其附加到邮件,如果文件不存在,请不要附加到邮件
Create the attachment after checking the file path is correct and attach it to the message, don't attach to the message if the file is not present
这篇关于当我不会浏览发送电子邮件的文件时如何解决问题....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!