Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MailAddressCollection addresses error on sending mail with system.net.mail

Posted on 2014-01-07
10
1,036 Views
Last Modified: 2014-01-17
while using system.net.mail to send email in asp.net, had encountered the following error:

System.ArgumentException: The parameter 'addresses' cannot be an empty string. Parameter name: addresses at System.Net.Mail.MailAddressCollection.Add(String addresses) at SendMail.btnSend_Click(Object sender, EventArgs e)

MailMessage mailMessage = new MailMessage();

                mailMessage.To.Add(txtReceiver.Text);
                mailMessage.CC.Add(txtCc.Text);
                mailMessage.Bcc.Add(txtBcc.Text);

                mailMessage.From = new MailAddress(txtSender.Text, "asdasda");  
                  //      mailMessage.From = txtSender.Text;
                  //      mailMessage.To = txtReceiver.Text;
                  //      mailMessage.Cc = txtCc.Text;
                  //      mailMessage.Bcc = txtBcc.Text;
                        mailMessage.Subject = txtSubject.Text;
                        mailMessage.Body = txtBody.Text;

             
                List<string> lststring = new List<string>();

                lststring.Add(txtReceiver.Text);
                lststring.Add(txtCc.Text);
                lststring.Add(txtBcc.Text);

                MailAddressCollection collection = new MailAddressCollection();

                collection.Add(new MailAddress(txtReceiver.Text));

not sure whether it will use one address or many addresses, if required many addresses, it may required list collection, but the list collecton could not be enter to collection.Add,
therefore, tried the single address, as stated in other forum, but the error is still there
0
Comment
Question by:doramail05
  • 7
  • 3
10 Comments
 
LVL 4

Expert Comment

by:ItWorked
ID: 39764195
Your mail message is type of what?
1) System.Net.Mail.MailMessage
2) System.Web.Mail.MailMessage
0
 
LVL 1

Author Comment

by:doramail05
ID: 39764202
previously used 2)

now use 1) after reading forums, for testing
0
 
LVL 1

Author Comment

by:doramail05
ID: 39764380
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Expert Comment

by:ItWorked
ID: 39764402
Is it reference site or you are pointing to a problem you posted there?
0
 
LVL 1

Author Comment

by:doramail05
ID: 39764407
no, not sure, but i was working on the source file found that,
0
 
LVL 1

Author Comment

by:doramail05
ID: 39764486
just that it could not send the attachment but only the email

error :
System.IO.IOException: The process cannot access the file 'C:..\Websites\SendEmailwithAttachment\timesheet.xlsx' because it is being used by another process.
i guess this works:

eventhough it is checked from the task manager it is not running there,

  
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("asdasda@gmail.com", "pwd"),
            };
            using (var message = new MailMessage("asdasd@gmail.com", "asddsasa@gmail.com")
            {
                Subject = "sadsasdaad",
                Body = "assadsadsada",

                

                

            })
            {

             //   string strFileName = null;

                if (inpAttachment2.PostedFile != null)
                {
                    HttpPostedFile attFile = inpAttachment2.PostedFile;
                    int attachFileLength = attFile.ContentLength;
                    if (attachFileLength > 0)
                    {
                        strFileName = Path.GetFileName(inpAttachment2.PostedFile.FileName);
                        inpAttachment2.PostedFile.SaveAs(Server.MapPath(strFileName));

                        Attachment attach = new Attachment(Server.MapPath(strFileName));

                        //MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
                        message.Attachments.Add(attach);
                        attach.Dispose();
                        //    attach2 = strFileName;
                    }
                }

                smtp.Send(message);
                message.Dispose();

Open in new window

0
 
LVL 4

Expert Comment

by:ItWorked
ID: 39764531
You need to remove memory references to close the file first before trying to access it..
inpAttachment2.PostedFile.SaveAs(Server.MapPath(strFileName));

Open in new window

inpAttachment2.PostedFile.FileContent.Dispose();

Open in new window

Attachment attach = new Attachment(Server.MapPath(strFileName));

Open in new window



Hope this works
0
 
LVL 1

Author Comment

by:doramail05
ID: 39764538
hi there,

        notice that there are only, contenttype, contentlength, inputstream, filename, could not really find FileContent

example :

          inpAttachment2.PostedFile.InputStream.Dispose();
0
 
LVL 1

Accepted Solution

by:
doramail05 earned 0 total points
ID: 39770159
solved the problem,

as the code above the attached one is called once before it is used again,

which rectify the problem
0
 
LVL 1

Author Closing Comment

by:doramail05
ID: 39788061
figure out the above code before this causes the problem
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
A phishing scam that claims a recipient’s credit card details have been “suspended” is the latest trend in spoof emails.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question