HOW TO: Send email using System.Net.Mail

 Looks like they locked the thread in the FAQ section, so I'll just ask my question here.

 I"m using a version of the System.Net.Mail class that was posted in the FAQ section.

 
I'm trying to use it like this:

            string from = EmailTextBox.Text;
            string to = ContactIdentifier.EmailAddress;
            string bcc = "";
            string cc = "";
            string subject = subjectTextBox.Text;
            string body = DescriptionTextBox.Text;

           
            MailHelper.SendMailMessage(from, to, bcc, cc, subject, body);

I get the following message:

The specified string is not in the form required for an e-mail address.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

Source Error:

Line 115:
Line 116:
Line 117: MailHelper.SendMailMessage(from, to, bcc, cc, subject, body);
Line 118: }
Line 119: }

 

Does this mean i cannot post reference values, that the parameters must be actual lifteral values?

 

Thanks for any replies. 

0
Fuziononline
11/7/2007 5:20:37 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

7 Replies
1086 Views

Similar Articles

[PageSpeed] 56

The problem is that in 2.0, the MailMessage.Send method was changed to take parameters of type MailAddress and MailAddressCollection, so your helper will need to change the following when you create a new MailMessage:

 

1    MailMessage message = new MailMessage();
2    message.Subject = "Test Email";
3    message.From = new MailAddress("sender@sendingorg.com");
4    message.To = new MailAddressCollection().Add("person1@targetaddr.com,person2@targetaddr.com");
5    message.CC = new MailAddressCollection().Add(new MailAddress("john@somecompany.com", "John Smith"));
6    message.Body = "This is a test email";
7    
8    
 

There are a few variations and overloads for how you can add new MailAddress classes and add new mail addresses to the collections, so I've shown a couple of different ways to skin the cat above.

Hope that helps,

D. 

 


** Remember to mark posts as the answer to help future users. **
Visoft, Inc - Web Site | Blogs
0
DMAR78
11/7/2007 6:14:00 PM

 I'm still relatively new at this. If I make those changes in my mailhelper class do I have to put literal values in according to your examples? I need to pull the values out of the textboxes and pass them as paramaters to the class. So my example above wouldn't work?

0
Fuziononline
11/7/2007 6:27:57 PM

No, you can use variables where I've placed literals, as long as they're the right type.

 

For example.. assume I have a MailMessage class called message, I can do this.

 string myEmail = someone@someplace.com;

message.To = new MailAddressCollection().Add(new MailAddress(myEmail));

Hope that helps,

--D


** Remember to mark posts as the answer to help future users. **
Visoft, Inc - Web Site | Blogs
0
DMAR78
11/7/2007 9:47:32 PM

Try the Below Code: Working Perfectly:

send a plain text email:

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

send a simple Html email:

 //create the mail message
 MailMessage mail = new MailMessage();

 //set the addresses
 mail.From = new MailAddress("me@mycompany.com");
 mail.To.Add("you@yourcompany.com");

 //set the content
 mail.Subject = "This is an email";
 mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
 mail.IsBodyHtml = true;

 //send the message
 SmtpClient smtp = new SmtpClient("127.0.0.1");
 smtp.Send(mail);

For more on System.Net.Mail: www.systemnetmail.com

For more on System.Web.Mail: www.system.webmail.com

 

 

Thanks

 


Bilal Nazer N
MCTS
Please remember to click "Mark as Answer" on this post if it helped you.
0
nazerbilal
11/8/2007 3:55:33 AM

Try the Below Code: Working Perfectly:

send a plain text email:

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

send a simple Html email:

 //create the mail message
 MailMessage mail = new MailMessage();

 //set the addresses
 mail.From = new MailAddress("me@mycompany.com");
 mail.To.Add("you@yourcompany.com");

 //set the content
 mail.Subject = "This is an email";
 mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
 mail.IsBodyHtml = true;

 //send the message
 SmtpClient smtp = new SmtpClient("127.0.0.1");
 smtp.Send(mail);

For more on System.Net.Mail: www.systemnetmail.com

For more on System.Web.Mail: www.systemwebmail.com

 

 

Thanks

 


Bilal Nazer N
MCTS
Please remember to click "Mark as Answer" on this post if it helped you.
0
nazerbilal
11/8/2007 3:55:41 AM

This link will help you to find out your solution: http://www.codeproject.com/useritems/SmtpEmailComponent.asp


Ahsan Murshed
__________________________
Please "Mark as Answered" if helpful for you.
ASP BOSS
0
ahsanm
11/8/2007 5:49:40 AM

MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");

MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");

MailMessage message = new MailMessage(from, to);

message.Subject = "Using the SmtpClient class.";

message.Subject = "Using the SmtpClient class.";

message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";

// Add a carbon copy recipient.

MailAddress copy = new MailAddress("Notification_List@contoso.com");

message.CC.Add(copy);

SmtpClient client = new SmtpClient(server);

// Include credentials if the server requires them.

client.Credentials = CredentialCache.DefaultNetworkCredentials;

Console.WriteLine("Sending an e-mail message to {0} by using the SMTP host {1}.",

to.Address, client.Host);

client.Send(message);

 

 

Mark as answer

0
Saurav
11/8/2007 6:16:22 AM
Reply:

Similar Artilces:

Send Email from form using system.net.mail
Please see code behind below. This my message is being received by only with hard coded text of subject with no body.  How do I correct my syntax so that the information is passed from form fields to email message?  The form data is being inserted into the db and redirected to .aspx page.  Just need to get data from for fields to message.  -------------------------------------------- Imports System.Net.MailPartial Class apps_contactpage Inherits System.Web.UI.PageSub Send2Mail(ByVal sender As Object, ByVal e As FormViewInsertedEventArgs) 'create the mail...

Why do Sending SMTP Authenticated E-mail succeed using SmtpMail.Send() function but fail using SmtpClient.send() function ? Bugs in System.Net.Mail.SmtpClient when send a Authenticated E-mail?
I click btnOK button, the email can send OK, but click btnFail button,  the following error is displayed! why ? you can test the section code. The SMTP, username, password are valid!  Many thanks! "Syntax error, command unrecognized. The server response was: authentication failed,decode user password error" I have tested some other SMTP, somes is OK. I think there are some bugs with System.Net.Mail.SmtpClient when send a Authenticated E-mail with some SMTP!   //------------------------------------------.ASPX Code----------------------------------------...

Sending email using System.net.Mail
 I am trying to send email using Asp .Net. I am using System.Net.Mail namespace. The code i written is below MailMessage message = new MailMessage(); message.From = new MailAddress("myemail@domain.com"); message.To.Add(new MailAddress("someone@domain.com")); message.Subject = "Test"; message.Body = "This is a test message"; message.IsBodyHtml = false; SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587); mailClient.Credentials = ne...

Send email using System.Net.Mail
hi to all,  while i am sending mail to gmail ,i am facing problem.but i can send mail to other smtp adress like(smtp.horasoft.in)plz give advice how to send email to gmail(i didn't give any details other than  below code).thank you in advance.   protected void btnSubmit_Click(object sender, EventArgs e)        {                                     &nbs...

Sending Mail Using System.Net.Mail with out using User credentials
HI, I have an application where in i send mails based on some condition, the user is already authenticated, so i need not check the User Credentials.Right now this is how the piece lokks:- mailClient.Credentials = new NetworkCredential(USERID, USER_PWD,DOMAIN); I want to send the mail, with out validating the user credentials, Can i skip the above line. Please post aqlong with the sample code.   Thanx in Advance. Anil Kumar.   I think you cannot skip. but store user credentials in session variables after login and then pass.If this post was useful to you, please mark it as...

Problem with difference between System.Web.Mail and System.Net.Mail in using attachments
I have a web .Net app which sends emails with attachments.  After the email is sent I clean up aftermyself and delete the attachments from disk.   In upgrading to .Net 2  I changed the email logic from using system.web.mail to system.net.mail  (of course vs is telling me system.web.mail is obsolete).In .Net 1.1,  the files deleted with no problem but in .Net 2 when I attempt to delete  I receive The process cannot access the file ...' because it is being used by another process. If I change the code back to using system.web.mail I can delete the file...

Aspose.Email 1.1 Released!
Aspose.Email 1.1 Released! Version 1.1.0 Released on January 25 2005 ...

Sending Email through System.Net.Mail is generating errors "Failure Sending mail
hi Sending Email through System.Web.Mail is working fine but System.Net.Mail is generating errors "Failure Sending mail  i treid in all possible ways but did't  pls help in giving a solution thanks & Regards vittal Maybe you can show us some code and post the exact error mesg as well (by looking at the value in ex.tostring)?My HomePage - www.findingsteve.net"It is more blessed to give than to receive." Acts 20: 35 (NIV, Bible)Progress - "Never discourage anyone...who continually makes progress, no matter how slow." Plato System.Web.Ma...

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

cannot send email from webpage using System.Net.Mail on external smtp server
Every time I am trying to send email from my webpage, or using passwordrecovery tool , i get following error :: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed I went through all the existing posts, but none of them worked for me. I have already checked http://www,systemwebmail.com/ web.config settings are as follows <system.net>    <mailSettings>      <smtp from="noreply@allure.com"&g...

Is it possible to specify which font to use when sending email with Imports System.Net.Mail?
I've got a basic vb.net page that after the user clicks submit, I send a confirmation email with System.Net.Mail.   I'm able to bold, italics and add horizontal rules, but would also like to set the font it is sent in as Verdana.    I searched here for how to change the font  http://msdn.microsoft.com/en-us/library/system.net.mail.aspx   but didn't have any luck.  Do you have any suggestions?  Never make important decisions on a Monday! Yeah if it's a HTML email then use <span style="font: 12px arial;">Text i...

Problem in sending mail using system.net.mail
I am using C# and ASP.Net2.0 for sending mail. But my problem is it is not being sent. I am not being able to understand it. I am using my gmail account for it. I am using System.Net and System .Net.Mail for sending the same. But i am getting the error ''Failure Sending Mail. Cannot connect to the server" . I have internet connection. Plz help me. It's urgent. I am attaching my code.  MailMessage mailMsg = new MailMessage();MailAddress ma = new MailAddress(username@gmail.com); mailMsg.From = ma; mailMsg.To.Add(s);mailMsg.Subject = "Your...

.net 2 beta 2 - System.Net.Mail
Hi guysI'm using the System.Net.Mail namespace in .net version 2, beta 2, to send out some HTML formatted e-mails.However, for some reason when the e-mail are sent out some of the full stops from the e-mail are removed.For example, part of the e-mail contains this code<img src="http://www.avonandsomerset.police.uk/images/grey_box_bottom.gif" alt="">But this is what the code is like when the e-mail source code is viewed<img src="http://www.avonandsomerset.police.uk/images/grey_box_bottomgif" alt="">Is this a known bug in beta 2? If so, where can I view a list of known bugs an...

Web resources about - HOW TO: Send email using System.Net.Mail - asp.net.web-forms

List of areas in the United States National Park System - Wikipedia, the free encyclopedia
The National Park System of the United States is the collection of physical properties owned or administered by the National Park Service . This ...

Curtin University meteorite discovery could hold clues to solar system creation
... much, but a 1.7 kilogram rock discovered by a team from Curtin University on New Year's Eve could help solve the mystery of how the solar system ...

Panasonic to commercialize Facebook's Blu-ray cold storage systems
A couple of years ago, Facebook revealed it was using Blu-ray disks as a cost-efficient way to archive the billions of images that users uploaded ...

Robert Sarver's Purchase Of Mallorca Is An Indictment Of The American Soccer System
Phoenix Suns owner and amateur intergenerational psychologist Robert Sarver has purchased a controlling stake in Mallorca, a Spanish soccer club ...

We investigated Riot Games' new owner and its Chinese social credit system
... it appears I only scratched the surface. As of writing, the top comment on my story is a link to an Extra Credits video that exposes a system ...

Mitac Magellan SmartGPS navigation system adopted by Subaru
Mitac International has announced that its Magellan SmartGPS navigation system has been adopted by Japan-based automaker Subaru for vehicles ...

Comcast's Xfinity Home Security System Trivial to Hack -
Security researchers at Rapid7 have discovered vulnerabilities in Comcast s Xfinity Home Security system that could allow hackers to trick homeowners ...

Comcast Xfinity's Home Security System Is Easy To Disable, Claim Researchers
The cable giant says it is looking into claims that the jamming system's wireless signals disconnect intrusion sensors from hub. Cable providers ...

TCS joins auction of Dell's Perot Systems; Cognizant also in race
Perot Systems is major provider of IT consulting to hospitals and government departments

The Closest Star System To Ours Doesn't Have Any Planets (Yet), After All
In 2012, astronomers excitedly announced the discovery of Alpha Centauri Bb, the first exoplanet around our nearest neighboring star system. ...

Resources last updated: 1/6/2016 5:37:27 PM