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

0

Hello, I am trying to send an email from ASP.NET but something is wrong with me, I found that to use the mailer I should not pass plain text to it than variables , but my valuable is a String , I do not know if I'm passing something else.

Here's my complete method below

[HttpPost]
public ActionResult UploadRequest(HttpPostedFileBase postedFile, string name, string fn, string ft, int fs, string wiw, int dep, int cat, int schem, string email,
  string dat) {
  int status = 0;
  int seen = 0;
  int edit = 0;
  var send = "none";
  var appro = "none";
  int reject = 0;
  var exp = "30 days";
  byte[] bytes;
  using(BinaryReader br = new BinaryReader(postedFile.InputStream)) {
    bytes = br.ReadBytes(postedFile.ContentLength);
  }
  string constr = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
  using(SqlConnection con = new SqlConnection(constr)) {
    string query = "INSERT INTO Requests ([nameproject],[wiw],[format],[email],[fsize],[ftype],[fname]" +
      ",[categoriesid],[departmentid],[schemeid],[status],[expiration]," +
      "[seen],[edit],[createddate],[seendate],[answerdate],[rejected]" +
      ")" +
      " VALUES (@name,@wiw,@formato,@email,@size,@fType,@fname," +
      "@cat,@dep,@schem,@status,@exp,@seen,@edit,@dat,@seend,@ansd," +
      "@rej" +
      ")";
    using(SqlCommand cmd = new SqlCommand(query)) {
      cmd.Connection = con;
      cmd.Parameters.AddWithValue("@fname", Path.GetFileName(postedFile.FileName));
      cmd.Parameters.AddWithValue("@fType", ft);
      cmd.Parameters.AddWithValue("@formato", bytes);
      cmd.Parameters.AddWithValue("@size", fs);
      cmd.Parameters.AddWithValue("@wiw", wiw);
      cmd.Parameters.AddWithValue("@email", email);
      cmd.Parameters.AddWithValue("@name", name);
      cmd.Parameters.AddWithValue("@cat", cat);
      cmd.Parameters.AddWithValue("@dep", dep);
      cmd.Parameters.AddWithValue("@schem", schem);
      cmd.Parameters.AddWithValue("@dat", dat);
      cmd.Parameters.AddWithValue("@status", status);
      cmd.Parameters.AddWithValue("@exp", exp);
      cmd.Parameters.AddWithValue("@seen", seen);
      cmd.Parameters.AddWithValue("@edit", edit);
      cmd.Parameters.AddWithValue("@seend", send);
      cmd.Parameters.AddWithValue("@ansd", appro);
      cmd.Parameters.AddWithValue("@rej", reject);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
    }
  }

  sendUploadRequestMail(email, wiw);

  return View();
}

public void sendUploadRequestMail( string email , string wiw ) {

        string to = "[email protected]" ;
        string from = email;
        MailMessage message = new MailMessage(from, to);
        message.Subject = "Using the new SMTP client.";
        message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Host = "smtp.google.com";
        // Credentials are necessary if the server requires the client 
        // to authenticate before it will send e-mail on the client's behalf.
        // client.UseDefaultCredentials = true;

        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
                        ex.ToString());
        }
    }

I leave it to you only to have context where the data comes from, but what is relevant is the last thing. Thanks.

    
asked by E.Rawrdríguez.Ophanim 13.12.2017 в 21:00
source

2 answers

0

Good morning,

As I see it, I think you have confused the second parameter that the MailMessage class receives. The first is the sender and the second is the recipient.

If we go to Microsoft's help class MailMessage we see that the constructor that receives 2 Strings expects two strings with a certain format, and that if it does not comply with it, it will throw a Format exception, which is exactly what appears to you. If you want to add the body and the subject of the mail you have to access its properties as follows

message.Subject = "Asunto";
message.Body = "Cuerpo del mensaje";
    
answered by 13.12.2017 в 21:15
0

There seems to be an incompatibility in the format of the sent parameter. You could represent the string in email format, you can try the following and see what happens.

string to = "[email protected]" ;
string from = email;//Tiene que ser un formato de correo valido ejemplo: [email protected]
MailMessage message = new MailMessage();
//Damos el formato a una direccion de correo electronico
message.To.Add(new MailAddress(to));
message.From = new MailAddress(from);

It's the way I have it implemented and it works correctly for me. Greetings.

    
answered by 13.12.2017 в 23:44