2013-08-22 3 views
0

나는 잊어 버린 암호 기능을 제공하고자하는 Windows phone 앱을 만들고 있습니다. 앱이 비밀번호 기억 버튼을 누를 때 사용자의 저장된 이메일 ID로 이메일을 보냅니다. Windows 전화에서 사용할 수있는 smtp 클래스가 없으므로 앱에서 이메일 ID (및 비밀번호)를 받게 될 asp.net 웹 API를 만들고 싶습니다. 그 ID로 이메일을 보냅니다. 웹 서비스에 익숙하지 않고이 지식이 0 개임이 이 작업을 수행하는 방법을 안내하고 누구든지 코드를 제공 할 수 있으면 이처럼 사용할 수있는 웹 서비스가 있어야합니다.asp.net 웹 api를 통해 이메일을 보내는 방법

+0

웹 API이 적응되지 않습니다, 당신은 필요하지 않습니다 RESTful 서비스, 대신 WCF에서 확인하십시오. – glautrou

답변

3

다음은 사용할 수있는 이메일을 보내는 기능의 예입니다. 또한 ASP.NET에서 웹 서비스를 만드는 데 도움이되는 몇 가지 링크가 있습니다.

실제로이 경우 웹 서비스를 만들 필요는 없습니다 (권장 사항 임). 당신이 그가 .NET을 사용하기 위해이 example.com/sendemail.aspx?account=jack.smith 같은 & ID = 223232343

private static void SendEmail(string from, string from_name, string to, string cc, string bcc, string subject, string body, bool isHtml) 
{ 
     SmtpClient mailClient = new SmtpClient(Config.SmptSettings.Server); 
     mailClient.Credentials = new NetworkCredential(Config.SmptSettings.UserName, Config.SmptSettings.Password); 
    mailClient.Port = Config.SmptSettings.Port; 

    MailMessage message = new MailMessage(); 
    if (!string.IsNullOrEmpty(from_name)) 
    { 
     message.From = new MailAddress(from, from_name); 
    } 
    else 
    { 
     message.From = new MailAddress(Formatter.UnFormatSqlInput(from)); 
    } 

    message.To.Add(new MailAddress(to)); 

    if (!string.IsNullOrEmpty(cc)) 
    { 
     message.CC.Add(cc); 
    } 

    if (!string.IsNullOrEmpty(bcc)) 
    { 
     message.Bcc.Add(bcc); 
    } 

    message.Subject = subject; 
    message.Body = body; 
    message.IsBodyHtml = isHtml; 

    mailClient.EnableSsl = Config.SmptSettings.SSL; 
    mailClient.Send(message); 
} 
+0

안녕하세요, 코드 – Mannu

+0

환영합니다. –

+0

현재이 기능은 .NET Framework 및 .NET Core를 사용하는 ASP.NET에서만 작동합니다. –

-5
string random; 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    string s1 = string.Empty; 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("select EmailId from tblEmail where EmailId='" + txtemail.Text + "'", con); 
    SqlDataReader dr =cmd.ExecuteReader(); 

    if (dr.HasRows) 
    { 
     while (dr.Read()) 
     { 
      s1 = dr.GetString(0); 
     } 
    } 
    dr.Close(); 

    if (s1.Equals(txtemail.Text)) 
    { 
     Session["Email"] = txtemail.Text; 

     try 
     { 
      Random rndm = new Random(); 
      random = rndm.Next(99999).ToString(); 
      MailMessage message = new MailMessage(); 
      message.From = new MailAddress("[email protected]"); 
      message.To.Add(new MailAddress(txtemail.Text)); 
      message.Subject = " Hello... This is Your Serial No to change your password:"; 
      message.Body = random.ToString(); 
      message.IsBodyHtml = true; 

      SmtpClient client = new SmtpClient(); 
      client.Host = "smtp.gmail.com"; 
      client.Port = 587;//Gmail port number 
      client.UseDefaultCredentials = true; 
      client.EnableSsl = true; 
      client.Credentials = new System.Net.NetworkCredential("[email protected]", "yourpassword"); 
      client.Send(message); 
      SqlCommand cmd1 = new SqlCommand("insert into tblRandom values('" + txtemail.Text + "','" + random.ToString() + "')", con); 

      cmd1.ExecuteNonQuery(); 
      con.Close(); 
      Response.Write("<script>alert('Security Code Successfully Sent in Your Email Id')</script>"); 

      Response.Redirect("~/anotherpage.aspx"); 
     } 
     catch (Exception) 
     { 
      // Response.Write(ee.Message); 
      lblmsg.Text = "Please Enter Email-Id.."; 
      lblmsg.Visible = true; 
      //MessageBox.Show("Please Enter Email-ID"); 
      //Response.Write("<script>alert('Please Enter Email-ID')</script>"); 
     } 
    } 
    else 
    { 
     lblmsg.Text = "Please Enter Correct Email-Id.."; 
     lblmsg.Visible = true; 
    } 
} 
+0

이 코드는 전자 메일로 보내기 For Forgot Password –

+0

SQL 주입에 대해 들어 본 적이 있습니까? EmailId = ' "+ txtemail.Text +"' "대신에 SQLParameters를 사용해야합니다. – marco

1
## Call this function in your WebApi controller ## 

    private void sendEmailViaWebApi() 
    { 
       string subject = "Email Subject"; 
       string body = "Email body"; 
       string FromMail = "[email protected]"; 
      string emailTo = "[email protected]"; 
       MailMessage mail = new MailMessage(); 
       SmtpClient SmtpServer = new SmtpClient("mail.reckonbits.com.pk"); 

       mail.From = new MailAddress(FromMail); 
       mail.To.Add(emailTo); 
       mail.Subject = subject; 
       mail.Body = body; 

       SmtpServer.Port = 25; 
       SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "your password"); 
       SmtpServer.EnableSsl = false; 
       SmtpServer.Send(mail); 
    } 
+0

수신자 메일 제공자의 방화벽에 문제가 있습니까? 인증이 필요하지 않습니까? –

0

를 매개 변수를 전달하는 곳 당신은 신속하고 더러운 웹 양식을 만들 수 있습니다 핵심 사항으로, 현재 이메일 전송에 대한 지원은 없습니다. https://github.com/dotnet/corefx/issues/1006

대체 솔루션이 구현되었습니다 - MailKit :

은 GitHub의를 통해 .NET 코어에 대한 다음 문제를 참조하십시오 https://github.com/jstedfast/MailKit

관련 문제