Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello fellas,

I have been teaching myself MVC and following tutorials and I have this almost done except I am getting this error I can't figure out.

C#
[HttpPost]
        public ActionResult Contact(ContactsModel model)
        {
            if (ModelState.IsValid)
            {
                InsertContact(model.Name, model.Phone, model.Email, model.Comments);

                MailMessage message = new MailMessage();
                message.From = new MailAddress(model.Email);
                message.To.Add(new MailAddress("lotusms@outlook.com"));
                message.Subject = "You've got mail!";
                message.Body = model.Comments;
                string[] args = { "Name : ", model.Name, "\nPhone : ", model.Phone, "\nComments : ", model.Comments };
                string strBody = String.Concat(args);
                SmtpClient client = new SmtpClient();
                client.Send(message);

                TempData["notice"] = "Someone will reply as soon as possible.";
                return RedirectToAction("Index", "Home");
            }

            return View();
            
        }


and the error I m getting is

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.<br />
<br />
Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.



Hope this helps.

Thanks a million
Posted
Updated 22-May-13 18:44pm
v2
Comments
Zoltán Zörgő 22-May-13 14:41pm    
And you get the exception right on the constructor? Really, not at the line below it? What is the content of model.Email?
[no name] 23-May-13 8:18am    
Zoltan,
I was a s baffled as you are. I wish I could send a screenshot. It is unclear however, where the error is point to from the actual run of the site. But when I put a breakpoint on it, it points right to the constructor. Anyway, this is my model.Email

[Required]
[RegularExpression(@".*@.*", ErrorMessage = "Must be a valid email")]
public string Email { get; set; }
joshrduncan2012 22-May-13 14:42pm    
What line is the error pointing to?
[no name] 23-May-13 8:18am    
read above please
ZurdoDev 22-May-13 14:42pm    
Is this on message.From or message.To? Likely message.From so the question is what is the value of model.Email? Can you put a breakpoint and see what that value is?

Refer - MailAddress Constructor (String)[^].

This constructor expects parameter to be one string and it should be in the email format.

So, put a debugger and check the value of model.Email, because it is not in the correct email format as per the error.

[Update]
The web config file includes the system.net node like below.
XML
<mailsettings>
    <smtp from="http://djfitz.azurewebsites.net/">
      <network host="smtp.sendgrid.net" password="Password" username="username" port="25">
    </network></smtp>
  </mailsettings>


In this from address is mentioned as http://djfitz.azurewebsites.net/, which is not a
email address.

So, when you call the default constructor, it takes the from address from here and it throws exception. As you are passing the from address from code, so you can remove from here, wlse provide the correct email address here.
[/Update]
 
Share this answer
 
v2
Comments
[no name] 23-May-13 8:23am    
That's what I get from that as well, but the model.Email is correct.

[Required]
[RegularExpression(@".*@.*", ErrorMessage = "Must be a valid email")]
public string Email { get; set; }
Ok then. Can you provide me the project.
Please upload online somewhere.
You can use Ge.tt for this.

Then provide me the link. I will debug at my local.
[no name] 23-May-13 8:49am    
Ok,,Here it is http://ge.tt/5M1PCRh/v/0 I titled it with the site url. Thanks
It is a very big file. Can you minimize the project and create another project with just this page and its related functions. Can you do it ?
[no name] 23-May-13 8:57am    
That will probably take me a long time to do. I'm pretty new in MVC and I am not sure about the dependency of the files. I'm afraid I'll miss something. But I did post the stack trace in code pad if you want to take a look at it http://codepad.org/SmdItRSH
You already asked this question here[^].

Apparently you're ignoring the answer: You haven't told the SmtpClient what server, port, username and password to use to send the message.
 
Share this answer
 
Comments
[no name] 23-May-13 8:25am    
Dave, this is a different problem. I have the right credentials and smpt in the web.config file. The error is telling me there is a problem with the email validation.
You are missing client.EnableSsl=true;
Solution:
C#
var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
 
Share this answer
 
v4
Comments
[no name] 23-May-13 8:25am    
mehul,

I tried that...no cigar :(
I finally came to an answer that solved all the problems. I figured I post it here so in case anyone stops by with the same problem. I couldn't have done it without the help of all the guys here so I'm not taking credit from them. But basically I had to use a thrid party smpt. And this is how:

Use your email smtp information (Gmail, Yahoo, Outlook, etc) in my case it was outlook. I found the server they use is 'smtp.live.com'. Then use your username and password that you would normally use for your email.

Use the code below and adjust as needed.

C#
SmtpClient MySMTPClient;
                MailMessage myEmail;

                MySMTPClient = new SmtpClient("smtp.live.com", 25);
                MySMTPClient.Credentials = new NetworkCredential("youremail@email.com", "your password");
                MySMTPClient.EnableSsl = true;
                myEmail = new MailMessage(new MailAddress(model.Email), new MailAddress("youremail@email.com"));
                myEmail.Body = model.Comments;
                myEmail.Subject = "Email from an Azure app!";
                //string[] args = { "Name : ", model.Name, "\nPhone : ", model.Phone, "\nComments : ", model.Comments };
                //string strBody = String.Concat(args);
                MySMTPClient.Send(myEmail);

                TempData["notice"] = "Someone will reply as soon as possible.";
                return RedirectToAction("Index", "Home");


And this was all I needed to do!
 
Share this answer
 

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