Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have this code for sending emails from windows form app,and it works fine with one attachment, but I cant get it to run with more then one attachment
C#
mail.From = new MailAddress(Main.ml.Text);
            mail.Subject = subject.Text;
            mail.Body = message.Text;
            
            foreach (string s in to.Text.Split(';'))
            {
                mail.To.Add(s);
            }
foreach (Attachment attach in attachment.Text.Split(';'))
{
mail.Attachments.Add(attach)
}
            {
                mail.To.Add(s);
            }

            
            mail.Headers.Add("Disposition-Notification-To", Main.ml.Text);
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

            SmtpClient client = new SmtpClient(Main.smtp.Text);
            client.Port = 25;
            client.Credentials = new System.Net.NetworkCredential(emailusername.Text, emailpassword.Text);
            
                client.EnableSsl = true;
            
            client.Send(mail);

and I get error (cannot convert type string to system. mail.attachment )on that piece of highlighted code, I mean it doesnt work, it works with one attachment, but I dont know how to get multiple

[Edit]Code block added[/Edit]
Posted
Updated 17-Oct-12 7:51am
v2
Comments
Sergey Alexandrovich Kryukov 17-Oct-12 14:02pm    
You don't show what is attachment and what attachement.Text is equal to before splitting in case when you need your multiple attachments. Run it under debugger and see what it is.
--SA
shonezi 17-Oct-12 14:29pm    
I found this code on net , somebody also had the same problem

string attachments = attachment.Text;

char[] separator = {';'};

string[] myattach;

myattach = attachments.Split(separator);

if (attachments != null)

if (attachments.Length != 0)
foreach (string str in myattach)
{
if (str.Trim() != "")
{
mail.Attachments.Add(new Attachment(Path.GetFullPath(str)));

}
}

and on THIS ONE I get failure in sending mail????
shonezi 17-Oct-12 14:35pm    
I wonder if maybe the problem is that the mailFrom address that I use to send mail from is not used on this PC, or better said it is not created account in outlook on THIS PC that I am on

 
Share this answer
 
Comments
ridoy 17-Oct-12 14:15pm    
+5
Hello,
Try
mail.Attachments.Add(new Attachment(attach));

see here:
http://msdn.microsoft.com/en-us/library/56wesadc.aspx[^]

Valery.
 
Share this answer
 
v2
Comments
shonezi 17-Oct-12 14:07pm    
I get failure in sending mail
Valery Possoz 17-Oct-12 14:11pm    
The exception you asked a question about was "cannot convert type string to system. mail.attachment", that's fixed. What is the error you have now?
ridoy 17-Oct-12 14:14pm    
+5
Valery Possoz 17-Oct-12 14:17pm    
Thanks. ;)
shonezi 17-Oct-12 14:15pm    
sorry, I did something, i dont know what, but anyway when I made the change you suggested it still says the same error
Hi,

Please have a look into this link

send multiple email attachments in asp.net

Thanks
Tapan kumar
 
Share this answer
 
Comments
tarun kumar sahoo 18-Oct-12 10:39am    
hi thanks for sharing this
fjdiewornncalwe 18-Oct-12 15:56pm    
My vote of 1: Don't sockpuppet vote your own answers, please.
Kipperr 7-Mar-17 5:22am    
Here is a simpler (and I would say quite intuitive) solution for sending emails with attachments in C#.

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



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