Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i wrote code to send emails in asp.net MVC and it worked but issue was that i had to mention FROM field during sending mail so i tried to resolve that by writing a piece of code which send empID to store procedure and fetches EMAILADD from there against each user once session begins but problem is that it isn't getting any record from SP and returns null why ? + error:

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

SP: (working OK)

SQL
ALTER PROCEDURE [dbo].[GetEmailAdd_Sp]
	@emplid int
	AS
BEGIN
	Declare @emplEmail varchar(50)

	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

   Select emailAdd from HrEmployee where EmplID = @emplid
	--return @emplEmail

END

Code:
C#
public ActionResult dailyLog(String txtEmailTo, String txtCC, String txtSubject, String txtBody) 
        {   
            if (!String.IsNullOrEmpty(Session["Employee"] as string))
            {
                int emplid = Convert.ToInt32(Session["Employee"]);
                String emailFrom = Convert.ToString(DataContext.GetEmailAdd_Sp(emplid).FirstOrDefault());
                SmtpClient SmtpServer = new SmtpClient("mail.precisetech.com.pk");
                var mail = new MailMessage();
                mail.From = new MailAddress(emailFrom);

                string[] to = txtEmailTo.Split(';');
                foreach (string mailTo in to)
                {
                    mail.To.Add(mailTo);
                }

                string[] cc = txtCC.Split(';');
                foreach (string copyTo in cc) 
                {
                    MailAddress cCopy = new MailAddress(txtCC);
                    mail.CC.Add(copyTo);
                } 
                mail.Subject = txtSubject;
                mail.IsBodyHtml = true;
               // mail.CC = txtCC;
                string htmlBody;
                htmlBody = txtBody;
                mail.Body = txtBody;
                SmtpServer.Port = 25;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new System.Net.NetworkCredential(emailFrom, "");
                SmtpServer.EnableSsl = false;
               
			
	
	     try {
                SmtpServer.Send(mail);
                ViewBag.Confirmation = "Daily Log Has Been Submitted";
                return View();
             }
         
         catch (Exception ex) 
             {
               ViewBag.Confirmation = ex.Message;
               return View();
  	         }
      }


Session Employee stores Empid from previos login page i.e. Session["Employee"] = Convert.ToString(EmplID);
Posted
Updated 20-Feb-14 20:19pm
v2
Comments
Hunain Hafeez 21-Feb-14 2:34am    
?????

The first thing you need to do is look at what is actually going on, using the debugger.
You need to know what is being returned by DataContext.GetEmailAdd_Sp(emplid), propbably what is actually in emplid, and exactly what gets put into emailFrom - because the error message is specific, but unhelpful: "The specified string is not in the form required for an e-mail address." doesn't tell you much, but if the value in emailFrom is blank because the rest of the code is returning a null - which FirstOrDefault is supposed to do - then you need to look at the sources: and the first thing to find out is what values are you feeding in, and what values are you getting back.

We can;t do that for you - we don't have access to your code, or your database - so you need to look at the data for yourself. When you know that, it should become clearer, or at least give us some information to work form and suggest new avenues for you to investigate.
 
Share this answer
 
Used .TryParse instead of convert.ToInt
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900