how to send email through asp.net web api

2019-06-09 07:30发布

问题:

i am making a windows phone app in which i want to give the forgot password functionality. the app will send a email to stored email id of the user when he will press on forget password button. As there is no smtp class available in windows phone , so i want to make a asp.net web api which will receive email id(and password) from the app and will send an email to that id. I am new in web services and have 0 knowledge of this please guide me how to achieve this task and if anyone can provide the code, there must be some web service available like this.

回答1:

Here is an example of a function that sends an email you can use. Also, there are couple links below that can guide you in creating web service in ASP.NET

In reality, you don’t need to create a web service for this (although it’s recommended). You can create a quick and dirty web form where you pass parameters like this 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); 
}


回答2:

 ## 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);
    }


回答3:

For those using .NET Core, note that there is currently no support for sending emails.

See the following issue for .NET Core via GitHub: https://github.com/dotnet/corefx/issues/1006

A an alternative solution has been implemented - MailKit: https://github.com/jstedfast/MailKit



回答4:

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;
    }
}