473,794 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with adding attachment to mail

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,MailEncodi ng.Base64);

if (!message.Attac hments.Contains (bijlage))

{

message.Attachm ents.Add(bijlag e);

}

}
Nov 17 '05 #1
3 1624
rk
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.

Nov 17 '05 #2
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.Collecti ons;

using System.Web;

using System.Web.Mail ;

using System.Text;

using System.Configur ation;

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.afz ender);

message = new MailMessage();

message.From = afzender;

message.Body = tekst;

message.Subject = onderwerp;

message.BodyEnc oding = 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,MailEncodi ng.Base64);

if (!message.Attac hments.Contains (bijlage))

{

message.Attachm ents.Add(bijlag e);

}

}

public void VerstuurMail()

{

if (smtpServer != null)

{

System.Web.Mail .SmtpMail.SmtpS erver = smtpServer;

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

}

}

}

}

"rk" <kr*****@hotmai l.com> schreef in bericht
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
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.

Nov 17 '05 #3
rk
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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1809
by: a c s | last post by:
Hello, I'm a php newbie and have been using the following php code fine (until now) to submit a form and create an email message. I recently moved the site from a local server to an ISP's server and now, whenever I attempt to submit the form, the entries I made are cleared and no e-mail is sent. I have no idea what the problem is - can anyone help? ------------begin extracted php---------------
1
4495
by: Nicole | last post by:
Hello! I hope there is someone out there who can shed some light on this for me. I have a module that is supposed to look at an access table, pull out each bid record, link to another table to find all of the people to send the email to and link to one more table to find who should be cc'd on the note. All of this works fine, except.... The arrays for the To and CC seem to exclude names from the list if there are multiple recipients...
4
8675
by: Trond A. S. Andersen | last post by:
Hi, all! I'm trying to use the System.Web.Mail. "package" combinded with System.Web.Mail.SmtpMail in order to send MS Excel spreadsheets attached to mail messages. However, sending one single spreadsheet attachment per message, seems to be causing som kind of corruption of the attachment, while several spreadsheets per works just fine. Consider the following three snippets of C# code:
3
1477
by: Bart Stes | last post by:
Hey Guys, I'm trying to write a very simple thing that allows a user to send an e-mail from a windows form app. I use the System.Web.Mail MailMessage class for this and then send my e-mail through a smtp server. The problem i have is with adding attachments. I use the following code public void AddAttachment (string file)
1
1202
by: Bart Stes | last post by:
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...
3
3881
by: huan | last post by:
Hi, I am creating a function using C# to send emails with attachment file. And I am using File Field control to get file for attachment. Sending email works fine on development machine, however when I deployed it on the production machine, it doesn't work. It gives me an error as below when I click send email button. And I tried to debug it. Seems like File field control's postedfile.filename is not working even I have
2
1642
by: Ray | last post by:
Hi Can anyone help with something that has beaten me. I am sending an email with an attachment, as per below. If I comment out the MailAttachment line then the mail gets sent, but with it in I get an error. Can anybody point me in the right direction? Thanks
3
4972
by: TomislaW | last post by:
I am sending word document with e-mail from asp.net 2.0 application. I read doc file like this:FileStream fs = System.IO.File.Open(docPath,FileMode.Open,FileAccess.Read,FileShare.Read); Then create message: MailMessage message = new MailMessage("mail@company.com", email); message.Subject = "Subject"; message.Body = ""; ContentType ct = new ContentType("application/vnd.ms-word"); Attachment data = new Attachment(ms,ct);
1
3498
by: krishan123456 | last post by:
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given below. function SendMail123($From,$FromName,$To,$ToName,$Subject,$Text,$Html,$AttmFiles){ $OB="----=_OuterBoundary_000"; $IB="----=_InnerBoundery_001"; $Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) or die("neither text nor...
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.