473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sending an Email from C#

8
hi,

i am trying to add the functionality into my code to send an email.

this is my code snippet.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Net.Mail;
  4.  
  5.  
  6. namespace logchecktest1
  7. {
  8.     class Program
  9.     {
  10.  
  11.         private void SendMail()
  12.         {
  13.             MailMessage MyMail = new MailMessage();
  14.             SmtpClient server = new SmtpClient("localhost");   // from server
  15.  
  16.  
  17.  
  18.             MyMail.From = new MailAddress("support@test.com");
  19.             MyMail.To = new MailAddress("kwareham@test.com");
  20.             MyMail.Subject = "arerror log";
  21.             MyMail.Body = "errors found in arerror log - go check -(@'C:\\Documents and Settings\\kwareham01\\Desktop\\test.txt')";
  22.  
  23.             server.Send(MyMail);
  24.  
  25.         }
however, the MyMail.To = new throws up the errors :
cannot implicitly convert type 'System.Net.Mail.MailAddress' to 'System.Net.MailAddressCollection' &
Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only

if i take out the 'To' and have just MyMail. = new ...
i get just the 1 error - 'identifier expected'

any help would be appreciated

thanks
Jun 2 '10 #1

✓ answered by balabaster

Disclaimer: I haven't coded this in Visual Studio, nor have I tried to compile it, I'm working purely off the exception you've provided, so this may not work, but should point you in the right direction.

MyMail.To is expecting a MailAddressCollection, not a single MailAddress and you are providing a MailAddress instead of a MailAddressCollection. What you need to do is create the collection and add the mail address to that collection before assigning it to MyMail.To.

Things you need to check are:

Is MyMail.To already set up as a MailAddressCollection, in which case, can you just add the mail address using MyMail.To.Add(new MailAddress("recipName")); or do you need to new up the collection.

My assumption is that it is not already initialized and as such you would need to use the following approach:

Expand|Select|Wrap|Line Numbers
  1. MailAddressCollection recips = new MailAddressCollection();
  2. recips.Add(new MailAddress("mailrecipient@address.com"));
  3. MyMail.To = recips;
Like I said, this may not work in its current form, I haven't used Visual Studio, nor have I attempted to compile this - it's just a hint that should get you thinking about the relationship between the exception you're getting and the approach you need to take to solve your problem. A good rule of thumb is that the exception usually provides a clue as to what you're doing wrong.

3 2559
balabaster
797 Expert 512MB
Disclaimer: I haven't coded this in Visual Studio, nor have I tried to compile it, I'm working purely off the exception you've provided, so this may not work, but should point you in the right direction.

MyMail.To is expecting a MailAddressCollection, not a single MailAddress and you are providing a MailAddress instead of a MailAddressCollection. What you need to do is create the collection and add the mail address to that collection before assigning it to MyMail.To.

Things you need to check are:

Is MyMail.To already set up as a MailAddressCollection, in which case, can you just add the mail address using MyMail.To.Add(new MailAddress("recipName")); or do you need to new up the collection.

My assumption is that it is not already initialized and as such you would need to use the following approach:

Expand|Select|Wrap|Line Numbers
  1. MailAddressCollection recips = new MailAddressCollection();
  2. recips.Add(new MailAddress("mailrecipient@address.com"));
  3. MyMail.To = recips;
Like I said, this may not work in its current form, I haven't used Visual Studio, nor have I attempted to compile this - it's just a hint that should get you thinking about the relationship between the exception you're getting and the approach you need to take to solve your problem. A good rule of thumb is that the exception usually provides a clue as to what you're doing wrong.
Jun 2 '10 #2
kieth
8
@balabaster
brilliant,

thank you, i'll have a play about and see if i can get it running.

thanks again!
Jun 2 '10 #3
balabaster
797 Expert 512MB
@kieth
No worries. If you get stuck, give us a another shout.
Jun 2 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul Lamonby | last post by:
Hi, I am sending a file from the server as an email attachment. The file is being attached no problem and sending the email, but I get an error when I try to open it saying it is corrupt....
13
by: joe215 | last post by:
I want my users to send emails from a Windows app that I am developing in Visual Basic.NET 2003. I found a good example of sending email to a SMTP server using the SmtpMail class. However, using...
7
by: Marcin | last post by:
Hello all! A few years ago I created a form with button which let me send an email with an attachment. It was created in Access 97. Now I would like to move this application into Access 2003....
17
by: Bonj | last post by:
Right guys. (I would like a solution to this in VB6 as this is what our needy app is written in, but any solutions that involve .NET would be much appreciated likewise as I could instantiate...
2
by: Mark | last post by:
I have a service that I've built that runs on our web server. The web service has a reference to the System.Web namespace so it can use the classes titled: MailMessage SmtpMail My web...
6
by: Eduardo Rosa | last post by:
Somebody knows how I queue email using .Net? thanks a lot
1
by: xin.yadong | last post by:
Hi: I have a shared function for sending Email using SMTP. It works fine in a ASP.NET web application. But when I use it in a VB.Net Windows application, it always gave me an error: "Could not...
6
by: Jack | last post by:
Hi, I am still new to .NET so, i'm sorry if my question is a bit too simple :-) I would like to know what is the "best-practice-way" of sending email from asp.net (VbScript). I want to make a...
31
by: happyse27 | last post by:
Hi All, I am trying for weeks how to send email from windows pc, which from my gmail account to my hotmail account. Using net::smtp module sending email failed,Kindly assist. (for the item d it...
6
by: Chocolade | last post by:
Hi, Im using System.Net.Mail to send email in my application it was working great without any problems untill this morning after like 20-30 tries it was sending the email ok then suddenly this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.