更新时间:2023-12-02 15:18:52
您需要做的第一件事是使用调试器查看实际情况。
您需要知道DataContext.GetEmailAdd_Sp(emplid)
返回的内容,可能是emplid
中的实际内容,并确切地将其放入emailFrom
- 因为错误消息是特定的,但无益:指定的字符串不是电子邮件地址所需的格式。并没有告诉你多少,但如果emailFrom中的值为空,因为其余代码返回null - FirstOrDefault应该这样做 - 那么你需要查看源代码:首先要知道的是你在吃什么价值,你得到什么价值。
我们可以为你做这件事 - 我们无权访问您的代码或数据库 - 因此您需要自己查看数据。如果您知道这一点,它应该变得更加清晰,或者至少向我们提供一些工作形式的信息并为您提供新的途径进行调查。
使用.TryParse而不是convert.ToInt
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)
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:
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);
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 byDataContext.GetEmailAdd_Sp(emplid)
, propbably what is actually inemplid
, and exactly what gets put intoemailFrom
- 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.
Used .TryParse instead of convert.ToInt