problem with adding attachment to mail

  • Thread starter Thread starter Bart Stes
  • Start date Start date
B

Bart Stes

Hey guys,

I'm try to write a very simple winform app that allows the users to send an
email. I use the System.Web.Mail MailMessage class for this.
But when it comes to adding an attachment, something appears to go wrong.
After I execute the following code, the MailMessage appears to have an
attachment (looked it up through a watch in debug mode), but when I actually
send the message, the attachment is not send along with it. So the message
arrives in my inbox, but without the attachment. What am I doing wrong,

Many thanks,

Bart

public void AddAttachment (string file)

{

MailAttachment bijlage = new MailAttachment(file,MailEncoding.Base64);

if (!message.Attachments.Contains(bijlage))

{

message.Attachments.Add(bijlage);

}

}
 
Your function looks alright. How do you know that the attachment is
not being sent? Have you inspected the message right before send to
see if the attachment is still there? Since you have not put the
complete code it is difficult to diagnose the issue. It might be
problem where a local copy of the object is passed thru an argument
and thus losing all the changes when it finally gets out of the
function, but it is still a guess.
 
Hey,

I don't know if the attachment isn't being sent, I just not that it doesn't
arrive along with the email. I get the email in my inbox but without the
attachment. The only thing I pass to the function is a string, so that
shouldn't be an issue. Here is the complete code of the class I wrote

using System;

using System.Collections;

using System.Web;

using System.Web.Mail;

using System.Text;

using System.Configuration;

namespace PT_proj1

{

/// <summary>

/// Summary description for Mail.

/// </summary>

public class Mail

{

private MailMessage message;

private string afzender;

private string smtpServer;

public Mail (string afz, string smtp)

{

message = new MailMessage();

this.afzender = afz;

this.smtpServer = smtp;

}

public void VulMail (ArrayList ontvangers, ArrayList cc,

string onderwerp, string tekst)

{

cc.Add(this.afzender);

message = new MailMessage();

message.From = afzender;

message.Body = tekst;

message.Subject = onderwerp;

message.BodyEncoding = Encoding.ASCII;

foreach(string adres in ontvangers)

{

if (message.To != "") message.To += ", ";

message.To += adres;

}

foreach(string adres in cc)

{

if (message.Cc != "") message.Cc += ", ";

message.Cc += adres;

}

}


public void AddAttachment (string file)

{

MailAttachment bijlage = new MailAttachment(file,MailEncoding.Base64);

if (!message.Attachments.Contains(bijlage))

{

message.Attachments.Add(bijlage);

}

}

public void VerstuurMail()

{

if (smtpServer != null)

{

System.Web.Mail.SmtpMail.SmtpServer = smtpServer;

System.Web.Mail.SmtpMail.Send(message);

}

}

}

}
 
I just executed the code piece that you put here and it all worked fine
for me. I did get an exception when I was trying to set the afz
parameter of the constructor to an address other than mine. But since
you are able to get the mail you are past this error. Are you able to
get the message that you set in the mail? Do you know if your server is
programmed to strip the attachment?

That's all I can think of right now.

--rk
 
Back
Top