0

我正在制作一个 Windows Phone 应用程序,我想在其中提供忘记密码的功能。当用户按下忘记密码按钮时,该应用程序将向用户存储的电子邮件 ID 发送一封电子邮件。由于 windows phone 中没有可用的 smtp 类,所以我想制作一个 asp.net web api,它将从应用程序接收电子邮件 ID(和密码)并向该 ID 发送电子邮件。我是网络服务的新手,对此了解为零,请指导我如何完成此任务,如果有人可以提供代码,则必须有一些这样的网络服务可用。

4

4 回答 4

4

下面是一个发送电子邮件的函数示例,您可以使用它。此外,下面还有几个链接可以指导您在 ASP.NET 中创建 Web 服务

实际上,您不需要为此创建 Web 服务(尽管建议这样做)。您可以创建一个快速而肮脏的 Web 表单,在其中传递参数,例如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(bcc, cc))
    {
        message.CC.Add(cc);
        message.Bcc.Add(bcc);
    }

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

    mailClient.EnableSsl = Config.SmptSettings.SSL;
    mailClient.Send(message); 
}
于 2013-08-23T09:27:14.567 回答
1
 ## Call this function in your WebApi controller ##

    private void sendEmailViaWebApi()
    {
                string subject = "Email Subject";
                string body = "Email body";
                string FromMail = "shahid@reckonbits.com.pk";
            string emailTo = "reciever@reckonbits.com.pk";
                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("shahid@reckonbits.com.pk", "your password");
                SmtpServer.EnableSsl = false;
                SmtpServer.Send(mail);
    }
于 2015-04-03T10:21:50.197 回答
0

对于使用 .NET Core 的用户,请注意目前不支持发送电子邮件。

通过 GitHub 查看 .NET Core 的以下问题: https ://github.com/dotnet/corefx/issues/1006

已经实施了一种替代解决方案 - MailKit: https ://github.com/jstedfast/MailKit

于 2016-10-10T15:50:13.160 回答
-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("yourid@gmail.com");
            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("yourid@gmail.com", "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;
    }
}
于 2013-08-23T09:57:16.223 回答