Click here to Skip to main content
16,011,930 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}
}
Posted

The problem is here:
C#
Attachment data = new Attachment(txtattachment.Text);
if (txtattachment.Text == "")
{
    msg.Attachments.Add(data);
    client.Send(msg);
}
else
{
    msg.Attachments.Add(data);
    client.Send(msg);
} 

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:
C#
if (!string.IsNullOrEmpty(txtattachment.Text))
{
    Attachment data = new Attachment(txtattachment.Text);
    msg.Attachments.Add(data);
}

client.Send(msg);
 
Share this answer
 
Comments
balajidileepkumar 30-Jan-15 8:32am    
what if the file path is wrong? user entered a text.. in the textbox???
Richard Deeming 30-Jan-15 8:35am    
If the user types in an incorrect file path, it would be better to show them an error message than to send the email without the attachment.
Thank u very much.......it works properly........
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900